|
6 | 6 |
|
7 | 7 | const utils = require('../utils')
|
8 | 8 |
|
9 |
| -/** |
10 |
| - * Collect used variables recursively. |
11 |
| - * |
12 |
| - * @param {Node} node - The node to collect. |
13 |
| - * @param {Object} used(optional) - The object to restore result. |
14 |
| - * @returns {Object} used variables. |
15 |
| - */ |
16 |
| -function collectUsed (node, used) { |
17 |
| - used = used || {} |
18 |
| - |
19 |
| - if (node.type === 'VExpressionContainer') { |
20 |
| - node.references.forEach(ref => { |
21 |
| - used[ref.id.name] = true |
22 |
| - }) |
23 |
| - } |
24 |
| - |
25 |
| - if (node.type === 'VAttribute') { |
26 |
| - collectUsed(node.value, used) |
27 |
| - } |
28 |
| - |
29 |
| - if (node.type === 'VElement') { |
30 |
| - node.children.forEach(child => collectUsed(child, used)) |
31 |
| - node.startTag.attributes.forEach(attr => collectUsed(attr, used)) |
32 |
| - } |
33 |
| - |
34 |
| - return used |
35 |
| -} |
36 |
| - |
37 | 9 | /**
|
38 | 10 | * Creates AST event handlers for require-v-for-key.
|
39 | 11 | *
|
40 | 12 | * @param {RuleContext} context - The rule context.
|
41 | 13 | * @returns {Object} AST event handlers.
|
42 | 14 | */
|
43 | 15 | function create (context) {
|
44 |
| - utils.registerTemplateBodyVisitor(context, { |
45 |
| - "VAttribute[directive=true][key.name='for']": function (node) { |
46 |
| - const vars = node.value.expression.left |
47 |
| - const used = collectUsed(node.parent.parent) |
| 16 | + const stack = [] |
| 17 | + let unused = [] |
48 | 18 |
|
49 |
| - // report unused. |
50 |
| - vars.filter(v => !used[v.name]).forEach(v => { |
51 |
| - context.report({ |
52 |
| - node, |
53 |
| - loc: v.loc, |
54 |
| - message: `'{{name}}' is defined but never used.`, |
55 |
| - data: { |
56 |
| - name: v.name |
57 |
| - } |
| 19 | + return utils.defineTemplateBodyVisitor(context, { |
| 20 | + 'VElement:exit' () { |
| 21 | + unused |
| 22 | + .forEach((node) => { |
| 23 | + context.report({ |
| 24 | + node, |
| 25 | + loc: node.loc, |
| 26 | + message: `'{{name}}' is defined but never used.`, |
| 27 | + data: { |
| 28 | + name: node.name |
| 29 | + } |
| 30 | + }) |
58 | 31 | })
|
59 |
| - }) |
| 32 | + unused = stack.pop() |
60 | 33 | },
|
61 |
| - |
62 |
| - "VAttribute[directive=false][key.name='scope']": function (node) { |
63 |
| - const used = collectUsed(node.parent.parent) |
64 |
| - |
65 |
| - if (!used[node.value.value]) { |
66 |
| - context.report({ |
67 |
| - node, |
68 |
| - loc: node.value.loc, |
69 |
| - message: `'{{name}}' is defined but never used.`, |
70 |
| - data: { |
71 |
| - name: node.value.value |
| 34 | + VElement (node) { |
| 35 | + stack.push(unused) |
| 36 | + unused = [] |
| 37 | + if (node.variables) { |
| 38 | + for (const variable of node.variables) { |
| 39 | + unused.push(variable.id) |
| 40 | + } |
| 41 | + } |
| 42 | + }, |
| 43 | + VExpressionContainer (node) { |
| 44 | + if (node.references) { |
| 45 | + for (const reference of node.references) { |
| 46 | + if (reference.mode !== 'w') { |
| 47 | + const name = reference.id.name |
| 48 | + unused = unused.filter((el) => el.name !== name) |
| 49 | + stack.forEach((list, i) => { |
| 50 | + stack[i] = list.filter((el) => el.name !== name) |
| 51 | + }) |
72 | 52 | }
|
73 |
| - }) |
| 53 | + } |
74 | 54 | }
|
75 | 55 | }
|
76 | 56 | })
|
77 |
| - |
78 |
| - return {} |
79 | 57 | }
|
80 | 58 |
|
81 | 59 | // ------------------------------------------------------------------------------
|
|
0 commit comments