Skip to content

Commit 60a5f6c

Browse files
feat: add fixable, no more error when unused variable with prefix _ (#1070)
* feat: add fixable, no more error when unused variable with prefix _ implement proposal #1058 feat #1058 * fix: make fix optional,add varIgnorePattern * feat: change varIgnorepattern to ignorePattern, add test, update readme * docs: update doc
1 parent 7a790bc commit 60a5f6c

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

docs/rules/no-unused-vars.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,16 @@ This rule report variable definitions of v-for directives or scope attributes if
3333

3434
## :wrench: Options
3535

36-
Nothing.
37-
36+
```js
37+
{
38+
"vue/no-unsed-vars": [{
39+
"ignorePattern": '^_',
40+
}]
41+
}
42+
```
43+
- `ignorePattern` ... disables reporting when your definitions of v-for directives or scope attributes match your ignorePattern Regular expression. default `null`, will ignore nothing
44+
## :rocket: Suggestion
45+
- When your ignorePattern set to `^_`, we could provide a suggestion which add a prefix`_` to your variable and no more eslint error
3846
## :mag: Implementation
3947

4048
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-unused-vars.js)

lib/rules/no-unused-vars.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,50 @@ module.exports = {
1919
url: 'https://eslint.vuejs.org/rules/no-unused-vars.html'
2020
},
2121
fixable: null,
22-
schema: []
22+
schema: [
23+
{
24+
'type': 'object',
25+
'properties': {
26+
'ignorePattern': {
27+
'type': 'string'
28+
}
29+
},
30+
'additionalProperties': false
31+
}
32+
]
2333
},
2434

2535
create (context) {
36+
const option = context.options[0] || { }
37+
const pattern = option['ignorePattern']
38+
let regExp = null
39+
if (pattern) {
40+
regExp = new RegExp(pattern, 'u')
41+
}
2642
return utils.defineTemplateBodyVisitor(context, {
2743
VElement (node) {
2844
const variables = node.variables
2945

3046
for (
3147
let i = variables.length - 1;
32-
i >= 0 && !variables[i].references.length;
48+
// eslint-disable-next-line no-unmodified-loop-condition
49+
i >= 0 && !variables[i].references.length && (regExp === null || !regExp.test(variables[i].id.name));
3350
i--
3451
) {
3552
const variable = variables[i]
3653
context.report({
3754
node: variable.id,
3855
loc: variable.id.loc,
3956
message: `'{{name}}' is defined but never used.`,
40-
data: variable.id
57+
data: variable.id,
58+
suggest: pattern === '^_' ? [
59+
{
60+
desc: `Replace the ${variable.id.name} with _${variable.id.name}`,
61+
fix: function (fixer) {
62+
return fixer.replaceText(variable.id, `_${variable.id.name}`)
63+
}
64+
}
65+
] : []
4166
})
4267
}
4368
}

tests/lib/rules/no-unused-vars.js

+39
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ tester.run('no-unused-vars', rule, {
5252
},
5353
{
5454
code: '<template><div v-for="x in foo" :[x]></div></template>'
55+
},
56+
{
57+
code: '<template><div v-for="_ in foo" ></div></template>',
58+
options: [{ ignorePattern: '^_' }]
59+
},
60+
{
61+
code: '<template><div v-for="ignorei in foo" ></div></template>',
62+
options: [{ ignorePattern: '^ignore' }]
63+
},
64+
{
65+
code: '<template><div v-for="thisisignore in foo" ></div></template>',
66+
options: [{ ignorePattern: 'ignore$' }]
5567
}
5668
],
5769
invalid: [
@@ -82,6 +94,7 @@ tester.run('no-unused-vars', rule, {
8294
{
8395
code: '<template><div v-for="(a, b, c) in foo"></div></template>',
8496
errors: ["'a' is defined but never used.", "'b' is defined but never used.", "'c' is defined but never used."]
97+
8598
},
8699
{
87100
code: '<template><div v-for="(a, b, c) in foo">{{a}}</div></template>',
@@ -97,7 +110,33 @@ tester.run('no-unused-vars', rule, {
97110
},
98111
{
99112
code: '<template><div v-for="x in items">{{value | x}}</div></template>',
113+
errors: [{
114+
message: "'x' is defined but never used.",
115+
suggestions: [{
116+
desc: 'Replace the x with _x',
117+
output: '<template><div v-for="_x in items">{{value | x}}</div></template>'
118+
}]
119+
}],
120+
options: [{ ignorePattern: '^_' }]
121+
},
122+
{
123+
code: '<template><div v-for="x in items">{{value}}</div></template>',
124+
options: [{ ignorePattern: 'ignore$' }],
100125
errors: ["'x' is defined but never used."]
126+
},
127+
{
128+
code: '<template><span slot-scope="props"></span></template>',
129+
errors: ["'props' is defined but never used."],
130+
options: [{ ignorePattern: '^ignore' }]
131+
},
132+
{
133+
code: '<template><span><template scope="props"></template></span></template>',
134+
errors: ["'props' is defined but never used."],
135+
options: [{ ignorePattern: '^ignore' }]
136+
},
137+
{
138+
code: '<template><div v-for="_i in foo" ></div></template>',
139+
errors: ["'_i' is defined but never used."]
101140
}
102141
]
103142
})

0 commit comments

Comments
 (0)