diff --git a/docs/rules/no-unused-vars.md b/docs/rules/no-unused-vars.md
index cde104f..3ce57fa 100644
--- a/docs/rules/no-unused-vars.md
+++ b/docs/rules/no-unused-vars.md
@@ -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)
diff --git a/lib/rules/no-unused-vars.js b/lib/rules/no-unused-vars.js
index 205531f..6e3e39e 100644
--- a/lib/rules/no-unused-vars.js
+++ b/lib/rules/no-unused-vars.js
@@ -19,17 +19,34 @@ 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]
@@ -37,7 +54,15 @@ module.exports = {
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}`)
+ }
+ }
+ ] : []
})
}
}
diff --git a/tests/lib/rules/no-unused-vars.js b/tests/lib/rules/no-unused-vars.js
index 09cfde9..cd2eb64 100644
--- a/tests/lib/rules/no-unused-vars.js
+++ b/tests/lib/rules/no-unused-vars.js
@@ -52,6 +52,18 @@ tester.run('no-unused-vars', rule, {
},
{
code: ''
+ },
+ {
+ code: '',
+ options: [{ ignorePattern: '^_' }]
+ },
+ {
+ code: '',
+ options: [{ ignorePattern: '^ignore' }]
+ },
+ {
+ code: '',
+ options: [{ ignorePattern: 'ignore$' }]
}
],
invalid: [
@@ -82,6 +94,7 @@ tester.run('no-unused-vars', rule, {
{
code: '',
errors: ["'a' is defined but never used.", "'b' is defined but never used.", "'c' is defined but never used."]
+
},
{
code: '{{a}}
',
@@ -97,7 +110,33 @@ tester.run('no-unused-vars', rule, {
},
{
code: '{{value | x}}
',
+ errors: [{
+ message: "'x' is defined but never used.",
+ suggestions: [{
+ desc: 'Replace the x with _x',
+ output: '{{value | x}}
'
+ }]
+ }],
+ options: [{ ignorePattern: '^_' }]
+ },
+ {
+ code: '{{value}}
',
+ options: [{ ignorePattern: 'ignore$' }],
errors: ["'x' is defined but never used."]
+ },
+ {
+ code: '',
+ errors: ["'props' is defined but never used."],
+ options: [{ ignorePattern: '^ignore' }]
+ },
+ {
+ code: '',
+ errors: ["'props' is defined but never used."],
+ options: [{ ignorePattern: '^ignore' }]
+ },
+ {
+ code: '',
+ errors: ["'_i' is defined but never used."]
}
]
})