Skip to content

Commit

Permalink
117616: Fixed unique-decorators rule not working in IntelliJ because …
Browse files Browse the repository at this point in the history
…the decorator calls were registered every time the file was updated
  • Loading branch information
alexandrevryghem committed Oct 7, 2024
1 parent fe4e103 commit c0df9a2
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions lint/src/rules/ts/unique-decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ export enum Message {
DUPLICATE_DECORATOR_CALL = 'duplicateDecoratorCall',
}

const decoratorCalls: Map<string, Set<string>> = new Map();
/**
* Saves the decorators by decoratorName → file → Set<String>
*/
const decoratorCalls: Map<string, Map<string, Set<string>>> = new Map();

/**
* Keep a list of the files wo contain a decorator. This is done in order to prevent the `Program` selector from being
* run for every file.
*/
const fileWithDecorators: Set<string> = new Set();

export interface UniqueDecoratorsOptions {
decorators: string[];
Expand Down Expand Up @@ -75,6 +84,13 @@ export const rule = ESLintUtils.RuleCreator.withoutDocs({
create(context: TSESLint.RuleContext<Message, unknown[]>, options: any) {

return {
['Program']: () => {
if (fileWithDecorators.has(context.physicalFilename)) {
for (const decorator of options[0].decorators) {
decoratorCalls.get(decorator)?.get(context.physicalFilename)?.clear();
}
}
},
[`ClassDeclaration > Decorator > CallExpression[callee.name=/^(${options[0].decorators.join('|')})$/]`]: (node: TSESTree.CallExpression) => {
if (isTestFile(context)) {
return;
Expand All @@ -85,7 +101,9 @@ export const rule = ESLintUtils.RuleCreator.withoutDocs({
return;
}

if (!isUnique(node)) {
fileWithDecorators.add(context.physicalFilename);

if (!isUnique(node, context.physicalFilename)) {
context.report({
messageId: Message.DUPLICATE_DECORATOR_CALL,
node: node,
Expand Down Expand Up @@ -183,22 +201,29 @@ function callKey(node: TSESTree.CallExpression): string {
return key;
}

function isUnique(node: TSESTree.CallExpression): boolean {
function isUnique(node: TSESTree.CallExpression, filePath: string): boolean {
const decorator = (node.callee as TSESTree.Identifier).name;

if (!decoratorCalls.has(decorator)) {
decoratorCalls.set(decorator, new Set());
decoratorCalls.set(decorator, new Map());
}

if (!decoratorCalls.get(decorator)!.has(filePath)) {
decoratorCalls.get(decorator)!.set(filePath, new Set());
}

const key = callKey(node);

let unique = true;

if (decoratorCalls.get(decorator)?.has(key)) {
unique = !unique;
for (const decoratorCallsByFile of decoratorCalls.get(decorator)!.values()) {
if (decoratorCallsByFile.has(key)) {
unique = !unique;
break;
}
}

decoratorCalls.get(decorator)?.add(key);
decoratorCalls.get(decorator)?.get(filePath)?.add(key);

return unique;
}

0 comments on commit c0df9a2

Please sign in to comment.