Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin-template): [no-duplicate-attributes] Add option to ignore properties #1104

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -35,6 +35,12 @@ interface Options {
* Default: `true`
*/
allowTwoWayDataBinding?: boolean;
/**
* Input or output properties for which duplicate presence is allowed as an exception to the rule.
*
* Default: `[]`
*/
ignore?: string[];
}

```
Expand Down Expand Up @@ -346,6 +352,38 @@ interface Options {
~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

<br>

---

<br>

#### Custom Config

```json
{
"rules": {
"@angular-eslint/template/no-duplicate-attributes": [
"error",
{
"ignore": [
"class"
]
}
]
}
}
```

<br>

#### ❌ Invalid Code

```html
<input [name]="foo" class="css-static" name="bar" [class]="dynamic">
~~~~~~~~~~~~ ~~~~~~~~~~
```

</details>

<br>
Expand Down
Expand Up @@ -10,10 +10,18 @@ import {
} from '../utils/create-eslint-rule';
import { getOriginalAttributeName } from '../utils/get-original-attribute-name';

type Options = [{ readonly allowTwoWayDataBinding?: boolean }];
type Options = [
{
readonly allowTwoWayDataBinding?: boolean;
readonly ignore?: readonly string[];
},
];
export type MessageIds = 'noDuplicateAttributes' | 'suggestRemoveAttribute';
export const RULE_NAME = 'no-duplicate-attributes';
const DEFAULT_OPTIONS: Options[number] = { allowTwoWayDataBinding: true };
const DEFAULT_OPTIONS: Options[number] = {
allowTwoWayDataBinding: true,
ignore: [],
};

export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
Expand All @@ -34,6 +42,13 @@ export default createESLintRule<Options, MessageIds>({
default: DEFAULT_OPTIONS.allowTwoWayDataBinding,
description: `Whether or not two-way data binding is allowed as an exception to the rule.`,
},
ignore: {
type: 'array',
items: { type: 'string' },
uniqueItems: true,
default: DEFAULT_OPTIONS.ignore,
description: `Input or output properties for which duplicate presence is allowed as an exception to the rule.`,
},
},
additionalProperties: false,
},
Expand All @@ -44,7 +59,7 @@ export default createESLintRule<Options, MessageIds>({
},
},
defaultOptions: [DEFAULT_OPTIONS],
create(context, [{ allowTwoWayDataBinding }]) {
create(context, [{ allowTwoWayDataBinding, ignore }]) {
const parserServices = getTemplateParserServices(context);

return {
Expand All @@ -68,7 +83,15 @@ export default createESLintRule<Options, MessageIds>({
...duplicateOutputs,
] as const;

allDuplicates.forEach((duplicate) => {
const filteredDuplicates =
ignore && ignore.length > 0
? allDuplicates.filter(
(duplicate) =>
!ignore.includes(getOriginalAttributeName(duplicate)),
)
: allDuplicates;

filteredDuplicates.forEach((duplicate) => {
const loc = parserServices.convertNodeSourceSpanToLoc(
duplicate.sourceSpan,
);
Expand Down
Expand Up @@ -496,4 +496,44 @@ export const invalid = [
},
],
}),
convertAnnotatedSourceToFailureCase({
description: 'should not report ignored properties',
annotatedSource: `
<input [name]="foo" class="css-static" name="bar" [class]="dynamic">
~~~~~~~~~~~~ ^^^^^^^^^^
`,
options: [{ ignore: ['class'] }],
messages: [
{
char: '~',
messageId,
data: { attributeName: 'name' },
suggestions: [
{
messageId: suggestRemoveAttribute,
output: `
<input class="css-static" name="bar" [class]="dynamic">

`,
data: { attributeName: 'name' },
},
],
},
{
char: '^',
messageId,
data: { attributeName: 'name' },
suggestions: [
{
messageId: suggestRemoveAttribute,
output: `
<input [name]="foo" class="css-static" [class]="dynamic">

`,
data: { attributeName: 'name' },
},
],
},
],
}),
];