From 598471913e70ce39dba3dd11dee5fdec88c81c1f Mon Sep 17 00:00:00 2001 From: Alexandre Vryghem Date: Mon, 23 Sep 2024 11:41:09 +0200 Subject: [PATCH] 117616: Fixed unique-decorators rule to work across multiple files Also moved the logic to filter out unwanted decorators to the ESLint's selector and fixed a typo --- docs/lint/ts/rules/themed-component-classes.md | 4 ++-- lint/src/rules/ts/themed-component-classes.ts | 4 ++-- lint/src/rules/ts/unique-decorators.ts | 15 +++++---------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/docs/lint/ts/rules/themed-component-classes.md b/docs/lint/ts/rules/themed-component-classes.md index 1f4ec72801..e3ca8d6582 100644 --- a/docs/lint/ts/rules/themed-component-classes.md +++ b/docs/lint/ts/rules/themed-component-classes.md @@ -34,7 +34,7 @@ class Something { selector: 'ds-base-test-themable', standalone: true, }) -class TestThemeableTomponent { +class TestThemeableComponent { } ``` @@ -50,7 +50,7 @@ Filename: `lint/test/fixture/src/app/test/themed-test-themeable.component.ts` TestThemeableComponent, ], }) -class ThemedTestThemeableTomponent extends ThemedComponent { +class ThemedTestThemeableComponent extends ThemedComponent { } ``` diff --git a/lint/src/rules/ts/themed-component-classes.ts b/lint/src/rules/ts/themed-component-classes.ts index 527655adfa..c1514fee12 100644 --- a/lint/src/rules/ts/themed-component-classes.ts +++ b/lint/src/rules/ts/themed-component-classes.ts @@ -180,7 +180,7 @@ class Something { selector: 'ds-base-test-themable', standalone: true, }) -class TestThemeableTomponent { +class TestThemeableComponent { } `, }, @@ -195,7 +195,7 @@ class TestThemeableTomponent { TestThemeableComponent, ], }) -class ThemedTestThemeableTomponent extends ThemedComponent { +class ThemedTestThemeableComponent extends ThemedComponent { } `, }, diff --git a/lint/src/rules/ts/unique-decorators.ts b/lint/src/rules/ts/unique-decorators.ts index 17e4a547fb..659c4f500f 100644 --- a/lint/src/rules/ts/unique-decorators.ts +++ b/lint/src/rules/ts/unique-decorators.ts @@ -15,6 +15,8 @@ export enum Message { DUPLICATE_DECORATOR_CALL = 'duplicateDecoratorCall', } +const decoratorCalls: Map> = new Map(); + export const info: DSpaceESLintRuleInfo = { name: 'unique-decorators', meta: { @@ -51,10 +53,9 @@ export const info: DSpaceESLintRuleInfo = { export const rule = ESLintUtils.RuleCreator.withoutDocs({ ...info, create(context: TSESLint.RuleContext, options: any) { - const decoratorCalls: Map> = new Map(); return { - 'ClassDeclaration > Decorator > CallExpression': (node: TSESTree.CallExpression) => { // todo: limit to class decorators + [`ClassDeclaration > Decorator > CallExpression[callee.name=/^(${options[0].decorators.join('|')})$/]`]: (node: TSESTree.CallExpression) => { if (isTestFile(context)) { return; } @@ -64,13 +65,7 @@ export const rule = ESLintUtils.RuleCreator.withoutDocs({ return; } - // todo: can we fold this into the selector actually? - if (!(options[0].decorators as string[]).includes(node.callee.name)) { - // We don't care about this decorator - return; - } - - if (!isUnique(node, decoratorCalls)) { + if (!isUnique(node)) { context.report({ messageId: Message.DUPLICATE_DECORATOR_CALL, node: node, @@ -168,7 +163,7 @@ function callKey(node: TSESTree.CallExpression): string { return key; } -function isUnique(node: TSESTree.CallExpression, decoratorCalls: Map>): boolean { +function isUnique(node: TSESTree.CallExpression): boolean { const decorator = (node.callee as TSESTree.Identifier).name; if (!decoratorCalls.has(decorator)) {