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
This commit is contained in:
Wojciech Duda 2024-03-14 14:52:48 +01:00 committed by GitHub
parent e675a12dfd
commit 0a1fd8dd2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 82 additions and 1 deletions

View File

@ -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
}
};

View File

@ -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<unknown[], MessageIds>({
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
});
}
};
}
});