@@ -54,15 +54,21 @@ module.exports = {
54
54
} ,
55
55
/** @param {RuleContext } context */
56
56
create ( context ) {
57
+ /**
58
+ * @typedef {object } SetupScopeData
59
+ * @property {boolean } afterAwait
60
+ * @property {[number,number] } range
61
+ */
62
+
57
63
/** @type {Map<ESNode, string> } */
58
64
const restrictedCallNodes = new Map ( )
59
- /** @type {Map<FunctionExpression | ArrowFunctionExpression | FunctionDeclaration, { setupProperty: Property, afterAwait: boolean } > } */
60
- const setupFunctions = new Map ( )
65
+ /** @type {Map<FunctionExpression | ArrowFunctionExpression | FunctionDeclaration | Program, SetupScopeData > } */
66
+ const setupScopes = new Map ( )
61
67
62
68
/**x
63
69
* @typedef {object } ScopeStack
64
70
* @property {ScopeStack | null } upper
65
- * @property {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration } functionNode
71
+ * @property {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration | Program } scopeNode
66
72
*/
67
73
/** @type {ScopeStack | null } */
68
74
let scopeStack = null
@@ -136,6 +142,11 @@ module.exports = {
136
142
{
137
143
/** @param {Program } node */
138
144
Program ( node ) {
145
+ scopeStack = {
146
+ upper : scopeStack ,
147
+ scopeNode : node
148
+ }
149
+
139
150
const tracker = new ReferenceTracker ( context . getScope ( ) )
140
151
141
152
for ( const option of context . options ) {
@@ -170,39 +181,39 @@ module.exports = {
170
181
}
171
182
}
172
183
}
173
- }
174
- } ,
175
- utils . defineVueVisitor ( context , {
184
+ } ,
176
185
/** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration } node */
177
186
':function' ( node ) {
178
187
scopeStack = {
179
188
upper : scopeStack ,
180
- functionNode : node
189
+ scopeNode : node
181
190
}
182
191
} ,
183
- onSetupFunctionEnter ( node ) {
184
- setupFunctions . set ( node , {
185
- setupProperty : node . parent ,
186
- afterAwait : false
187
- } )
192
+ ':function:exit' ( ) {
193
+ scopeStack = scopeStack && scopeStack . upper
188
194
} ,
189
- AwaitExpression ( ) {
195
+ /** @param {AwaitExpression } node */
196
+ AwaitExpression ( node ) {
190
197
if ( ! scopeStack ) {
191
198
return
192
199
}
193
- const setupFunctionData = setupFunctions . get ( scopeStack . functionNode )
194
- if ( ! setupFunctionData ) {
200
+ const setupScope = setupScopes . get ( scopeStack . scopeNode )
201
+ if ( ! setupScope || ! utils . inRange ( setupScope . range , node ) ) {
195
202
return
196
203
}
197
- setupFunctionData . afterAwait = true
204
+ setupScope . afterAwait = true
198
205
} ,
199
206
/** @param {CallExpression } node */
200
207
CallExpression ( node ) {
201
208
if ( ! scopeStack ) {
202
209
return
203
210
}
204
- const setupFunctionData = setupFunctions . get ( scopeStack . functionNode )
205
- if ( ! setupFunctionData || ! setupFunctionData . afterAwait ) {
211
+ const setupScope = setupScopes . get ( scopeStack . scopeNode )
212
+ if (
213
+ ! setupScope ||
214
+ ! setupScope . afterAwait ||
215
+ ! utils . inRange ( setupScope . range , node )
216
+ ) {
206
217
return
207
218
}
208
219
@@ -214,12 +225,34 @@ module.exports = {
214
225
data : { message }
215
226
} )
216
227
}
228
+ }
229
+ } ,
230
+ ( ( ) => {
231
+ const scriptSetup = utils . getScriptSetupElement ( context )
232
+ if ( ! scriptSetup ) {
233
+ return { }
234
+ }
235
+ return {
236
+ /**
237
+ * @param {Program } node
238
+ */
239
+ Program ( node ) {
240
+ setupScopes . set ( node , {
241
+ afterAwait : false ,
242
+ range : scriptSetup . range
243
+ } )
244
+ }
245
+ }
246
+ } ) ( ) ,
247
+ utils . defineVueVisitor ( context , {
248
+ onSetupFunctionEnter ( node ) {
249
+ setupScopes . set ( node , {
250
+ afterAwait : false ,
251
+ range : node . range
252
+ } )
217
253
} ,
218
- /** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration } node */
219
- ':function:exit' ( node ) {
220
- scopeStack = scopeStack && scopeStack . upper
221
-
222
- setupFunctions . delete ( node )
254
+ onSetupFunctionExit ( node ) {
255
+ setupScopes . delete ( node )
223
256
}
224
257
} )
225
258
)
0 commit comments