@@ -57,20 +57,31 @@ module.exports = {
57
57
/** @param {RuleContext } context */
58
58
create ( context ) {
59
59
const watchCallNodes = new Set ( )
60
- /** @type {Map<FunctionExpression | ArrowFunctionExpression | FunctionDeclaration, { setupProperty: Property, afterAwait: boolean }> } */
61
- const setupFunctions = new Map ( )
60
+ /**
61
+ * @typedef {object } SetupScopeData
62
+ * @property {boolean } afterAwait
63
+ * @property {[number,number] } range
64
+ */
65
+ /** @type {Map<FunctionExpression | ArrowFunctionExpression | FunctionDeclaration | Program, SetupScopeData> } */
66
+ const setupScopes = new Map ( )
62
67
63
68
/**
64
69
* @typedef {object } ScopeStack
65
70
* @property {ScopeStack | null } upper
66
- * @property {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration } functionNode
71
+ * @property {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration | Program } scopeNode
67
72
*/
68
73
/** @type {ScopeStack | null } */
69
74
let scopeStack = null
70
75
71
- return Object . assign (
76
+ return utils . compositingVisitors (
72
77
{
73
- Program ( ) {
78
+ /** @param {Program } node */
79
+ Program ( node ) {
80
+ scopeStack = {
81
+ upper : scopeStack ,
82
+ scopeNode : node
83
+ }
84
+
74
85
const tracker = new ReferenceTracker ( context . getScope ( ) )
75
86
const traceMap = {
76
87
vue : {
@@ -87,39 +98,39 @@ module.exports = {
87
98
for ( const { node } of tracker . iterateEsmReferences ( traceMap ) ) {
88
99
watchCallNodes . add ( node )
89
100
}
90
- }
91
- } ,
92
- utils . defineVueVisitor ( context , {
101
+ } ,
93
102
/** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration } node */
94
103
':function' ( node ) {
95
104
scopeStack = {
96
105
upper : scopeStack ,
97
- functionNode : node
106
+ scopeNode : node
98
107
}
99
108
} ,
100
- onSetupFunctionEnter ( node ) {
101
- setupFunctions . set ( node , {
102
- setupProperty : node . parent ,
103
- afterAwait : false
104
- } )
109
+ ':function:exit' ( ) {
110
+ scopeStack = scopeStack && scopeStack . upper
105
111
} ,
106
- AwaitExpression ( ) {
112
+ /** @param {AwaitExpression } node */
113
+ AwaitExpression ( node ) {
107
114
if ( ! scopeStack ) {
108
115
return
109
116
}
110
- const setupFunctionData = setupFunctions . get ( scopeStack . functionNode )
111
- if ( ! setupFunctionData ) {
117
+ const setupScope = setupScopes . get ( scopeStack . scopeNode )
118
+ if ( ! setupScope || ! utils . inRange ( setupScope . range , node ) ) {
112
119
return
113
120
}
114
- setupFunctionData . afterAwait = true
121
+ setupScope . afterAwait = true
115
122
} ,
116
123
/** @param {CallExpression } node */
117
124
CallExpression ( node ) {
118
125
if ( ! scopeStack ) {
119
126
return
120
127
}
121
- const setupFunctionData = setupFunctions . get ( scopeStack . functionNode )
122
- if ( ! setupFunctionData || ! setupFunctionData . afterAwait ) {
128
+ const setupScope = setupScopes . get ( scopeStack . scopeNode )
129
+ if (
130
+ ! setupScope ||
131
+ ! setupScope . afterAwait ||
132
+ ! utils . inRange ( setupScope . range , node )
133
+ ) {
123
134
return
124
135
}
125
136
@@ -129,12 +140,34 @@ module.exports = {
129
140
messageId : 'forbidden'
130
141
} )
131
142
}
143
+ }
144
+ } ,
145
+ ( ( ) => {
146
+ const scriptSetup = utils . getScriptSetupElement ( context )
147
+ if ( ! scriptSetup ) {
148
+ return { }
149
+ }
150
+ return {
151
+ /**
152
+ * @param {Program } node
153
+ */
154
+ Program ( node ) {
155
+ setupScopes . set ( node , {
156
+ afterAwait : false ,
157
+ range : scriptSetup . range
158
+ } )
159
+ }
160
+ }
161
+ } ) ( ) ,
162
+ utils . defineVueVisitor ( context , {
163
+ onSetupFunctionEnter ( node ) {
164
+ setupScopes . set ( node , {
165
+ afterAwait : false ,
166
+ range : node . range
167
+ } )
132
168
} ,
133
- /** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration } node */
134
- ':function:exit' ( node ) {
135
- scopeStack = scopeStack && scopeStack . upper
136
-
137
- setupFunctions . delete ( node )
169
+ onSetupFunctionExit ( node ) {
170
+ setupScopes . delete ( node )
138
171
}
139
172
} )
140
173
)
0 commit comments