Skip to content

Commit dff7c46

Browse files
authored
fix: ignore attribute key with templates (#393)
* fix: ignore attribute key with templates * Update no-duplicate-attrs.test.js * Update no-duplicate-attrs.js
1 parent 54f6a75 commit dff7c46

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

packages/eslint-plugin/lib/rules/no-duplicate-attrs.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
const { RULE_CATEGORY } = require("../constants");
77
const { createVisitors } = require("./utils/visitors");
88
const { getRuleUrl } = require("./utils/rule");
9+
const { hasTemplate } = require("./utils/node");
910

1011
const MESSAGE_IDS = {
1112
DUPLICATE_ATTRS: "duplicateAttrs",
@@ -61,7 +62,10 @@ module.exports = {
6162
if (Array.isArray(node.attributes)) {
6263
const attrsSet = new Set();
6364
node.attributes.forEach((attr) => {
64-
if (attr.key && attrsSet.has(attr.key.value.toLowerCase())) {
65+
if (hasTemplate(attr.key)) {
66+
return;
67+
}
68+
if (attrsSet.has(attr.key.value.toLowerCase())) {
6569
context.report({
6670
node: attr,
6771
data: {

packages/eslint-plugin/tests/rules/no-duplicate-attrs.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ ruleTester.run("no-duplicate-attrs", rule, {
2929
.split("\n")
3030
.join("\r\n"),
3131
},
32+
{
33+
code: `
34+
<input
35+
{{#aria_label}}aria-label="{{aria_label}}"{{/aria_label}}
36+
{{^aria_label}}aria-labelledby="pl-big-o-input-{{uuid}}-label"{{/aria_label}}
37+
/>
38+
`,
39+
languageOptions: {
40+
parserOptions: {
41+
templateEngineSyntax: {
42+
"{{": "}}",
43+
},
44+
},
45+
},
46+
},
3247
],
3348
invalid: [
3449
{
@@ -138,6 +153,30 @@ ruleTester.run("no-duplicate-attrs", rule, {
138153
},
139154
],
140155
},
156+
{
157+
code: `<div foo foo {{aa}} {{aa}}> </div>`,
158+
languageOptions: {
159+
parserOptions: {
160+
templateEngineSyntax: {
161+
"{{": "}}",
162+
},
163+
},
164+
},
165+
errors: [
166+
{
167+
message: "The attribute 'foo' is duplicated.",
168+
suggestions: [
169+
{
170+
messageId: "removeAttr",
171+
data: {
172+
attrName: "foo",
173+
},
174+
output: `<div foo {{aa}} {{aa}}> </div>`,
175+
},
176+
],
177+
},
178+
],
179+
},
141180
],
142181
});
143182

0 commit comments

Comments
 (0)