Skip to content

Commit eb6222a

Browse files
authored
feat: add suggestion fixer in no-duplicate-attrs (#307)
1 parent 8c8b39a commit eb6222a

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
* @typedef { import("@html-eslint/types").StyleTag } StyleTag
44
* @typedef { import("@html-eslint/types").ScriptTag } ScriptTag
55
* @typedef { import("../types").RuleModule<[]> } RuleModule
6+
* @typedef { import("@html-eslint/types").Attribute } Attribute
7+
* @typedef { import("../types").SuggestionReportDescriptor } SuggestionReportDescriptor
68
*/
79

810
const { RULE_CATEGORY } = require("../constants");
911
const { createVisitors } = require("./utils/visitors");
1012

1113
const MESSAGE_IDS = {
1214
DUPLICATE_ATTRS: "duplicateAttrs",
15+
REMOVE_ATTR: "removeAttr",
1316
};
1417

1518
/**
@@ -26,14 +29,33 @@ module.exports = {
2629
},
2730

2831
fixable: null,
32+
hasSuggestions: true,
2933
schema: [],
3034
messages: {
3135
[MESSAGE_IDS.DUPLICATE_ATTRS]:
3236
"The attribute '{{attrName}}' is duplicated.",
37+
[MESSAGE_IDS.REMOVE_ATTR]:
38+
"Remove this duplicate '{{attrName}}' attribute.",
3339
},
3440
},
3541

3642
create(context) {
43+
/**
44+
* @param {Attribute} node
45+
* @returns {SuggestionReportDescriptor[]}
46+
*/
47+
function getSuggestions(node) {
48+
return [
49+
{
50+
messageId: MESSAGE_IDS.REMOVE_ATTR,
51+
fix: (fixer) => fixer.removeRange(node.range),
52+
data: {
53+
attrName: node.key.value,
54+
},
55+
},
56+
];
57+
}
58+
3759
/**
3860
* @param {Tag | StyleTag | ScriptTag} node
3961
*/
@@ -48,6 +70,7 @@ module.exports = {
4870
attrName: attr.key.value,
4971
},
5072
messageId: MESSAGE_IDS.DUPLICATE_ATTRS,
73+
suggest: getSuggestions(attr),
5174
});
5275
} else {
5376
attrsSet.add(attr.key.value.toLowerCase());

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ ruleTester.run("no-duplicate-attrs", rule, {
3636
errors: [
3737
{
3838
message: "The attribute 'foo' is duplicated.",
39+
suggestions: [
40+
{
41+
messageId: "removeAttr",
42+
data: {
43+
attrName: "foo",
44+
},
45+
output: `<div foo="foo1" > </div>`,
46+
},
47+
],
3948
},
4049
],
4150
},
@@ -44,6 +53,15 @@ ruleTester.run("no-duplicate-attrs", rule, {
4453
errors: [
4554
{
4655
message: "The attribute 'foo' is duplicated.",
56+
suggestions: [
57+
{
58+
messageId: "removeAttr",
59+
data: {
60+
attrName: "foo",
61+
},
62+
output: `<div foo > </div>`,
63+
},
64+
],
4765
},
4866
],
4967
},
@@ -63,6 +81,22 @@ ruleTester.run("no-duplicate-attrs", rule, {
6381
errors: [
6482
{
6583
message: "The attribute 'class' is duplicated.",
84+
suggestions: [
85+
{
86+
messageId: "removeAttr",
87+
output: `
88+
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
89+
class="feather feather-home"
90+
91+
>
92+
<path d="M3 919"></path>
93+
<polyline points="9 22"></polyline>
94+
</svg>
95+
`
96+
.split("\n")
97+
.join("\r\n"),
98+
},
99+
],
66100
},
67101
],
68102
},
@@ -76,9 +110,31 @@ ruleTester.run("no-duplicate-attrs", rule, {
76110
errors: [
77111
{
78112
message: "The attribute 'ID' is duplicated.",
113+
suggestions: [
114+
{
115+
messageId: "removeAttr",
116+
output: `
117+
<div id="1"
118+
119+
Id="1"
120+
></div>
121+
`,
122+
},
123+
],
79124
},
80125
{
81126
message: "The attribute 'Id' is duplicated.",
127+
suggestions: [
128+
{
129+
messageId: "removeAttr",
130+
output: `
131+
<div id="1"
132+
ID="1"
133+
134+
></div>
135+
`,
136+
},
137+
],
82138
},
83139
],
84140
},
@@ -103,6 +159,12 @@ templateRuleTester.run("[template] no-duplicate-attrs", rule, {
103159
errors: [
104160
{
105161
message: "The attribute 'foo' is duplicated.",
162+
suggestions: [
163+
{
164+
messageId: "removeAttr",
165+
output: `html\`<div foo="\${id1}" > </div>\`;`,
166+
},
167+
],
106168
},
107169
],
108170
},
@@ -111,6 +173,12 @@ templateRuleTester.run("[template] no-duplicate-attrs", rule, {
111173
errors: [
112174
{
113175
message: "The attribute 'foo' is duplicated.",
176+
suggestions: [
177+
{
178+
messageId: "removeAttr",
179+
output: `html\`<div foo > </div>\`;`,
180+
},
181+
],
114182
},
115183
],
116184
},

0 commit comments

Comments
 (0)