forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-no-duplicate-attributes.js
More file actions
179 lines (169 loc) · 6.7 KB
/
template-no-duplicate-attributes.js
File metadata and controls
179 lines (169 loc) · 6.7 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require('../../../lib/rules/template-no-duplicate-attributes');
const RuleTester = require('eslint').RuleTester;
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});
ruleTester.run('template-no-duplicate-attributes', rule, {
valid: [
`<template>
<div class="foo" id="bar"></div>
</template>`,
`<template>
<button type="button" disabled></button>
</template>`,
`<template>
{{helper arg1="a" arg2="b"}}
</template>`,
`<template>
{{#each items as |item|}}
{{item}}
{{/each}}
</template>`,
'<template>{{my-component firstName=firstName lastName=lastName}}</template>',
'<template> {{fullName}}</template>',
'<template><a class="btn">{{btnLabel}}</a></template>',
'<template>{{employee-profile employee=(hash firstName=firstName lastName=lastName age=age)}}</template>',
'<template>{{employee-profile employee=(hash fullName=(hash firstName=firstName lastName=lastName) age=age)}}</template>',
// Block form with params (no duplicates)
'<template>{{#my-component firstName=firstName lastName=lastName as |fullName|}}{{fullName}}{{/my-component}}</template>',
],
invalid: [
{
code: `<template>
<div class="foo" class="bar"></div>
</template>`,
output: `<template>
<div class="foo"></div>
</template>`,
errors: [
{
message: "Duplicate attribute 'class' found in the Element.",
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
<input type="text" disabled type="email" />
</template>`,
output: `<template>
<input type="text" disabled />
</template>`,
errors: [
{
message: "Duplicate attribute 'type' found in the Element.",
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
{{helper foo="bar" foo="baz"}}
</template>`,
output: `<template>
{{helper foo="bar"}}
</template>`,
errors: [
{
message: "Duplicate attribute 'foo' found in the MustacheStatement.",
type: 'GlimmerHashPair',
},
],
},
{
code: `<template>
{{#if condition key="a" key="b"}}
content
{{/if}}
</template>`,
output: `<template>
{{#if condition key="a"}}
content
{{/if}}
</template>`,
errors: [
{
message: "Duplicate attribute 'key' found in the BlockStatement.",
type: 'GlimmerHashPair',
},
],
},
{
code: '<template>{{my-component firstName=firstName lastName=lastName firstName=firstName}}</template>',
output: '<template>{{my-component firstName=firstName lastName=lastName}}</template>',
errors: [{ messageId: 'duplicateMustache', data: { name: 'firstName' } }],
},
{
code: '<template>{{#my-component firstName=firstName lastName=lastName firstName=firstName as |fullName|}}{{/my-component}}</template>',
output:
'<template>{{#my-component firstName=firstName lastName=lastName as |fullName|}}{{/my-component}}</template>',
errors: [{ messageId: 'duplicateBlock', data: { name: 'firstName' } }],
},
{
code: '<template><a class="btn" class="btn">{{btnLabel}}</a></template>',
output: '<template><a class="btn">{{btnLabel}}</a></template>',
errors: [{ messageId: 'duplicateElement', data: { name: 'class' } }],
},
{
code: '<template>{{employee-profile employee=(hash firstName=firstName lastName=lastName age=age firstName=firstName)}}</template>',
output:
'<template>{{employee-profile employee=(hash firstName=firstName lastName=lastName age=age)}}</template>',
errors: [{ messageId: 'duplicateSubExpr', data: { name: 'firstName' } }],
},
{
code: '<template>{{employee-profile employee=(hash fullName=(hash firstName=firstName lastName=lastName firstName=firstName) age=age)}}</template>',
output:
'<template>{{employee-profile employee=(hash fullName=(hash firstName=firstName lastName=lastName) age=age)}}</template>',
errors: [{ messageId: 'duplicateSubExpr', data: { name: 'firstName' } }],
},
],
});
const hbsRuleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser/hbs'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});
hbsRuleTester.run('template-no-duplicate-attributes (hbs)', rule, {
valid: [
'{{my-component firstName=firstName lastName=lastName}}',
'{{#my-component firstName=firstName lastName=lastName as |fullName|}} {{fullName}}{{/my-component}}',
'<a class="btn">{{btnLabel}}</a>',
'{{employee-profile employee=(hash firstName=firstName lastName=lastName age=age)}}',
'{{employee-profile employee=(hash fullName=(hash firstName=firstName lastName=lastName) age=age)}}',
],
invalid: [
{
code: '{{my-component firstName=firstName lastName=lastName firstName=firstName}}',
output: '{{my-component firstName=firstName lastName=lastName}}',
errors: [{ messageId: 'duplicateMustache', data: { name: 'firstName' } }],
},
{
code: '{{#my-component firstName=firstName lastName=lastName firstName=firstName as |fullName|}} {{fullName}}{{/my-component}}',
output:
'{{#my-component firstName=firstName lastName=lastName as |fullName|}} {{fullName}}{{/my-component}}',
errors: [{ messageId: 'duplicateBlock', data: { name: 'firstName' } }],
},
{
code: '<a class="btn" class="btn">{{btnLabel}}</a>',
output: '<a class="btn">{{btnLabel}}</a>',
errors: [{ messageId: 'duplicateElement', data: { name: 'class' } }],
},
{
code: '{{employee-profile employee=(hash firstName=firstName lastName=lastName age=age firstName=firstName)}}',
output: '{{employee-profile employee=(hash firstName=firstName lastName=lastName age=age)}}',
errors: [{ messageId: 'duplicateSubExpr', data: { name: 'firstName' } }],
},
{
code: '{{employee-profile employee=(hash fullName=(hash firstName=firstName lastName=lastName firstName=firstName) age=age)}}',
output:
'{{employee-profile employee=(hash fullName=(hash firstName=firstName lastName=lastName) age=age)}}',
errors: [{ messageId: 'duplicateSubExpr', data: { name: 'firstName' } }],
},
],
});