Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions docs/rules/no-unused-vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ This rule report variable definitions of v-for directives or scope attributes if

## :wrench: Options

Nothing.

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

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-unused-vars.js)
Expand Down
31 changes: 28 additions & 3 deletions lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,50 @@ module.exports = {
url: 'https://eslint.vuejs.org/rules/no-unused-vars.html'
},
fixable: null,
schema: []
schema: [
{
'type': 'object',
'properties': {
'ignorePattern': {
'type': 'string'
}
},
'additionalProperties': false
}
]
},

create (context) {
const option = context.options[0] || { }
const pattern = option['ignorePattern']
let regExp = null
if (pattern) {
regExp = new RegExp(pattern, 'u')
}
return utils.defineTemplateBodyVisitor(context, {
VElement (node) {
const variables = node.variables

for (
let i = variables.length - 1;
i >= 0 && !variables[i].references.length;
// eslint-disable-next-line no-unmodified-loop-condition
i >= 0 && !variables[i].references.length && (regExp === null || !regExp.test(variables[i].id.name));
i--
) {
const variable = variables[i]
context.report({
node: variable.id,
loc: variable.id.loc,
message: `'{{name}}' is defined but never used.`,
data: variable.id
data: variable.id,
suggest: pattern === '^_' ? [
{
desc: `Replace the ${variable.id.name} with _${variable.id.name}`,
fix: function (fixer) {
return fixer.replaceText(variable.id, `_${variable.id.name}`)
}
}
] : []
})
}
}
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ tester.run('no-unused-vars', rule, {
},
{
code: '<template><div v-for="x in foo" :[x]></div></template>'
},
{
code: '<template><div v-for="_ in foo" ></div></template>',
options: [{ ignorePattern: '^_' }]
},
{
code: '<template><div v-for="ignorei in foo" ></div></template>',
options: [{ ignorePattern: '^ignore' }]
},
{
code: '<template><div v-for="thisisignore in foo" ></div></template>',
options: [{ ignorePattern: 'ignore$' }]
}
],
invalid: [
Expand Down Expand Up @@ -82,6 +94,7 @@ tester.run('no-unused-vars', rule, {
{
code: '<template><div v-for="(a, b, c) in foo"></div></template>',
errors: ["'a' is defined but never used.", "'b' is defined but never used.", "'c' is defined but never used."]

},
{
code: '<template><div v-for="(a, b, c) in foo">{{a}}</div></template>',
Expand All @@ -97,7 +110,33 @@ tester.run('no-unused-vars', rule, {
},
{
code: '<template><div v-for="x in items">{{value | x}}</div></template>',
errors: [{
message: "'x' is defined but never used.",
suggestions: [{
desc: 'Replace the x with _x',
output: '<template><div v-for="_x in items">{{value | x}}</div></template>'
}]
}],
options: [{ ignorePattern: '^_' }]
},
{
code: '<template><div v-for="x in items">{{value}}</div></template>',
options: [{ ignorePattern: 'ignore$' }],
errors: ["'x' is defined but never used."]
},
{
code: '<template><span slot-scope="props"></span></template>',
errors: ["'props' is defined but never used."],
options: [{ ignorePattern: '^ignore' }]
},
{
code: '<template><span><template scope="props"></template></span></template>',
errors: ["'props' is defined but never used."],
options: [{ ignorePattern: '^ignore' }]
},
{
code: '<template><div v-for="_i in foo" ></div></template>',
errors: ["'_i' is defined but never used."]
}
]
})