Skip to content

Commit e004975

Browse files
authored
Fixed false negatives when using ignorePattern for vue/no-unused-var rule. (#1163)
1 parent d206d35 commit e004975

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

lib/rules/no-unused-vars.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,31 @@ module.exports = {
3535

3636
create(context) {
3737
const option = context.options[0] || {}
38-
const pattern = option.ignorePattern
39-
let regExp = null
40-
if (pattern) {
41-
regExp = new RegExp(pattern, 'u')
38+
const ignorePattern = option.ignorePattern
39+
let ignoreRegEx = null
40+
if (ignorePattern) {
41+
ignoreRegEx = new RegExp(ignorePattern, 'u')
4242
}
4343
return utils.defineTemplateBodyVisitor(context, {
4444
VElement(node) {
4545
const variables = node.variables
46-
47-
for (
48-
let i = variables.length - 1;
49-
i >= 0 &&
50-
!variables[i].references.length &&
51-
// eslint-disable-next-line no-unmodified-loop-condition
52-
(regExp === null || !regExp.test(variables[i].id.name));
53-
i--
54-
) {
46+
for (let i = variables.length - 1; i >= 0; i--) {
5547
const variable = variables[i]
48+
49+
if (variable.references.length) {
50+
break
51+
}
52+
53+
if (ignoreRegEx != null && ignoreRegEx.test(variable.id.name)) {
54+
continue
55+
}
5656
context.report({
5757
node: variable.id,
5858
loc: variable.id.loc,
5959
message: `'{{name}}' is defined but never used.`,
6060
data: variable.id,
6161
suggest:
62-
pattern === '^_'
62+
ignorePattern === '^_'
6363
? [
6464
{
6565
desc: `Replace the ${variable.id.name} with _${variable.id.name}`,

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

+5
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ tester.run('no-unused-vars', rule, {
157157
{
158158
code: '<template><div v-for="_i in foo" ></div></template>',
159159
errors: ["'_i' is defined but never used."]
160+
},
161+
{
162+
code: '<template><div v-for="(a, _i) in foo" ></div></template>',
163+
options: [{ ignorePattern: '^_' }],
164+
errors: ["'a' is defined but never used."]
160165
}
161166
]
162167
})

0 commit comments

Comments
 (0)