|
| 1 | +/** |
| 2 | + * @fileoverview Disallow unused refs. |
| 3 | + * @author Yosuke Ota |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const utils = require('../utils') |
| 12 | + |
| 13 | +// ------------------------------------------------------------------------------ |
| 14 | +// Helpers |
| 15 | +// ------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +/** |
| 18 | + * Extract names from references objects. |
| 19 | + * @param {VReference[]} references |
| 20 | + */ |
| 21 | +function getReferences(references) { |
| 22 | + return references.filter((ref) => ref.variable == null).map((ref) => ref.id) |
| 23 | +} |
| 24 | + |
| 25 | +// ------------------------------------------------------------------------------ |
| 26 | +// Rule Definition |
| 27 | +// ------------------------------------------------------------------------------ |
| 28 | + |
| 29 | +module.exports = { |
| 30 | + meta: { |
| 31 | + type: 'suggestion', |
| 32 | + docs: { |
| 33 | + description: 'disallow unused refs', |
| 34 | + categories: undefined, |
| 35 | + url: 'https://eslint.vuejs.org/rules/no-unused-refs.html' |
| 36 | + }, |
| 37 | + fixable: null, |
| 38 | + schema: [], |
| 39 | + messages: { |
| 40 | + unused: "'{{name}}' is defined as ref, but never used." |
| 41 | + } |
| 42 | + }, |
| 43 | + /** @param {RuleContext} context */ |
| 44 | + create(context) { |
| 45 | + /** @type {Set<string>} */ |
| 46 | + const usedRefs = new Set() |
| 47 | + |
| 48 | + /** @type {VLiteral[]} */ |
| 49 | + const defineRefs = [] |
| 50 | + let hasUnknown = false |
| 51 | + |
| 52 | + /** |
| 53 | + * Report all unused refs. |
| 54 | + */ |
| 55 | + function reportUnusedRefs() { |
| 56 | + for (const defineRef of defineRefs) { |
| 57 | + if (usedRefs.has(defineRef.value)) { |
| 58 | + continue |
| 59 | + } |
| 60 | + |
| 61 | + context.report({ |
| 62 | + node: defineRef, |
| 63 | + messageId: 'unused', |
| 64 | + data: { |
| 65 | + name: defineRef.value |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Extract the use ref names for ObjectPattern. |
| 73 | + * @param {ObjectPattern} node |
| 74 | + * @returns {void} |
| 75 | + */ |
| 76 | + function extractUsedForObjectPattern(node) { |
| 77 | + for (const prop of node.properties) { |
| 78 | + if (prop.type === 'Property') { |
| 79 | + const name = utils.getStaticPropertyName(prop) |
| 80 | + if (name) { |
| 81 | + usedRefs.add(name) |
| 82 | + } else { |
| 83 | + hasUnknown = true |
| 84 | + return |
| 85 | + } |
| 86 | + } else { |
| 87 | + hasUnknown = true |
| 88 | + return |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + /** |
| 93 | + * Extract the use ref names. |
| 94 | + * @param {Identifier | MemberExpression} refsNode |
| 95 | + * @returns {void} |
| 96 | + */ |
| 97 | + function extractUsedForPattern(refsNode) { |
| 98 | + /** @type {Identifier | MemberExpression | ChainExpression} */ |
| 99 | + let node = refsNode |
| 100 | + while (node.parent.type === 'ChainExpression') { |
| 101 | + node = node.parent |
| 102 | + } |
| 103 | + const parent = node.parent |
| 104 | + if (parent.type === 'AssignmentExpression') { |
| 105 | + if (parent.right === node) { |
| 106 | + if (parent.left.type === 'ObjectPattern') { |
| 107 | + // `({foo} = $refs)` |
| 108 | + extractUsedForObjectPattern(parent.left) |
| 109 | + } else if (parent.left.type === 'Identifier') { |
| 110 | + // `foo = $refs` |
| 111 | + hasUnknown = true |
| 112 | + } |
| 113 | + } |
| 114 | + } else if (parent.type === 'VariableDeclarator') { |
| 115 | + if (parent.init === node) { |
| 116 | + if (parent.id.type === 'ObjectPattern') { |
| 117 | + // `const {foo} = $refs` |
| 118 | + extractUsedForObjectPattern(parent.id) |
| 119 | + } else if (parent.id.type === 'Identifier') { |
| 120 | + // `const foo = $refs` |
| 121 | + hasUnknown = true |
| 122 | + } |
| 123 | + } |
| 124 | + } else if (parent.type === 'MemberExpression') { |
| 125 | + if (parent.object === node) { |
| 126 | + // `$refs.foo` |
| 127 | + const name = utils.getStaticPropertyName(parent) |
| 128 | + if (name) { |
| 129 | + usedRefs.add(name) |
| 130 | + } else { |
| 131 | + hasUnknown = true |
| 132 | + } |
| 133 | + } |
| 134 | + } else if (parent.type === 'CallExpression') { |
| 135 | + const argIndex = parent.arguments.indexOf(node) |
| 136 | + if (argIndex > -1) { |
| 137 | + // `foo($refs)` |
| 138 | + hasUnknown = true |
| 139 | + } |
| 140 | + } else if ( |
| 141 | + parent.type === 'ForInStatement' || |
| 142 | + parent.type === 'ReturnStatement' |
| 143 | + ) { |
| 144 | + hasUnknown = true |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return utils.defineTemplateBodyVisitor( |
| 149 | + context, |
| 150 | + { |
| 151 | + /** |
| 152 | + * @param {VExpressionContainer} node |
| 153 | + */ |
| 154 | + VExpressionContainer(node) { |
| 155 | + if (hasUnknown) { |
| 156 | + return |
| 157 | + } |
| 158 | + for (const id of getReferences(node.references)) { |
| 159 | + if (id.name !== '$refs') { |
| 160 | + continue |
| 161 | + } |
| 162 | + extractUsedForPattern(id) |
| 163 | + } |
| 164 | + }, |
| 165 | + /** |
| 166 | + * @param {VAttribute} node |
| 167 | + */ |
| 168 | + 'VAttribute[directive=false]'(node) { |
| 169 | + if (hasUnknown) { |
| 170 | + return |
| 171 | + } |
| 172 | + if (node.key.name === 'ref' && node.value != null) { |
| 173 | + defineRefs.push(node.value) |
| 174 | + } |
| 175 | + }, |
| 176 | + "VElement[parent.type!='VElement']:exit"() { |
| 177 | + if (hasUnknown) { |
| 178 | + return |
| 179 | + } |
| 180 | + reportUnusedRefs() |
| 181 | + } |
| 182 | + }, |
| 183 | + { |
| 184 | + Identifier(id) { |
| 185 | + if (hasUnknown) { |
| 186 | + return |
| 187 | + } |
| 188 | + if (id.name !== '$refs') { |
| 189 | + return |
| 190 | + } |
| 191 | + /** @type {Identifier | MemberExpression} */ |
| 192 | + let refsNode = id |
| 193 | + if (id.parent.type === 'MemberExpression') { |
| 194 | + if (id.parent.property === id) { |
| 195 | + // `this.$refs.foo` |
| 196 | + refsNode = id.parent |
| 197 | + } |
| 198 | + } |
| 199 | + extractUsedForPattern(refsNode) |
| 200 | + } |
| 201 | + } |
| 202 | + ) |
| 203 | + } |
| 204 | +} |
0 commit comments