@@ -8,6 +8,7 @@ const utils = require('../utils')
8
8
9
9
/**
10
10
* @typedef {import('../utils').VueObjectData } VueObjectData
11
+ * @typedef {import('../utils').VueVisitor } VueVisitor
11
12
* @typedef {import('../utils').ComponentComputedProperty } ComponentComputedProperty
12
13
*/
13
14
@@ -97,11 +98,16 @@ module.exports = {
97
98
98
99
/**
99
100
* @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression } node
100
- * @param {VueObjectData } data
101
+ * @param {VueObjectData|undefined } [info]
101
102
*/
102
- function onFunctionEnter ( node , { node : vueNode } ) {
103
+ function onFunctionEnter ( node , info ) {
103
104
if ( node . async ) {
104
- verify ( node , node . body , 'async' , computedPropertiesMap . get ( vueNode ) )
105
+ verify (
106
+ node ,
107
+ node . body ,
108
+ 'async' ,
109
+ info ? computedPropertiesMap . get ( info . node ) : null
110
+ )
105
111
}
106
112
107
113
scopeStack = {
@@ -118,10 +124,10 @@ module.exports = {
118
124
* @param {ESNode } node
119
125
* @param {BlockStatement | Expression } targetBody
120
126
* @param {keyof expressionTypes } type
121
- * @param {ComponentComputedProperty[] } computedProperties
127
+ * @param {ComponentComputedProperty[]|undefined|null } computedProperties
122
128
*/
123
- function verify ( node , targetBody , type , computedProperties = [ ] ) {
124
- for ( const cp of computedProperties ) {
129
+ function verify ( node , targetBody , type , computedProperties ) {
130
+ for ( const cp of computedProperties || [ ] ) {
125
131
if (
126
132
cp . value &&
127
133
node . loc . start . line >= cp . value . loc . start . line &&
@@ -158,7 +164,74 @@ module.exports = {
158
164
}
159
165
}
160
166
}
161
- return Object . assign (
167
+ const nodeVisitor = {
168
+ ':function' : onFunctionEnter ,
169
+ ':function:exit' : onFunctionExit ,
170
+
171
+ /**
172
+ * @param {NewExpression } node
173
+ * @param {VueObjectData|undefined } [info]
174
+ */
175
+ NewExpression ( node , info ) {
176
+ if ( ! scopeStack ) {
177
+ return
178
+ }
179
+ if (
180
+ node . callee . type === 'Identifier' &&
181
+ node . callee . name === 'Promise'
182
+ ) {
183
+ verify (
184
+ node ,
185
+ scopeStack . body ,
186
+ 'new' ,
187
+ info ? computedPropertiesMap . get ( info . node ) : null
188
+ )
189
+ }
190
+ } ,
191
+
192
+ /**
193
+ * @param {CallExpression } node
194
+ * @param {VueObjectData|undefined } [info]
195
+ */
196
+ CallExpression ( node , info ) {
197
+ if ( ! scopeStack ) {
198
+ return
199
+ }
200
+ if ( isPromise ( node ) ) {
201
+ verify (
202
+ node ,
203
+ scopeStack . body ,
204
+ 'promise' ,
205
+ info ? computedPropertiesMap . get ( info . node ) : null
206
+ )
207
+ } else if ( isTimedFunction ( node ) ) {
208
+ verify (
209
+ node ,
210
+ scopeStack . body ,
211
+ 'timed' ,
212
+ info ? computedPropertiesMap . get ( info . node ) : null
213
+ )
214
+ }
215
+ } ,
216
+
217
+ /**
218
+ * @param {AwaitExpression } node
219
+ * @param {VueObjectData|undefined } [info]
220
+ */
221
+ AwaitExpression ( node , info ) {
222
+ if ( ! scopeStack ) {
223
+ return
224
+ }
225
+ verify (
226
+ node ,
227
+ scopeStack . body ,
228
+ 'await' ,
229
+ info ? computedPropertiesMap . get ( info . node ) : null
230
+ )
231
+ }
232
+ }
233
+
234
+ return utils . compositingVisitors (
162
235
{
163
236
Program ( ) {
164
237
const tracker = new ReferenceTracker ( context . getScope ( ) )
@@ -181,63 +254,14 @@ module.exports = {
181
254
}
182
255
}
183
256
} ,
184
- utils . defineVueVisitor ( context , {
185
- onVueObjectEnter ( node ) {
186
- computedPropertiesMap . set ( node , utils . getComputedProperties ( node ) )
187
- } ,
188
- ':function' : onFunctionEnter ,
189
- ':function:exit' : onFunctionExit ,
190
-
191
- NewExpression ( node , { node : vueNode } ) {
192
- if ( ! scopeStack ) {
193
- return
194
- }
195
- if (
196
- node . callee . type === 'Identifier' &&
197
- node . callee . name === 'Promise'
198
- ) {
199
- verify (
200
- node ,
201
- scopeStack . body ,
202
- 'new' ,
203
- computedPropertiesMap . get ( vueNode )
204
- )
205
- }
206
- } ,
207
-
208
- CallExpression ( node , { node : vueNode } ) {
209
- if ( ! scopeStack ) {
210
- return
211
- }
212
- if ( isPromise ( node ) ) {
213
- verify (
214
- node ,
215
- scopeStack . body ,
216
- 'promise' ,
217
- computedPropertiesMap . get ( vueNode )
218
- )
219
- } else if ( isTimedFunction ( node ) ) {
220
- verify (
221
- node ,
222
- scopeStack . body ,
223
- 'timed' ,
224
- computedPropertiesMap . get ( vueNode )
225
- )
226
- }
227
- } ,
228
-
229
- AwaitExpression ( node , { node : vueNode } ) {
230
- if ( ! scopeStack ) {
231
- return
232
- }
233
- verify (
234
- node ,
235
- scopeStack . body ,
236
- 'await' ,
237
- computedPropertiesMap . get ( vueNode )
238
- )
239
- }
240
- } )
257
+ utils . isScriptSetup ( context )
258
+ ? utils . defineScriptSetupVisitor ( context , nodeVisitor )
259
+ : utils . defineVueVisitor ( context , {
260
+ onVueObjectEnter ( node ) {
261
+ computedPropertiesMap . set ( node , utils . getComputedProperties ( node ) )
262
+ } ,
263
+ ...nodeVisitor
264
+ } )
241
265
)
242
266
}
243
267
}
0 commit comments