|
1 | 1 | import { asyncWalk, walk } from 'estree-walker' |
2 | | -import { isExpressionType } from './check' |
| 2 | +import { |
| 3 | + isExpressionType, |
| 4 | + isForStatement, |
| 5 | + isFunctionType, |
| 6 | + isIdentifier, |
| 7 | + isReferencedIdentifier, |
| 8 | +} from './check' |
| 9 | +import { extractIdentifiers } from './extract' |
3 | 10 | import { resolveString } from './resolve' |
| 11 | +import { TS_NODE_TYPES } from './utils' |
4 | 12 | import type { LiteralUnion } from './types' |
5 | 13 | import type * as t from '@babel/types' |
6 | 14 |
|
@@ -219,3 +227,178 @@ export function walkExportDeclaration( |
219 | 227 |
|
220 | 228 | setExport() |
221 | 229 | } |
| 230 | + |
| 231 | +/** |
| 232 | + * Modified from https://github.com/vuejs/core/blob/main/packages/compiler-core/src/babelUtils.ts |
| 233 | + * To support browser environments and JSX. |
| 234 | + * |
| 235 | + * https://github.com/vuejs/core/blob/main/LICENSE |
| 236 | + */ |
| 237 | + |
| 238 | +/** |
| 239 | + * Return value indicates whether the AST walked can be a constant |
| 240 | + */ |
| 241 | +export function walkIdentifiers( |
| 242 | + root: t.Node, |
| 243 | + onIdentifier: ( |
| 244 | + node: t.Identifier, |
| 245 | + parent: t.Node | null | undefined, |
| 246 | + parentStack: t.Node[], |
| 247 | + isReference: boolean, |
| 248 | + isLocal: boolean, |
| 249 | + ) => void, |
| 250 | + includeAll = false, |
| 251 | + parentStack: t.Node[] = [], |
| 252 | + knownIds: Record<string, number> = Object.create(null), |
| 253 | +): void { |
| 254 | + const rootExp = |
| 255 | + root.type === 'Program' |
| 256 | + ? root.body[0].type === 'ExpressionStatement' && root.body[0].expression |
| 257 | + : root |
| 258 | + |
| 259 | + walkAST<t.Node>(root, { |
| 260 | + enter(node: t.Node & { scopeIds?: Set<string> }, parent) { |
| 261 | + parent && parentStack.push(parent) |
| 262 | + if ( |
| 263 | + parent && |
| 264 | + parent.type.startsWith('TS') && |
| 265 | + !TS_NODE_TYPES.includes(parent.type as any) |
| 266 | + ) { |
| 267 | + return this.skip() |
| 268 | + } |
| 269 | + if (isIdentifier(node)) { |
| 270 | + const isLocal = !!knownIds[node.name] |
| 271 | + const isRefed = isReferencedIdentifier(node, parent, parentStack) |
| 272 | + if (includeAll || (isRefed && !isLocal)) { |
| 273 | + onIdentifier(node, parent, parentStack, isRefed, isLocal) |
| 274 | + } |
| 275 | + } else if ( |
| 276 | + node.type === 'ObjectProperty' && |
| 277 | + parent?.type === 'ObjectPattern' |
| 278 | + ) { |
| 279 | + // mark property in destructure pattern |
| 280 | + ;(node as any).inPattern = true |
| 281 | + } else if (isFunctionType(node)) { |
| 282 | + /* v8 ignore start */ |
| 283 | + if (node.scopeIds) { |
| 284 | + node.scopeIds.forEach((id) => markKnownIds(id, knownIds)) |
| 285 | + /* v8 ignore end */ |
| 286 | + } else { |
| 287 | + // walk function expressions and add its arguments to known identifiers |
| 288 | + // so that we don't prefix them |
| 289 | + walkFunctionParams(node, (id) => |
| 290 | + markScopeIdentifier(node, id, knownIds), |
| 291 | + ) |
| 292 | + } |
| 293 | + } else if (node.type === 'BlockStatement') { |
| 294 | + /* v8 ignore start */ |
| 295 | + if (node.scopeIds) { |
| 296 | + node.scopeIds.forEach((id) => markKnownIds(id, knownIds)) |
| 297 | + /* v8 ignore end */ |
| 298 | + } else { |
| 299 | + // #3445 record block-level local variables |
| 300 | + walkBlockDeclarations(node, (id) => |
| 301 | + markScopeIdentifier(node, id, knownIds), |
| 302 | + ) |
| 303 | + } |
| 304 | + } else if (node.type === 'CatchClause' && node.param) { |
| 305 | + for (const id of extractIdentifiers(node.param)) { |
| 306 | + markScopeIdentifier(node, id, knownIds) |
| 307 | + } |
| 308 | + } else if (isForStatement(node)) { |
| 309 | + walkForStatement(node, false, (id) => |
| 310 | + markScopeIdentifier(node, id, knownIds), |
| 311 | + ) |
| 312 | + } |
| 313 | + }, |
| 314 | + leave(node: t.Node & { scopeIds?: Set<string> }, parent) { |
| 315 | + parent && parentStack.pop() |
| 316 | + if (node !== rootExp && node.scopeIds) { |
| 317 | + for (const id of node.scopeIds) { |
| 318 | + knownIds[id]-- |
| 319 | + if (knownIds[id] === 0) { |
| 320 | + delete knownIds[id] |
| 321 | + } |
| 322 | + } |
| 323 | + } |
| 324 | + }, |
| 325 | + }) |
| 326 | +} |
| 327 | + |
| 328 | +export function walkFunctionParams( |
| 329 | + node: t.Function, |
| 330 | + onIdent: (id: t.Identifier) => void, |
| 331 | +): void { |
| 332 | + for (const p of node.params) { |
| 333 | + for (const id of extractIdentifiers(p)) { |
| 334 | + onIdent(id) |
| 335 | + } |
| 336 | + } |
| 337 | +} |
| 338 | + |
| 339 | +export function walkBlockDeclarations( |
| 340 | + block: t.BlockStatement | t.Program, |
| 341 | + onIdent: (node: t.Identifier) => void, |
| 342 | +): void { |
| 343 | + for (const stmt of block.body) { |
| 344 | + if (stmt.type === 'VariableDeclaration') { |
| 345 | + if (stmt.declare) continue |
| 346 | + for (const decl of stmt.declarations) { |
| 347 | + for (const id of extractIdentifiers(decl.id)) { |
| 348 | + onIdent(id) |
| 349 | + } |
| 350 | + } |
| 351 | + } else if ( |
| 352 | + stmt.type === 'FunctionDeclaration' || |
| 353 | + stmt.type === 'ClassDeclaration' |
| 354 | + ) { |
| 355 | + /* v8 ignore next */ |
| 356 | + if (stmt.declare || !stmt.id) continue |
| 357 | + onIdent(stmt.id) |
| 358 | + } else if (isForStatement(stmt)) { |
| 359 | + walkForStatement(stmt, true, onIdent) |
| 360 | + } |
| 361 | + } |
| 362 | +} |
| 363 | + |
| 364 | +function walkForStatement( |
| 365 | + stmt: t.ForStatement | t.ForOfStatement | t.ForInStatement, |
| 366 | + isVar: boolean, |
| 367 | + onIdent: (id: t.Identifier) => void, |
| 368 | +) { |
| 369 | + const variable = stmt.type === 'ForStatement' ? stmt.init : stmt.left |
| 370 | + if ( |
| 371 | + variable && |
| 372 | + variable.type === 'VariableDeclaration' && |
| 373 | + (variable.kind === 'var' ? isVar : !isVar) |
| 374 | + ) { |
| 375 | + for (const decl of variable.declarations) { |
| 376 | + for (const id of extractIdentifiers(decl.id)) { |
| 377 | + onIdent(id) |
| 378 | + } |
| 379 | + } |
| 380 | + } |
| 381 | +} |
| 382 | + |
| 383 | +function markKnownIds(name: string, knownIds: Record<string, number>) { |
| 384 | + if (name in knownIds) { |
| 385 | + knownIds[name]++ |
| 386 | + } else { |
| 387 | + knownIds[name] = 1 |
| 388 | + } |
| 389 | +} |
| 390 | + |
| 391 | +function markScopeIdentifier( |
| 392 | + node: t.Node & { scopeIds?: Set<string> }, |
| 393 | + child: t.Identifier, |
| 394 | + knownIds: Record<string, number>, |
| 395 | +) { |
| 396 | + const { name } = child |
| 397 | + /* v8 ignore start */ |
| 398 | + if (node.scopeIds && node.scopeIds.has(name)) { |
| 399 | + return |
| 400 | + } |
| 401 | + /* v8 ignore end */ |
| 402 | + markKnownIds(name, knownIds) |
| 403 | + ;(node.scopeIds || (node.scopeIds = new Set())).add(name) |
| 404 | +} |
0 commit comments