|
| 1 | +/** |
| 2 | + * @fileoverview Enforce consistent usage of destructuring assignment of props, state, and context. |
| 3 | + **/ |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +const Components = require('../util/Components'); |
| 7 | + |
| 8 | +const DEFAULT_OPTION = 'always'; |
| 9 | + |
| 10 | +module.exports = { |
| 11 | + meta: { |
| 12 | + docs: { |
| 13 | + description: 'Enforce consistent usage of destructuring assignment of props, state, and context', |
| 14 | + category: 'Stylistic Issues', |
| 15 | + recommended: false |
| 16 | + }, |
| 17 | + schema: [{ |
| 18 | + type: 'string', |
| 19 | + enum: [ |
| 20 | + 'always', |
| 21 | + 'never' |
| 22 | + ] |
| 23 | + }] |
| 24 | + }, |
| 25 | + |
| 26 | + create: Components.detect((context, components, utils) => { |
| 27 | + const configuration = context.options[0] || DEFAULT_OPTION; |
| 28 | + |
| 29 | + |
| 30 | + /** |
| 31 | + * Checks if a prop is being assigned a value props.bar = 'bar' |
| 32 | + * @param {ASTNode} node The AST node being checked. |
| 33 | + * @returns {Boolean} |
| 34 | + */ |
| 35 | + |
| 36 | + function isAssignmentToProp(node) { |
| 37 | + return ( |
| 38 | + node.parent && |
| 39 | + node.parent.type === 'AssignmentExpression' && |
| 40 | + node.parent.left === node |
| 41 | + ); |
| 42 | + } |
| 43 | + /** |
| 44 | + * @param {ASTNode} node We expect either an ArrowFunctionExpression, |
| 45 | + * FunctionDeclaration, or FunctionExpression |
| 46 | + */ |
| 47 | + function handleStatelessComponent(node) { |
| 48 | + const destructuringProps = node.params && node.params[0] && node.params[0].type === 'ObjectPattern'; |
| 49 | + const destructuringContext = node.params && node.params[1] && node.params[1].type === 'ObjectPattern'; |
| 50 | + |
| 51 | + if (destructuringProps && components.get(node) && configuration === 'never') { |
| 52 | + context.report({ |
| 53 | + node: node, |
| 54 | + message: 'Must never use destructuring props assignment in SFC argument' |
| 55 | + }); |
| 56 | + } else if (destructuringContext && components.get(node) && configuration === 'never') { |
| 57 | + context.report({ |
| 58 | + node: node, |
| 59 | + message: 'Must never use destructuring context assignment in SFC argument' |
| 60 | + }); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + function handleSFCUsage(node) { |
| 65 | + // props.aProp || context.aProp |
| 66 | + const isPropUsed = (node.object.name === 'props' || node.object.name === 'context') && !isAssignmentToProp(node); |
| 67 | + if (isPropUsed && configuration === 'always') { |
| 68 | + context.report({ |
| 69 | + node: node, |
| 70 | + message: `Must use destructuring ${node.object.name} assignment` |
| 71 | + }); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + function handleClassUsage(node) { |
| 76 | + // this.props.Aprop || this.context.aProp || this.state.aState |
| 77 | + const isPropUsed = (node.object.type === 'MemberExpression' && node.object.object.type === 'ThisExpression' && |
| 78 | + (node.object.property.name === 'props' || node.object.property.name === 'context' || node.object.property.name === 'state') && |
| 79 | + node.object.type === 'MemberExpression' |
| 80 | + ); |
| 81 | + |
| 82 | + if (isPropUsed && configuration === 'always') { |
| 83 | + context.report({ |
| 84 | + node: node, |
| 85 | + message: `Must use destructuring ${node.object.property.name} assignment` |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return { |
| 91 | + |
| 92 | + FunctionDeclaration: handleStatelessComponent, |
| 93 | + |
| 94 | + ArrowFunctionExpression: handleStatelessComponent, |
| 95 | + |
| 96 | + FunctionExpression: handleStatelessComponent, |
| 97 | + |
| 98 | + MemberExpression: function(node) { |
| 99 | + const SFCComponent = components.get(context.getScope(node).block); |
| 100 | + const classComponent = utils.getParentComponent(node); |
| 101 | + if (SFCComponent) { |
| 102 | + handleSFCUsage(node); |
| 103 | + } |
| 104 | + if (classComponent) { |
| 105 | + handleClassUsage(node, classComponent); |
| 106 | + } |
| 107 | + }, |
| 108 | + |
| 109 | + VariableDeclarator: function(node) { |
| 110 | + const classComponent = utils.getParentComponent(node); |
| 111 | + const SFCComponent = components.get(context.getScope(node).block); |
| 112 | + |
| 113 | + const destructuring = (node.init && node.id && node.id.type === 'ObjectPattern'); |
| 114 | + // let {foo} = props; |
| 115 | + const destructuringSFC = destructuring && (node.init.name === 'props' || node.init.name === 'context'); |
| 116 | + // let {foo} = this.props; |
| 117 | + const destructuringClass = destructuring && node.init.object && node.init.object.type === 'ThisExpression' && ( |
| 118 | + node.init.property.name === 'props' || node.init.property.name === 'context' || node.init.property.name === 'state' |
| 119 | + ); |
| 120 | + |
| 121 | + if (SFCComponent && destructuringSFC && configuration === 'never') { |
| 122 | + context.report({ |
| 123 | + node: node, |
| 124 | + message: `Must never use destructuring ${node.init.name} assignment` |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + if (classComponent && destructuringClass && configuration === 'never') { |
| 129 | + context.report({ |
| 130 | + node: node, |
| 131 | + message: `Must never use destructuring ${node.init.property.name} assignment` |
| 132 | + }); |
| 133 | + } |
| 134 | + } |
| 135 | + }; |
| 136 | + }) |
| 137 | +}; |
0 commit comments