|
6 | 6 |
|
7 | 7 | const utils = require('../utils')
|
8 | 8 |
|
| 9 | +/** |
| 10 | + * @typedef {import('vue-eslint-parser').AST.Node} Node |
| 11 | + * @typedef {import('vue-eslint-parser').AST.VElement} VElement |
| 12 | + * @typedef {import('vue-eslint-parser').AST.Variable} Variable |
| 13 | + */ |
| 14 | +/** |
| 15 | + * @typedef {Variable['kind']} VariableKind |
| 16 | + */ |
| 17 | + |
| 18 | +// ------------------------------------------------------------------------------ |
| 19 | +// Helpers |
| 20 | +// ------------------------------------------------------------------------------ |
| 21 | + |
| 22 | +/** |
| 23 | + * Groups variables by directive kind. |
| 24 | + * @param {VElement} node The element node |
| 25 | + * @returns { { [kind in VariableKind]?: Variable[] } } The variables of grouped by directive kind. |
| 26 | + */ |
| 27 | +function groupingVariables(node) { |
| 28 | + /** @type { { [kind in VariableKind]?: Variable[] } } */ |
| 29 | + const result = {} |
| 30 | + for (const variable of node.variables) { |
| 31 | + const vars = result[variable.kind] || (result[variable.kind] = []) |
| 32 | + vars.push(variable) |
| 33 | + } |
| 34 | + return result |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Checks if the given variable was defined by destructuring. |
| 39 | + * @param {Variable} variable the given variable to check |
| 40 | + * @returns {boolean} `true` if the given variable was defined by destructuring. |
| 41 | + */ |
| 42 | +function isDestructuringVar(variable) { |
| 43 | + const node = variable.id |
| 44 | + /** @type {Node} */ |
| 45 | + let parent = node.parent |
| 46 | + while (parent) { |
| 47 | + if ( |
| 48 | + parent.type === 'VForExpression' || |
| 49 | + parent.type === 'VSlotScopeExpression' |
| 50 | + ) { |
| 51 | + return false |
| 52 | + } |
| 53 | + if (parent.type === 'Property' || parent.type === 'ArrayPattern') { |
| 54 | + return true |
| 55 | + } |
| 56 | + parent = parent.parent |
| 57 | + } |
| 58 | + return false |
| 59 | +} |
| 60 | + |
9 | 61 | // ------------------------------------------------------------------------------
|
10 | 62 | // Rule Definition
|
11 | 63 | // ------------------------------------------------------------------------------
|
@@ -41,38 +93,52 @@ module.exports = {
|
41 | 93 | ignoreRegEx = new RegExp(ignorePattern, 'u')
|
42 | 94 | }
|
43 | 95 | return utils.defineTemplateBodyVisitor(context, {
|
| 96 | + /** |
| 97 | + * @param {VElement} node |
| 98 | + */ |
44 | 99 | VElement(node) {
|
45 |
| - const variables = node.variables |
46 |
| - for (let i = variables.length - 1; i >= 0; i--) { |
47 |
| - const variable = variables[i] |
| 100 | + const vars = groupingVariables(node) |
| 101 | + for (const variables of Object.values(vars)) { |
| 102 | + let hasAfterUsed = false |
48 | 103 |
|
49 |
| - if (variable.references.length) { |
50 |
| - break |
51 |
| - } |
| 104 | + for (let i = variables.length - 1; i >= 0; i--) { |
| 105 | + const variable = variables[i] |
52 | 106 |
|
53 |
| - if (ignoreRegEx != null && ignoreRegEx.test(variable.id.name)) { |
54 |
| - continue |
55 |
| - } |
56 |
| - context.report({ |
57 |
| - node: variable.id, |
58 |
| - loc: variable.id.loc, |
59 |
| - message: `'{{name}}' is defined but never used.`, |
60 |
| - data: variable.id, |
61 |
| - suggest: |
62 |
| - ignorePattern === '^_' |
63 |
| - ? [ |
64 |
| - { |
65 |
| - desc: `Replace the ${variable.id.name} with _${variable.id.name}`, |
66 |
| - fix(fixer) { |
67 |
| - return fixer.replaceText( |
68 |
| - variable.id, |
69 |
| - `_${variable.id.name}` |
70 |
| - ) |
| 107 | + if (variable.references.length) { |
| 108 | + hasAfterUsed = true |
| 109 | + continue |
| 110 | + } |
| 111 | + |
| 112 | + if (ignoreRegEx != null && ignoreRegEx.test(variable.id.name)) { |
| 113 | + continue |
| 114 | + } |
| 115 | + |
| 116 | + if (hasAfterUsed && !isDestructuringVar(variable)) { |
| 117 | + // If a variable after the variable is used, it will be skipped. |
| 118 | + continue |
| 119 | + } |
| 120 | + |
| 121 | + context.report({ |
| 122 | + node: variable.id, |
| 123 | + loc: variable.id.loc, |
| 124 | + message: `'{{name}}' is defined but never used.`, |
| 125 | + data: variable.id, |
| 126 | + suggest: |
| 127 | + ignorePattern === '^_' |
| 128 | + ? [ |
| 129 | + { |
| 130 | + desc: `Replace the ${variable.id.name} with _${variable.id.name}`, |
| 131 | + fix(fixer) { |
| 132 | + return fixer.replaceText( |
| 133 | + variable.id, |
| 134 | + `_${variable.id.name}` |
| 135 | + ) |
| 136 | + } |
71 | 137 | }
|
72 |
| - } |
73 |
| - ] |
74 |
| - : [] |
75 |
| - }) |
| 138 | + ] |
| 139 | + : [] |
| 140 | + }) |
| 141 | + } |
76 | 142 | }
|
77 | 143 | }
|
78 | 144 | })
|
|
0 commit comments