Legend:
- Unmodified
- Added
- Removed
-
imaps-frontend/node_modules/eslint-plugin-react/lib/rules/no-unused-state.js
rd565449 r0c6b92a 11 11 12 12 const docsUrl = require('../util/docsUrl'); 13 const ast = require('../util/ast');13 const astUtil = require('../util/ast'); 14 14 const componentUtil = require('../util/componentUtil'); 15 15 const report = require('../util/report'); … … 45 45 46 46 function isThisExpression(node) { 47 return ast .unwrapTSAsExpression(uncast(node)).type === 'ThisExpression';47 return astUtil.unwrapTSAsExpression(uncast(node)).type === 'ThisExpression'; 48 48 } 49 49 … … 66 66 67 67 function isSetStateCall(node) { 68 const unwrappedCalleeNode = ast .unwrapTSAsExpression(node.callee);68 const unwrappedCalleeNode = astUtil.unwrapTSAsExpression(node.callee); 69 69 70 70 return ( … … 79 79 }; 80 80 81 /** @type {import('eslint').Rule.RuleModule} */ 81 82 module.exports = { 82 83 meta: { … … 175 176 // destructures `this.state`. 176 177 function handleStateDestructuring(node) { 177 for (const prop of node.properties){178 node.properties.forEach((prop) => { 178 179 if (prop.type === 'Property') { 179 180 addUsedStateField(prop.key); … … 184 185 classInfo.aliases.add(getName(prop.argument)); 185 186 } 186 } 187 }); 187 188 } 188 189 … … 190 191 // AssignmentExpressions and VariableDeclarators. 191 192 function handleAssignment(left, right) { 192 const unwrappedRight = ast .unwrapTSAsExpression(right);193 const unwrappedRight = astUtil.unwrapTSAsExpression(right); 193 194 194 195 switch (left.type) { … … 202 203 handleStateDestructuring(left); 203 204 } else if (isThisExpression(unwrappedRight) && classInfo.aliases) { 204 for (const prop of left.properties){205 left.properties.forEach((prop) => { 205 206 if (prop.type === 'Property' && getName(prop.key) === 'state') { 206 207 const name = getName(prop.value); … … 211 212 } 212 213 } 213 } 214 }); 214 215 } 215 216 break; … … 221 222 function reportUnusedFields() { 222 223 // Report all unused state fields. 223 for (const node of classInfo.stateFields){224 classInfo.stateFields.forEach((node) => { 224 225 const name = getName(node.key); 225 226 if (!classInfo.usedStateFields.has(name)) { … … 231 232 }); 232 233 } 233 } 234 }); 234 235 } 235 236 … … 293 294 } 294 295 295 const unwrappedNode = ast .unwrapTSAsExpression(node);296 const unwrappedArgumentNode = ast .unwrapTSAsExpression(unwrappedNode.arguments[0]);296 const unwrappedNode = astUtil.unwrapTSAsExpression(node); 297 const unwrappedArgumentNode = astUtil.unwrapTSAsExpression(unwrappedNode.arguments[0]); 297 298 298 299 // If we're looking at a `this.setState({})` invocation, record all the … … 309 310 && unwrappedArgumentNode.type === 'ArrowFunctionExpression' 310 311 ) { 311 const unwrappedBodyNode = ast .unwrapTSAsExpression(unwrappedArgumentNode.body);312 const unwrappedBodyNode = astUtil.unwrapTSAsExpression(unwrappedArgumentNode.body); 312 313 313 314 if (unwrappedBodyNode.type === 'ObjectExpression') { … … 331 332 // If we see state being assigned as a class property using an object 332 333 // expression, record all the fields of that object as state fields. 333 const unwrappedValueNode = ast .unwrapTSAsExpression(node.value);334 const unwrappedValueNode = astUtil.unwrapTSAsExpression(node.value); 334 335 335 336 const name = getName(node.key); … … 432 433 } 433 434 434 if (parent.key.name === 'getInitialState') { 435 if ( 436 'key' in parent 437 && 'name' in parent.key 438 && parent.key.name === 'getInitialState' 439 ) { 435 440 const body = node.body.body; 436 441 const lastBodyNode = body[body.length - 1]; … … 453 458 } 454 459 455 const unwrappedLeft = ast .unwrapTSAsExpression(node.left);456 const unwrappedRight = ast .unwrapTSAsExpression(node.right);460 const unwrappedLeft = astUtil.unwrapTSAsExpression(node.left); 461 const unwrappedRight = astUtil.unwrapTSAsExpression(node.right); 457 462 458 463 // Check for assignments like `this.state = {}` … … 464 469 ) { 465 470 // Find the nearest function expression containing this assignment. 471 /** @type {import("eslint").Rule.Node} */ 466 472 let fn = node; 467 473 while (fn.type !== 'FunctionExpression' && fn.parent) { … … 494 500 return; 495 501 } 496 if (isStateReference(ast .unwrapTSAsExpression(node.object))) {502 if (isStateReference(astUtil.unwrapTSAsExpression(node.object))) { 497 503 // If we see this.state[foo] access, give up. 498 504 if (node.computed && node.property.type !== 'Literal') { … … 503 509 addUsedStateField(node.property); 504 510 // If we see a `this.state` access in a CallExpression, give up. 505 } else if (isStateReference(node) && node.parent.type === 'CallExpression') {511 } else if (isStateReference(node) && astUtil.isCallExpression(node.parent)) { 506 512 classInfo = null; 507 513 }
Note:
See TracChangeset
for help on using the changeset viewer.