forked from vuejs/eslint-plugin-vue
/
no-unused-vars.js
69 lines (64 loc) · 1.95 KB
/
no-unused-vars.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @fileoverview disallow unused variable definitions of v-for directives or scope attributes.
* @author 薛定谔的猫<hh_2013@foxmail.com>
*/
'use strict'
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow unused variable definitions of v-for directives or scope attributes',
category: 'essential',
url: 'https://eslint.vuejs.org/rules/no-unused-vars.html'
},
fixable: 'code',
schema: [
{
'type': 'object',
'properties': {
'varIgnorePattern': {
'type': 'string'
}
},
'additionalProperties': false
}
]
},
create (context) {
const option = context.options[0] || { varIgnorePattern: '^_' }
const pattern = option['varIgnorePattern'] || '^_'
// only use for construct a regularExpression
// eslint-disable-next-line no-eval
const regExp = eval(`/${pattern}/`)
return utils.defineTemplateBodyVisitor(context, {
VElement (node) {
const variables = node.variables
for (
let i = variables.length - 1;
i >= 0 && !regExp.test(variables[i].id.name) && !variables[i].references.length;
i--
) {
const variable = variables[i]
context.report({
node: variable.id,
loc: variable.id.loc,
message: `'{{name}}' is defined but never used.`,
data: variable.id,
suggest: [
{
desc: 'Replace the unused-var with default ignore pattern',
fix: function (fixer) {
return fixer.replaceText(variable.id, `_${variable.id.name}`)
}
}
]
})
}
}
})
}
}