|
| 1 | +/** |
| 2 | + * @author Yosuke Ota <https://github.com/ota-meshi> |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const utils = require('../utils') |
| 12 | +const { |
| 13 | + extractRefObjectReferences, |
| 14 | + extractReactiveVariableReferences |
| 15 | +} = require('../utils/ref-object-references') |
| 16 | + |
| 17 | +// ------------------------------------------------------------------------------ |
| 18 | +// Helpers |
| 19 | +// ------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +/** |
| 22 | + * @typedef {import('../utils/ref-object-references').RefObjectReferences} RefObjectReferences |
| 23 | + * @typedef {import('../utils/ref-object-references').RefObjectReference} RefObjectReference |
| 24 | + */ |
| 25 | + |
| 26 | +/** |
| 27 | + * Checks whether writing assigns a value to the given pattern. |
| 28 | + * @param {Pattern | AssignmentProperty | Property} node |
| 29 | + * @returns {boolean} |
| 30 | + */ |
| 31 | +function isUpdate(node) { |
| 32 | + const parent = node.parent |
| 33 | + if (parent.type === 'UpdateExpression' && parent.argument === node) { |
| 34 | + // e.g. `pattern++` |
| 35 | + return true |
| 36 | + } |
| 37 | + if (parent.type === 'AssignmentExpression' && parent.left === node) { |
| 38 | + // e.g. `pattern = 42` |
| 39 | + return true |
| 40 | + } |
| 41 | + if ( |
| 42 | + (parent.type === 'Property' && parent.value === node) || |
| 43 | + parent.type === 'ArrayPattern' || |
| 44 | + (parent.type === 'ObjectPattern' && |
| 45 | + parent.properties.includes(/** @type {any} */ (node))) || |
| 46 | + (parent.type === 'AssignmentPattern' && parent.left === node) || |
| 47 | + parent.type === 'RestElement' || |
| 48 | + (parent.type === 'MemberExpression' && parent.object === node) |
| 49 | + ) { |
| 50 | + return isUpdate(parent) |
| 51 | + } |
| 52 | + return false |
| 53 | +} |
| 54 | + |
| 55 | +// ------------------------------------------------------------------------------ |
| 56 | +// Rule Definition |
| 57 | +// ------------------------------------------------------------------------------ |
| 58 | + |
| 59 | +module.exports = { |
| 60 | + meta: { |
| 61 | + type: 'problem', |
| 62 | + docs: { |
| 63 | + description: |
| 64 | + 'disallow destructuring of ref objects that can lead to loss of reactivity', |
| 65 | + categories: undefined, |
| 66 | + url: 'https://eslint.vuejs.org/rules/no-ref-object-destructure.html' |
| 67 | + }, |
| 68 | + fixable: null, |
| 69 | + schema: [], |
| 70 | + messages: { |
| 71 | + getValueInSameScope: |
| 72 | + 'Getting a value from the ref object in the same scope will cause the value to lose reactivity.', |
| 73 | + getReactiveVariableInSameScope: |
| 74 | + 'Getting a reactive variable in the same scope will cause the value to lose reactivity.' |
| 75 | + } |
| 76 | + }, |
| 77 | + /** |
| 78 | + * @param {RuleContext} context |
| 79 | + * @returns {RuleListener} |
| 80 | + */ |
| 81 | + create(context) { |
| 82 | + /** |
| 83 | + * @typedef {object} ScopeStack |
| 84 | + * @property {ScopeStack | null} upper |
| 85 | + * @property {Program | FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node |
| 86 | + */ |
| 87 | + /** @type {ScopeStack} */ |
| 88 | + let scopeStack = { upper: null, node: context.getSourceCode().ast } |
| 89 | + /** @type {Map<CallExpression, ScopeStack>} */ |
| 90 | + const scopes = new Map() |
| 91 | + |
| 92 | + const refObjectReferences = extractRefObjectReferences(context) |
| 93 | + const reactiveVariableReferences = |
| 94 | + extractReactiveVariableReferences(context) |
| 95 | + |
| 96 | + /** |
| 97 | + * Verify the given ref object value. `refObj = ref(); refObj.value;` |
| 98 | + * @param {Expression | Super | ObjectPattern} node |
| 99 | + */ |
| 100 | + function verifyRefObjectValue(node) { |
| 101 | + const ref = refObjectReferences.get(node) |
| 102 | + if (!ref) { |
| 103 | + return |
| 104 | + } |
| 105 | + if (scopes.get(ref.define) !== scopeStack) { |
| 106 | + // Not in the same scope |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + context.report({ |
| 111 | + node, |
| 112 | + messageId: 'getValueInSameScope' |
| 113 | + }) |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Verify the given reactive variable. `refVal = $ref(); refVal;` |
| 118 | + * @param {Identifier} node |
| 119 | + */ |
| 120 | + function verifyReactiveVariable(node) { |
| 121 | + const ref = reactiveVariableReferences.get(node) |
| 122 | + if (!ref || ref.escape) { |
| 123 | + return |
| 124 | + } |
| 125 | + if (scopes.get(ref.define) !== scopeStack) { |
| 126 | + // Not in the same scope |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + context.report({ |
| 131 | + node, |
| 132 | + messageId: 'getReactiveVariableInSameScope' |
| 133 | + }) |
| 134 | + } |
| 135 | + |
| 136 | + return { |
| 137 | + ':function'(node) { |
| 138 | + scopeStack = { upper: scopeStack, node } |
| 139 | + }, |
| 140 | + ':function:exit'() { |
| 141 | + scopeStack = scopeStack.upper || scopeStack |
| 142 | + }, |
| 143 | + CallExpression(node) { |
| 144 | + scopes.set(node, scopeStack) |
| 145 | + }, |
| 146 | + /** |
| 147 | + * Check for `refObj.value`. |
| 148 | + */ |
| 149 | + 'MemberExpression:exit'(node) { |
| 150 | + if (isUpdate(node)) { |
| 151 | + // e.g. `refObj.value = 42`, `refObj.value++` |
| 152 | + return |
| 153 | + } |
| 154 | + const name = utils.getStaticPropertyName(node) |
| 155 | + if (name !== 'value') { |
| 156 | + return |
| 157 | + } |
| 158 | + verifyRefObjectValue(node.object) |
| 159 | + }, |
| 160 | + /** |
| 161 | + * Check for `{value} = refObj`. |
| 162 | + */ |
| 163 | + 'ObjectPattern:exit'(node) { |
| 164 | + const prop = utils.findAssignmentProperty(node, 'value') |
| 165 | + if (!prop) { |
| 166 | + return |
| 167 | + } |
| 168 | + verifyRefObjectValue(node) |
| 169 | + }, |
| 170 | + /** |
| 171 | + * Check for reactive variable`. |
| 172 | + * @param {Identifier} node |
| 173 | + */ |
| 174 | + 'Identifier:exit'(node) { |
| 175 | + if (isUpdate(node)) { |
| 176 | + // e.g. `reactiveVariable = 42`, `reactiveVariable++` |
| 177 | + return |
| 178 | + } |
| 179 | + verifyReactiveVariable(node) |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments