forked from angular-eslint/angular-eslint
/
no-duplicate-attributes.ts
133 lines (126 loc) · 4.03 KB
/
no-duplicate-attributes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import type {
TmplAstBoundAttribute,
TmplAstBoundEvent,
TmplAstElement,
TmplAstTextAttribute,
} from '@angular-eslint/bundled-angular-compiler';
import { getTemplateParserServices } from '@angular-eslint/utils';
import { createESLintRule } from '../utils/create-eslint-rule';
import { getOriginalAttributeName } from '../utils/get-original-attribute-name';
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,
ignore: [],
};
export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description:
'Ensures that there are no duplicate input properties or output event listeners',
recommended: false,
},
hasSuggestions: true,
schema: [
{
type: 'object',
properties: {
allowTwoWayDataBinding: {
type: 'boolean',
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,
},
],
messages: {
noDuplicateAttributes: 'Duplicate attribute `{{attributeName}}`',
suggestRemoveAttribute: 'Remove attribute `{{attributeName}}`',
},
},
defaultOptions: [DEFAULT_OPTIONS],
create(context, [{ allowTwoWayDataBinding, ignore }]) {
const parserServices = getTemplateParserServices(context);
return {
Element$1({ inputs, outputs, attributes }: TmplAstElement) {
const duplicateInputsAndAttributes = findDuplicates([
...inputs,
...attributes,
]);
const filteredOutputs = allowTwoWayDataBinding
? outputs.filter((output) => {
return !inputs.some(
(input) =>
input.sourceSpan.start === output.sourceSpan.start &&
input.sourceSpan.end === output.sourceSpan.end,
);
})
: outputs;
const duplicateOutputs = findDuplicates(filteredOutputs);
const allDuplicates = [
...duplicateInputsAndAttributes,
...duplicateOutputs,
] as const;
const filteredDuplicates =
ignore && ignore.length > 0
? allDuplicates.filter(
(duplicate) =>
!ignore.includes(getOriginalAttributeName(duplicate)),
)
: allDuplicates;
filteredDuplicates.forEach((duplicate) => {
const loc = parserServices.convertNodeSourceSpanToLoc(
duplicate.sourceSpan,
);
const data = {
attributeName: getOriginalAttributeName(duplicate),
} as const;
context.report({
loc,
messageId: 'noDuplicateAttributes',
data,
suggest: [
{
messageId: 'suggestRemoveAttribute',
fix: (fixer) =>
fixer.removeRange([loc.start.column, loc.end.column + 1]),
data,
},
],
});
});
},
};
},
});
function findDuplicates<
TAttributeType extends
| TmplAstBoundEvent
| TmplAstBoundAttribute
| TmplAstTextAttribute,
>(elements: readonly TAttributeType[]): readonly TAttributeType[] {
return elements.filter((element) => {
return elements.some(
(otherElement) =>
otherElement !== element &&
getOriginalAttributeName(otherElement) ===
getOriginalAttributeName(element),
);
});
}