-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
2 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
lib/eslint-angular/src/rules/no-angular-material-selectors/no-angular-material-selectors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / run e2e / Lint
|
||
const message = filetypeErrors.find((fileTypeError) => | ||
context.getFilename().match(fileTypeError.regexp) | ||
) || { messageId: 'noAngularMaterialSelectors' }; | ||
|
||
context.report({ | ||
node, | ||
messageId: message.messageId as MessageIds | ||
}); | ||
} | ||
}; | ||
} | ||
}); |