|
| 1 | +/** |
| 2 | + * @fileoverview Prevent declaring unused methods and properties of component class |
| 3 | + * @author Paweł Nowak, Berton Zhu |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +const Components = require('../util/Components'); |
| 9 | +const docsUrl = require('../util/docsUrl'); |
| 10 | + |
| 11 | +// ------------------------------------------------------------------------------ |
| 12 | +// Rule Definition |
| 13 | +// ------------------------------------------------------------------------------ |
| 14 | + |
| 15 | +const LIFECYCLE_METHODS = new Set([ |
| 16 | + 'constructor', |
| 17 | + 'componentDidCatch', |
| 18 | + 'componentDidMount', |
| 19 | + 'componentDidUpdate', |
| 20 | + 'componentWillMount', |
| 21 | + 'componentWillReceiveProps', |
| 22 | + 'componentWillUnmount', |
| 23 | + 'componentWillUpdate', |
| 24 | + 'getSnapshotBeforeUpdate', |
| 25 | + 'render', |
| 26 | + 'shouldComponentUpdate', |
| 27 | + 'UNSAFE_componentWillMount', |
| 28 | + 'UNSAFE_componentWillReceiveProps', |
| 29 | + 'UNSAFE_componentWillUpdate' |
| 30 | +]); |
| 31 | + |
| 32 | +const ES6_LIFECYCLE = new Set([ |
| 33 | + 'state' |
| 34 | +]); |
| 35 | + |
| 36 | +const ES5_LIFECYCLE = new Set([ |
| 37 | + 'getInitialState', |
| 38 | + 'getDefaultProps', |
| 39 | + 'mixins' |
| 40 | +]); |
| 41 | + |
| 42 | +function isKeyLiteralLike(node, property) { |
| 43 | + return property.type === 'Literal' |
| 44 | + || (property.type === 'TemplateLiteral' && property.expressions.length === 0) |
| 45 | + || (node.computed === false && property.type === 'Identifier'); |
| 46 | +} |
| 47 | + |
| 48 | +// Descend through all wrapping TypeCastExpressions and return the expression |
| 49 | +// that was cast. |
| 50 | +function uncast(node) { |
| 51 | + while (node.type === 'TypeCastExpression') { |
| 52 | + node = node.expression; |
| 53 | + } |
| 54 | + return node; |
| 55 | +} |
| 56 | + |
| 57 | +// Return the name of an identifier or the string value of a literal. Useful |
| 58 | +// anywhere that a literal may be used as a key (e.g., member expressions, |
| 59 | +// method definitions, ObjectExpression property keys). |
| 60 | +function getName(node) { |
| 61 | + node = uncast(node); |
| 62 | + const type = node.type; |
| 63 | + |
| 64 | + if (type === 'Identifier') { |
| 65 | + return node.name; |
| 66 | + } |
| 67 | + if (type === 'Literal') { |
| 68 | + return String(node.value); |
| 69 | + } |
| 70 | + if (type === 'TemplateLiteral' && node.expressions.length === 0) { |
| 71 | + return node.quasis[0].value.raw; |
| 72 | + } |
| 73 | + return null; |
| 74 | +} |
| 75 | + |
| 76 | +function isThisExpression(node) { |
| 77 | + return uncast(node).type === 'ThisExpression'; |
| 78 | +} |
| 79 | + |
| 80 | +function getInitialClassInfo(node, isClass) { |
| 81 | + return { |
| 82 | + classNode: node, |
| 83 | + isClass, |
| 84 | + // Set of nodes where properties were defined. |
| 85 | + properties: new Set(), |
| 86 | + |
| 87 | + // Set of names of properties that we've seen used. |
| 88 | + usedProperties: new Set(), |
| 89 | + |
| 90 | + inStatic: false |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +module.exports = { |
| 95 | + meta: { |
| 96 | + docs: { |
| 97 | + description: 'Prevent declaring unused methods of component class', |
| 98 | + category: 'Best Practices', |
| 99 | + recommended: false, |
| 100 | + url: docsUrl('no-unused-class-component-methods') |
| 101 | + }, |
| 102 | + schema: [ |
| 103 | + { |
| 104 | + type: 'object', |
| 105 | + additionalProperties: false |
| 106 | + } |
| 107 | + ] |
| 108 | + }, |
| 109 | + |
| 110 | + create: Components.detect((context, components, utils) => { |
| 111 | + let classInfo = null; |
| 112 | + |
| 113 | + // Takes an ObjectExpression node and adds all named Property nodes to the |
| 114 | + // current set of properties. |
| 115 | + function addProperty(node) { |
| 116 | + classInfo.properties.add(node); |
| 117 | + } |
| 118 | + |
| 119 | + // Adds the name of the given node as a used property if the node is an |
| 120 | + // Identifier or a Literal. Other node types are ignored. |
| 121 | + function addUsedProperty(node) { |
| 122 | + const name = getName(node); |
| 123 | + if (name) { |
| 124 | + classInfo.usedProperties.add(name); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + function reportUnusedProperties() { |
| 129 | + // Report all unused properties. |
| 130 | + for (const node of classInfo.properties) { // eslint-disable-line no-restricted-syntax |
| 131 | + const name = getName(node); |
| 132 | + if ( |
| 133 | + !classInfo.usedProperties.has(name) |
| 134 | + && !LIFECYCLE_METHODS.has(name) |
| 135 | + && (classInfo.isClass ? !ES6_LIFECYCLE.has(name) : !ES5_LIFECYCLE.has(name)) |
| 136 | + ) { |
| 137 | + const className = (classInfo.classNode.id && classInfo.classNode.id.name) || ''; |
| 138 | + |
| 139 | + context.report({ |
| 140 | + node, |
| 141 | + message: `Unused method or property "${name}"${className ? ` of class "${className}"` : ''}` |
| 142 | + }); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + function exitMethod() { |
| 148 | + if (!classInfo || !classInfo.inStatic) { |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + classInfo.inStatic = false; |
| 153 | + } |
| 154 | + |
| 155 | + return { |
| 156 | + ClassDeclaration(node) { |
| 157 | + if (utils.isES6Component(node)) { |
| 158 | + classInfo = getInitialClassInfo(node, true); |
| 159 | + } |
| 160 | + }, |
| 161 | + |
| 162 | + ObjectExpression(node) { |
| 163 | + if (utils.isES5Component(node)) { |
| 164 | + classInfo = getInitialClassInfo(node, false); |
| 165 | + } |
| 166 | + }, |
| 167 | + |
| 168 | + 'ClassDeclaration:exit'() { |
| 169 | + if (!classInfo) { |
| 170 | + return; |
| 171 | + } |
| 172 | + reportUnusedProperties(); |
| 173 | + classInfo = null; |
| 174 | + }, |
| 175 | + |
| 176 | + 'ObjectExpression:exit'(node) { |
| 177 | + if (!classInfo || classInfo.classNode !== node) { |
| 178 | + return; |
| 179 | + } |
| 180 | + reportUnusedProperties(); |
| 181 | + classInfo = null; |
| 182 | + }, |
| 183 | + |
| 184 | + Property(node) { |
| 185 | + if (!classInfo || classInfo.classNode !== node.parent) { |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + if (isKeyLiteralLike(node, node.key)) { |
| 190 | + addProperty(node.key); |
| 191 | + } |
| 192 | + }, |
| 193 | + |
| 194 | + 'ClassProperty, MethodDefinition'(node) { |
| 195 | + if (!classInfo) { |
| 196 | + return; |
| 197 | + } |
| 198 | + |
| 199 | + if (node.static) { |
| 200 | + classInfo.inStatic = true; |
| 201 | + return; |
| 202 | + } |
| 203 | + |
| 204 | + if (isKeyLiteralLike(node, node.key)) { |
| 205 | + addProperty(node.key); |
| 206 | + } |
| 207 | + }, |
| 208 | + |
| 209 | + 'ClassProperty:exit': exitMethod, |
| 210 | + 'MethodDefinition:exit': exitMethod, |
| 211 | + |
| 212 | + MemberExpression(node) { |
| 213 | + if (!classInfo || classInfo.inStatic) { |
| 214 | + return; |
| 215 | + } |
| 216 | + |
| 217 | + if (isThisExpression(node.object) && isKeyLiteralLike(node, node.property)) { |
| 218 | + if (node.parent.type === 'AssignmentExpression' && node.parent.left === node) { |
| 219 | + // detect `this.property = xxx` |
| 220 | + addProperty(node.property); |
| 221 | + } else { |
| 222 | + // detect `this.property()`, `x = this.property`, etc. |
| 223 | + addUsedProperty(node.property); |
| 224 | + } |
| 225 | + } |
| 226 | + }, |
| 227 | + |
| 228 | + VariableDeclarator(node) { |
| 229 | + if (!classInfo || classInfo.inStatic) { |
| 230 | + return; |
| 231 | + } |
| 232 | + |
| 233 | + // detect `{ foo, bar: baz } = this` |
| 234 | + if (node.init && isThisExpression(node.init) && node.id.type === 'ObjectPattern') { |
| 235 | + node.id.properties.forEach((prop) => { |
| 236 | + if (prop.type === 'Property' && isKeyLiteralLike(prop, prop.key)) { |
| 237 | + addUsedProperty(prop.key); |
| 238 | + } |
| 239 | + }); |
| 240 | + } |
| 241 | + } |
| 242 | + }; |
| 243 | + }) |
| 244 | +}; |
0 commit comments