|
| 1 | +const MESSAGE_ID_ERROR = 'no-accessor-recursion/error'; |
| 2 | +const messages = { |
| 3 | + [MESSAGE_ID_ERROR]: 'Disallow recursive access to `this` within {{kind}}ters.', |
| 4 | +}; |
| 5 | + |
| 6 | +/** |
| 7 | +Get the closest non-arrow function scope. |
| 8 | +
|
| 9 | +@param {import('eslint').SourceCode} sourceCode |
| 10 | +@param {import('estree').Node} node |
| 11 | +@return {import('eslint').Scope.Scope | undefined} |
| 12 | +*/ |
| 13 | +const getClosestFunctionScope = (sourceCode, node) => { |
| 14 | + for (let scope = sourceCode.getScope(node); scope; scope = scope.upper) { |
| 15 | + if (scope.type === 'class') { |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + if (scope.type === 'function' && scope.block.type !== 'ArrowFunctionExpression') { |
| 20 | + return scope; |
| 21 | + } |
| 22 | + } |
| 23 | +}; |
| 24 | + |
| 25 | +/** @param {import('estree').Identifier | import('estree').PrivateIdentifier} node */ |
| 26 | +const isIdentifier = node => node.type === 'Identifier' || node.type === 'PrivateIdentifier'; |
| 27 | + |
| 28 | +/** @param {import('estree').ThisExpression} node */ |
| 29 | +const isDotNotationAccess = node => |
| 30 | + node.parent.type === 'MemberExpression' |
| 31 | + && node.parent.object === node |
| 32 | + && !node.parent.computed |
| 33 | + && isIdentifier(node.parent.property); |
| 34 | + |
| 35 | +/** |
| 36 | +Check if a property is a valid getter or setter. |
| 37 | +
|
| 38 | +@param {import('estree').Property | import('estree').MethodDefinition} property |
| 39 | +*/ |
| 40 | +const isValidProperty = property => |
| 41 | + ['Property', 'MethodDefinition'].includes(property.type) |
| 42 | + && !property.computed |
| 43 | + && ['set', 'get'].includes(property.kind) |
| 44 | + && isIdentifier(property.key); |
| 45 | + |
| 46 | +/** |
| 47 | +Check if two property keys are the same. |
| 48 | +
|
| 49 | +@param {import('estree').Property['key']} keyLeft |
| 50 | +@param {import('estree').Property['key']} keyRight |
| 51 | +*/ |
| 52 | +const isSameKey = (keyLeft, keyRight) => ['type', 'name'].every(key => keyLeft[key] === keyRight[key]); |
| 53 | + |
| 54 | +/** |
| 55 | +Check if `this` is accessed recursively within a getter or setter. |
| 56 | +
|
| 57 | +@param {import('estree').ThisExpression} node |
| 58 | +@param {import('estree').Property | import('estree').MethodDefinition} property |
| 59 | +*/ |
| 60 | +const isMemberAccess = (node, property) => |
| 61 | + isDotNotationAccess(node) |
| 62 | + && isSameKey(node.parent.property, property.key); |
| 63 | + |
| 64 | +/** |
| 65 | +Check if `this` is accessed recursively within a destructuring assignment. |
| 66 | +
|
| 67 | +@param {import('estree').ThisExpression} node |
| 68 | +@param {import('estree').Property | import('estree').MethodDefinition} property |
| 69 | +*/ |
| 70 | +const isRecursiveDestructuringAccess = (node, property) => |
| 71 | + node.parent.type === 'VariableDeclarator' |
| 72 | + && node.parent.init === node |
| 73 | + && node.parent.id.type === 'ObjectPattern' |
| 74 | + && node.parent.id.properties.some(declaratorProperty => |
| 75 | + declaratorProperty.type === 'Property' |
| 76 | + && !declaratorProperty.computed |
| 77 | + && isSameKey(declaratorProperty.key, property.key), |
| 78 | + ); |
| 79 | + |
| 80 | +const isPropertyRead = (thisExpression, property) => |
| 81 | + isMemberAccess(thisExpression, property) |
| 82 | + || isRecursiveDestructuringAccess(thisExpression, property); |
| 83 | + |
| 84 | +const isPropertyWrite = (thisExpression, property) => { |
| 85 | + if (!isMemberAccess(thisExpression, property)) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + const memberExpression = thisExpression.parent; |
| 90 | + const {parent} = memberExpression; |
| 91 | + |
| 92 | + // This part is similar to `isLeftHandSide`, try to DRY in future |
| 93 | + return ( |
| 94 | + // `this.foo = …` |
| 95 | + // `[this.foo = …] = …` |
| 96 | + // `({property: this.foo = …] = …)` |
| 97 | + ( |
| 98 | + (parent.type === 'AssignmentExpression' || parent.type === 'AssignmentPattern') |
| 99 | + && parent.left === memberExpression |
| 100 | + ) |
| 101 | + // `++ this.foo` |
| 102 | + || (parent.type === 'UpdateExpression' && parent.argument === memberExpression) |
| 103 | + // `[this.foo] = …` |
| 104 | + || (parent.type === 'ArrayPattern' && parent.elements.includes(memberExpression)) |
| 105 | + // `({property: this.foo} = …)` |
| 106 | + || ( |
| 107 | + parent.type === 'Property' |
| 108 | + && parent.value === memberExpression |
| 109 | + && parent.parent.type === 'ObjectPattern' |
| 110 | + && parent.parent.properties.includes(memberExpression.parent) |
| 111 | + ) |
| 112 | + ); |
| 113 | +}; |
| 114 | + |
| 115 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 116 | +const create = context => { |
| 117 | + const {sourceCode} = context; |
| 118 | + |
| 119 | + return { |
| 120 | + /** @param {import('estree').ThisExpression} thisExpression */ |
| 121 | + ThisExpression(thisExpression) { |
| 122 | + const scope = getClosestFunctionScope(sourceCode, thisExpression); |
| 123 | + |
| 124 | + if (!scope) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + /** @type {import('estree').Property | import('estree').MethodDefinition} */ |
| 129 | + const property = scope.block.parent; |
| 130 | + |
| 131 | + if (!isValidProperty(property)) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + if (property.kind === 'get' && isPropertyRead(thisExpression, property)) { |
| 136 | + return {node: thisExpression.parent, messageId: MESSAGE_ID_ERROR, data: {kind: property.kind}}; |
| 137 | + } |
| 138 | + |
| 139 | + if (property.kind === 'set' && isPropertyWrite(thisExpression, property)) { |
| 140 | + return {node: thisExpression.parent, messageId: MESSAGE_ID_ERROR, data: {kind: property.kind}}; |
| 141 | + } |
| 142 | + }, |
| 143 | + }; |
| 144 | +}; |
| 145 | + |
| 146 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 147 | +const config = { |
| 148 | + create, |
| 149 | + meta: { |
| 150 | + type: 'problem', |
| 151 | + docs: { |
| 152 | + description: 'Disallow recursive access to `this` within getters and setters.', |
| 153 | + recommended: true, |
| 154 | + }, |
| 155 | + defaultOptions: [], |
| 156 | + messages, |
| 157 | + }, |
| 158 | +}; |
| 159 | + |
| 160 | +export default config; |
0 commit comments