|
7 | 7 | ArrayPattern,
|
8 | 8 | Program,
|
9 | 9 | VariableDeclarator,
|
10 |
| - Expression |
| 10 | + Expression, |
| 11 | + VariableDeclaration |
11 | 12 | } from '@babel/types'
|
12 | 13 | import MagicString, { SourceMap } from 'magic-string'
|
13 | 14 | import { walk } from 'estree-walker'
|
@@ -216,40 +217,49 @@ export function transformAST(
|
216 | 217 | function walkScope(node: Program | BlockStatement, isRoot = false) {
|
217 | 218 | for (const stmt of node.body) {
|
218 | 219 | if (stmt.type === 'VariableDeclaration') {
|
219 |
| - if (stmt.declare) continue |
220 |
| - for (const decl of stmt.declarations) { |
221 |
| - let refCall |
222 |
| - const isCall = |
223 |
| - decl.init && |
224 |
| - decl.init.type === 'CallExpression' && |
225 |
| - decl.init.callee.type === 'Identifier' |
226 |
| - if ( |
227 |
| - isCall && |
228 |
| - (refCall = isRefCreationCall((decl as any).init.callee.name)) |
229 |
| - ) { |
230 |
| - processRefDeclaration(refCall, decl.id, decl.init as CallExpression) |
231 |
| - } else { |
232 |
| - const isProps = |
233 |
| - isRoot && |
234 |
| - isCall && |
235 |
| - (decl as any).init.callee.name === 'defineProps' |
236 |
| - for (const id of extractIdentifiers(decl.id)) { |
237 |
| - if (isProps) { |
238 |
| - // for defineProps destructure, only exclude them since they |
239 |
| - // are already passed in as knownProps |
240 |
| - excludedIds.add(id) |
241 |
| - } else { |
242 |
| - registerBinding(id) |
243 |
| - } |
244 |
| - } |
245 |
| - } |
246 |
| - } |
| 220 | + walkVariableDeclaration(stmt, isRoot) |
247 | 221 | } else if (
|
248 | 222 | stmt.type === 'FunctionDeclaration' ||
|
249 | 223 | stmt.type === 'ClassDeclaration'
|
250 | 224 | ) {
|
251 | 225 | if (stmt.declare || !stmt.id) continue
|
252 | 226 | registerBinding(stmt.id)
|
| 227 | + } else if ( |
| 228 | + (stmt.type === 'ForOfStatement' || stmt.type === 'ForInStatement') && |
| 229 | + stmt.left.type === 'VariableDeclaration' |
| 230 | + ) { |
| 231 | + walkVariableDeclaration(stmt.left) |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + function walkVariableDeclaration(stmt: VariableDeclaration, isRoot = false) { |
| 237 | + if (stmt.declare) { |
| 238 | + return |
| 239 | + } |
| 240 | + for (const decl of stmt.declarations) { |
| 241 | + let refCall |
| 242 | + const isCall = |
| 243 | + decl.init && |
| 244 | + decl.init.type === 'CallExpression' && |
| 245 | + decl.init.callee.type === 'Identifier' |
| 246 | + if ( |
| 247 | + isCall && |
| 248 | + (refCall = isRefCreationCall((decl as any).init.callee.name)) |
| 249 | + ) { |
| 250 | + processRefDeclaration(refCall, decl.id, decl.init as CallExpression) |
| 251 | + } else { |
| 252 | + const isProps = |
| 253 | + isRoot && isCall && (decl as any).init.callee.name === 'defineProps' |
| 254 | + for (const id of extractIdentifiers(decl.id)) { |
| 255 | + if (isProps) { |
| 256 | + // for defineProps destructure, only exclude them since they |
| 257 | + // are already passed in as knownProps |
| 258 | + excludedIds.add(id) |
| 259 | + } else { |
| 260 | + registerBinding(id) |
| 261 | + } |
| 262 | + } |
253 | 263 | }
|
254 | 264 | }
|
255 | 265 | }
|
|
0 commit comments