From 0a1fd8dd2ea1f4a61ea377b57cda028dda764d4e Mon Sep 17 00:00:00 2001 From: Wojciech Duda <69160975+wojd0@users.noreply.github.com> Date: Thu, 14 Mar 2024 14:52:48 +0100 Subject: [PATCH] AAE-20779 add custom eslint rule for material selectors (#9427) * AAE-20779 create custom eslint rule * AAE-20779 add dynamic error messages depending on the filetype * AAE-20779 update regex for classes * AAE-20779 remove console log, fix default message * AAE-20779 lint fix * AAE-20779 simplified error messages --- lib/eslint-angular/index.ts | 6 +- .../no-angular-material-selectors.ts | 77 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 lib/eslint-angular/src/rules/no-angular-material-selectors/no-angular-material-selectors.ts diff --git a/lib/eslint-angular/index.ts b/lib/eslint-angular/index.ts index d5f855bf1a..d9f29435a8 100644 --- a/lib/eslint-angular/index.ts +++ b/lib/eslint-angular/index.ts @@ -18,9 +18,13 @@ import useNoneEncapsulationRule, { RULE_NAME as useNoneEncapsulationRuleName } from './src/rules/use-none-component-view-encapsulation/use-none-component-view-encapsulation'; +import noAngularMaterialSelectors, { + RULE_NAME as noAngularMaterialSelectorsRuleName +} from './src/rules/no-angular-material-selectors/no-angular-material-selectors'; export = { rules: { - [useNoneEncapsulationRuleName]: useNoneEncapsulationRule + [useNoneEncapsulationRuleName]: useNoneEncapsulationRule, + [noAngularMaterialSelectorsRuleName]: noAngularMaterialSelectors } }; diff --git a/lib/eslint-angular/src/rules/no-angular-material-selectors/no-angular-material-selectors.ts b/lib/eslint-angular/src/rules/no-angular-material-selectors/no-angular-material-selectors.ts new file mode 100644 index 0000000000..d54341d282 --- /dev/null +++ b/lib/eslint-angular/src/rules/no-angular-material-selectors/no-angular-material-selectors.ts @@ -0,0 +1,77 @@ +/*! + * @license + * Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { createESLintRule } from '../../utils/create-eslint-rule/create-eslint-rule'; +import type { TSESTree } from '@typescript-eslint/utils'; + +export const RULE_NAME = 'no-angular-material-selectors'; + +const ASTSelectors = [ + ':not(Property[key=template]) > Literal[value=/(mat|mdc)-(?!datetimepicker)/i]', + ':not(Property[key.name="template"]) TemplateLiteral[quasis.value.raw=/(mat|mdc)-(?!datetimepicker)/i]' +]; + +const messages = { + noAngularMaterialSelectors: 'Using Angular Material internal selectors is not allowed', + useAngularMaterialTestingHarness: 'Use testing harness instead of Angular Material internal selectors', + useE2ELocatorVariables: 'Use locator variables instead of Angular Material internal selectors' +}; + +type MessageIds = keyof typeof messages; + +const filetypeErrors: {regexp: RegExp; messageId: MessageIds}[] = [ + { + regexp: /.*\.spec\.ts/, + messageId: 'useAngularMaterialTestingHarness' + }, + { + regexp: /.*\.e2e\.ts/, + messageId: 'useE2ELocatorVariables' + } +]; + +/** + * Custom ESLint rule for detecting the usage of internal Angular Material selectors + */ +export default createESLintRule({ + name: RULE_NAME, + meta: { + type: 'suggestion', + docs: { + description: 'Disallows using Angular Material internal selectors', + recommended: 'error' + }, + hasSuggestions: true, + schema: [], + messages + }, + defaultOptions: [], + create(context) { + return { + [ASTSelectors.join(',')](node: TSESTree.Literal | TSESTree.TemplateLiteral) { + const message = filetypeErrors.find((fileTypeError) => + context.getFilename().match(fileTypeError.regexp) + ) || { messageId: 'noAngularMaterialSelectors' }; + + context.report({ + node, + messageId: message.messageId as MessageIds + }); + } + }; + } +});