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