Skip to content

Commit

Permalink
AAE-20779 add custom eslint rule for material selectors (#9427)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wojd0 authored Mar 14, 2024
1 parent e675a12 commit 0a1fd8d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/eslint-angular/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};
Original file line number Diff line number Diff line change
@@ -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) {

Check warning on line 65 in lib/eslint-angular/src/rules/no-angular-material-selectors/no-angular-material-selectors.ts

View workflow job for this annotation

GitHub Actions / run e2e / Lint

Unknown word: "TSES"

Check warning on line 65 in lib/eslint-angular/src/rules/no-angular-material-selectors/no-angular-material-selectors.ts

View workflow job for this annotation

GitHub Actions / run e2e / Lint

Unknown word: "TSES"
const message = filetypeErrors.find((fileTypeError) =>
context.getFilename().match(fileTypeError.regexp)
) || { messageId: 'noAngularMaterialSelectors' };

context.report({
node,
messageId: message.messageId as MessageIds
});
}
};
}
});

0 comments on commit 0a1fd8d

Please sign in to comment.