| 1 |
import Renamer from "./lib/renamer"; |
| 2 |
import type NodePath from "../path"; |
| 3 |
import traverse from "../index"; |
| 4 |
import type { TraverseOptions } from "../index"; |
| 5 |
import Binding from "./binding"; |
| 6 |
import type { BindingKind } from "./binding"; |
| 7 |
import globals from "globals"; |
| 8 |
import { |
| 9 |
NOT_LOCAL_BINDING, |
| 10 |
callExpression, |
| 11 |
cloneNode, |
| 12 |
getBindingIdentifiers, |
| 13 |
identifier, |
| 14 |
isArrayExpression, |
| 15 |
isBinary, |
| 16 |
isClass, |
| 17 |
isClassBody, |
| 18 |
isClassDeclaration, |
| 19 |
isExportAllDeclaration, |
| 20 |
isExportDefaultDeclaration, |
| 21 |
isExportNamedDeclaration, |
| 22 |
isFunctionDeclaration, |
| 23 |
isIdentifier, |
| 24 |
isImportDeclaration, |
| 25 |
isLiteral, |
| 26 |
isMethod, |
| 27 |
isModuleSpecifier, |
| 28 |
isNullLiteral, |
| 29 |
isObjectExpression, |
| 30 |
isProperty, |
| 31 |
isPureish, |
| 32 |
isRegExpLiteral, |
| 33 |
isSuper, |
| 34 |
isTaggedTemplateExpression, |
| 35 |
isTemplateLiteral, |
| 36 |
isThisExpression, |
| 37 |
isUnaryExpression, |
| 38 |
isVariableDeclaration, |
| 39 |
matchesPattern, |
| 40 |
memberExpression, |
| 41 |
numericLiteral, |
| 42 |
toIdentifier, |
| 43 |
unaryExpression, |
| 44 |
variableDeclaration, |
| 45 |
variableDeclarator, |
| 46 |
isRecordExpression, |
| 47 |
isTupleExpression, |
| 48 |
isObjectProperty, |
| 49 |
isTopicReference, |
| 50 |
isMetaProperty, |
| 51 |
isPrivateName, |
| 52 |
isExportDeclaration, |
| 53 |
} from "@babel/types"; |
| 54 |
import type * as t from "@babel/types"; |
| 55 |
import { scope as scopeCache } from "../cache"; |
| 56 |
import type { Visitor } from "../types"; |
| 57 |
|
| 58 |
type NodePart = string | number | boolean; |
| 59 |
// Recursively gathers the identifying names of a node. |
| 60 |
function gatherNodeParts(node: t.Node, parts: NodePart[]) { |
| 61 |
switch (node?.type) { |
| 62 |
default: |
| 63 |
if (isImportDeclaration(node) || isExportDeclaration(node)) { |
| 64 |
if ( |
| 65 |
(isExportAllDeclaration(node) || |
| 66 |
isExportNamedDeclaration(node) || |
| 67 |
isImportDeclaration(node)) && |
| 68 |
node.source |
| 69 |
) { |
| 70 |
gatherNodeParts(node.source, parts); |
| 71 |
} else if ( |
| 72 |
(isExportNamedDeclaration(node) || isImportDeclaration(node)) && |
| 73 |
node.specifiers && |
| 74 |
node.specifiers.length |
| 75 |
) { |
| 76 |
for (const e of node.specifiers) gatherNodeParts(e, parts); |
| 77 |
} else if ( |
| 78 |
(isExportDefaultDeclaration(node) || |
| 79 |
isExportNamedDeclaration(node)) && |
| 80 |
node.declaration |
| 81 |
) { |
| 82 |
gatherNodeParts(node.declaration, parts); |
| 83 |
} |
| 84 |
} else if (isModuleSpecifier(node)) { |
| 85 |
// todo(flow->ts): should condition instead be: |
| 86 |
// ``` |
| 87 |
// t.isExportSpecifier(node) || |
| 88 |
// t.isImportDefaultSpecifier(node) || |
| 89 |
// t.isImportNamespaceSpecifier(node) || |
| 90 |
// t.isImportSpecifier(node) |
| 91 |
// ``` |
| 92 |
// allowing only nodes with `.local`? |
| 93 |
// @ts-expect-error todo(flow->ts) |
| 94 |
gatherNodeParts(node.local, parts); |
| 95 |
} else if ( |
| 96 |
isLiteral(node) && |
| 97 |
!isNullLiteral(node) && |
| 98 |
!isRegExpLiteral(node) && |
| 99 |
!isTemplateLiteral(node) |
| 100 |
) { |
| 101 |
parts.push(node.value); |
| 102 |
} |
| 103 |
break; |
| 104 |
|
| 105 |
case "MemberExpression": |
| 106 |
case "OptionalMemberExpression": |
| 107 |
case "JSXMemberExpression": |
| 108 |
gatherNodeParts(node.object, parts); |
| 109 |
gatherNodeParts(node.property, parts); |
| 110 |
break; |
| 111 |
|
| 112 |
case "Identifier": |
| 113 |
case "JSXIdentifier": |
| 114 |
parts.push(node.name); |
| 115 |
break; |
| 116 |
|
| 117 |
case "CallExpression": |
| 118 |
case "OptionalCallExpression": |
| 119 |
case "NewExpression": |
| 120 |
gatherNodeParts(node.callee, parts); |
| 121 |
break; |
| 122 |
|
| 123 |
case "ObjectExpression": |
| 124 |
case "ObjectPattern": |
| 125 |
for (const e of node.properties) { |
| 126 |
gatherNodeParts(e, parts); |
| 127 |
} |
| 128 |
break; |
| 129 |
|
| 130 |
case "SpreadElement": |
| 131 |
case "RestElement": |
| 132 |
gatherNodeParts(node.argument, parts); |
| 133 |
break; |
| 134 |
|
| 135 |
case "ObjectProperty": |
| 136 |
case "ObjectMethod": |
| 137 |
case "ClassProperty": |
| 138 |
case "ClassMethod": |
| 139 |
case "ClassPrivateProperty": |
| 140 |
case "ClassPrivateMethod": |
| 141 |
gatherNodeParts(node.key, parts); |
| 142 |
break; |
| 143 |
|
| 144 |
case "ThisExpression": |
| 145 |
parts.push("this"); |
| 146 |
break; |
| 147 |
|
| 148 |
case "Super": |
| 149 |
parts.push("super"); |
| 150 |
break; |
| 151 |
|
| 152 |
case "Import": |
| 153 |
parts.push("import"); |
| 154 |
break; |
| 155 |
|
| 156 |
case "DoExpression": |
| 157 |
parts.push("do"); |
| 158 |
break; |
| 159 |
|
| 160 |
case "YieldExpression": |
| 161 |
parts.push("yield"); |
| 162 |
gatherNodeParts(node.argument, parts); |
| 163 |
break; |
| 164 |
|
| 165 |
case "AwaitExpression": |
| 166 |
parts.push("await"); |
| 167 |
gatherNodeParts(node.argument, parts); |
| 168 |
break; |
| 169 |
|
| 170 |
case "AssignmentExpression": |
| 171 |
gatherNodeParts(node.left, parts); |
| 172 |
break; |
| 173 |
|
| 174 |
case "VariableDeclarator": |
| 175 |
gatherNodeParts(node.id, parts); |
| 176 |
break; |
| 177 |
|
| 178 |
case "FunctionExpression": |
| 179 |
case "FunctionDeclaration": |
| 180 |
case "ClassExpression": |
| 181 |
case "ClassDeclaration": |
| 182 |
gatherNodeParts(node.id, parts); |
| 183 |
break; |
| 184 |
|
| 185 |
case "PrivateName": |
| 186 |
gatherNodeParts(node.id, parts); |
| 187 |
break; |
| 188 |
|
| 189 |
case "ParenthesizedExpression": |
| 190 |
gatherNodeParts(node.expression, parts); |
| 191 |
break; |
| 192 |
|
| 193 |
case "UnaryExpression": |
| 194 |
case "UpdateExpression": |
| 195 |
gatherNodeParts(node.argument, parts); |
| 196 |
break; |
| 197 |
|
| 198 |
case "MetaProperty": |
| 199 |
gatherNodeParts(node.meta, parts); |
| 200 |
gatherNodeParts(node.property, parts); |
| 201 |
break; |
| 202 |
|
| 203 |
case "JSXElement": |
| 204 |
gatherNodeParts(node.openingElement, parts); |
| 205 |
break; |
| 206 |
|
| 207 |
case "JSXOpeningElement": |
| 208 |
gatherNodeParts(node.name, parts); |
| 209 |
break; |
| 210 |
|
| 211 |
case "JSXFragment": |
| 212 |
gatherNodeParts(node.openingFragment, parts); |
| 213 |
break; |
| 214 |
|
| 215 |
case "JSXOpeningFragment": |
| 216 |
parts.push("Fragment"); |
| 217 |
break; |
| 218 |
|
| 219 |
case "JSXNamespacedName": |
| 220 |
gatherNodeParts(node.namespace, parts); |
| 221 |
gatherNodeParts(node.name, parts); |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
// |
| 227 |
interface CollectVisitorState { |
| 228 |
assignments: NodePath<t.AssignmentExpression>[]; |
| 229 |
references: NodePath<t.Identifier | t.JSXIdentifier>[]; |
| 230 |
constantViolations: NodePath[]; |
| 231 |
} |
| 232 |
|
| 233 |
const collectorVisitor: Visitor<CollectVisitorState> = { |
| 234 |
ForStatement(path) { |
| 235 |
const declar = path.get("init"); |
| 236 |
// delegate block scope handling to the `BlockScoped` method |
| 237 |
if (declar.isVar()) { |
| 238 |
const { scope } = path; |
| 239 |
const parentScope = scope.getFunctionParent() || scope.getProgramParent(); |
| 240 |
parentScope.registerBinding("var", declar); |
| 241 |
} |
| 242 |
}, |
| 243 |
|
| 244 |
Declaration(path) { |
| 245 |
// delegate block scope handling to the `BlockScoped` method |
| 246 |
if (path.isBlockScoped()) return; |
| 247 |
|
| 248 |
// delegate import handing to the `ImportDeclaration` method |
| •249 |
// @ts-ignore |
| 250 |
if (path.isImportDeclaration()) return; |
| 251 |
|
| 252 |
// this will be hit again once we traverse into it after this iteration |
| •253 |
// @ts-ignore |
| 254 |
if (path.isExportDeclaration()) return; |
| 255 |
|
| 256 |
// we've ran into a declaration! |
| 257 |
const parent = |
| •258 |
// @ts-ignore |
| 259 |
path.scope.getFunctionParent() || path.scope.getProgramParent(); |
| 260 |
parent.registerDeclaration(path); |
| 261 |
}, |
| 262 |
|
| 263 |
ImportDeclaration(path) { |
| 264 |
// import may only appear in the top level or inside a module/namespace (for TS/flow) |
| 265 |
const parent = path.scope.getBlockParent(); |
| 266 |
|
| 267 |
parent.registerDeclaration(path); |
| 268 |
}, |
| 269 |
|
| 270 |
ReferencedIdentifier(path, state) { |
| 271 |
state.references.push(path); |
| 272 |
}, |
| 273 |
|
| 274 |
ForXStatement(path, state) { |
| 275 |
const left = path.get("left"); |
| 276 |
if (left.isPattern() || left.isIdentifier()) { |
| 277 |
state.constantViolations.push(path); |
| 278 |
} |
| 279 |
// delegate block scope handling to the `BlockScoped` method |
| 280 |
else if (left.isVar()) { |
| 281 |
const { scope } = path; |
| 282 |
const parentScope = scope.getFunctionParent() || scope.getProgramParent(); |
| 283 |
parentScope.registerBinding("var", left); |
| 284 |
} |
| 285 |
}, |
| 286 |
|
| 287 |
ExportDeclaration: { |
| 288 |
exit(path) { |
| 289 |
const { node, scope } = path; |
| 290 |
// ExportAllDeclaration does not have `declaration` |
| 291 |
if (isExportAllDeclaration(node)) return; |
| 292 |
const declar = node.declaration; |
| 293 |
if (isClassDeclaration(declar) || isFunctionDeclaration(declar)) { |
| 294 |
const id = declar.id; |
| 295 |
if (!id) return; |
| 296 |
|
| 297 |
const binding = scope.getBinding(id.name); |
| 298 |
binding?.reference(path); |
| 299 |
} else if (isVariableDeclaration(declar)) { |
| 300 |
for (const decl of declar.declarations) { |
| 301 |
for (const name of Object.keys(getBindingIdentifiers(decl))) { |
| 302 |
const binding = scope.getBinding(name); |
| 303 |
binding?.reference(path); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
}, |
| 308 |
}, |
| 309 |
|
| 310 |
LabeledStatement(path) { |
| 311 |
path.scope.getBlockParent().registerDeclaration(path); |
| 312 |
}, |
| 313 |
|
| 314 |
AssignmentExpression(path, state) { |
| 315 |
state.assignments.push(path); |
| 316 |
}, |
| 317 |
|
| 318 |
UpdateExpression(path, state) { |
| 319 |
state.constantViolations.push(path); |
| 320 |
}, |
| 321 |
|
| 322 |
UnaryExpression(path, state) { |
| 323 |
if (path.node.operator === "delete") { |
| 324 |
state.constantViolations.push(path); |
| 325 |
} |
| 326 |
}, |
| 327 |
|
| 328 |
BlockScoped(path) { |
| 329 |
let scope = path.scope; |
| 330 |
if (scope.path === path) scope = scope.parent; |
| 331 |
|
| 332 |
const parent = scope.getBlockParent(); |
| 333 |
parent.registerDeclaration(path); |
| 334 |
|
| 335 |
// Register class identifier in class' scope if this is a class declaration. |
| 336 |
if (path.isClassDeclaration() && path.node.id) { |
| 337 |
const id = path.node.id; |
| 338 |
const name = id.name; |
| 339 |
|
| 340 |
path.scope.bindings[name] = path.scope.parent.getBinding(name); |
| 341 |
} |
| 342 |
}, |
| 343 |
|
| 344 |
CatchClause(path) { |
| 345 |
path.scope.registerBinding("let", path); |
| 346 |
}, |
| 347 |
|
| 348 |
Function(path) { |
| 349 |
const params: Array<NodePath> = path.get("params"); |
| 350 |
for (const param of params) { |
| 351 |
path.scope.registerBinding("param", param); |
| 352 |
} |
| 353 |
|
| 354 |
// Register function expression id after params. When the id |
| 355 |
// collides with a function param, the id effectively can't be |
| 356 |
// referenced: here we registered it as a constantViolation |
| 357 |
if ( |
| 358 |
path.isFunctionExpression() && |
| 359 |
path.has("id") && |
| 360 |
// @ts-expect-error Fixme: document symbol ast properties |
| 361 |
!path.get("id").node[NOT_LOCAL_BINDING] |
| 362 |
) { |
| 363 |
path.scope.registerBinding("local", path.get("id"), path); |
| 364 |
} |
| 365 |
}, |
| 366 |
|
| 367 |
ClassExpression(path) { |
| 368 |
if ( |
| 369 |
path.has("id") && |
| 370 |
// @ts-expect-error Fixme: document symbol ast properties |
| 371 |
!path.get("id").node[NOT_LOCAL_BINDING] |
| 372 |
) { |
| 373 |
path.scope.registerBinding("local", path); |
| 374 |
} |
| 375 |
}, |
| 376 |
}; |
| 377 |
|
| 378 |
let uid = 0; |
| 379 |
|
| 380 |
export type { Binding }; |
| 381 |
|
| 382 |
export default class Scope { |
| 383 |
uid; |
| 384 |
|
| 385 |
path: NodePath; |
| 386 |
block: t.Pattern | t.Scopable; |
| 387 |
|
| 388 |
labels; |
| 389 |
inited; |
| 390 |
|
| 391 |
bindings: { [name: string]: Binding }; |
| 392 |
references: { [name: string]: true }; |
| 393 |
globals: { [name: string]: t.Identifier | t.JSXIdentifier }; |
| 394 |
uids: { [name: string]: boolean }; |
| 395 |
data: { [key: string | symbol]: unknown }; |
| 396 |
crawling: boolean; |
| 397 |
|
| 398 |
/** |
| 399 |
* This searches the current "scope" and collects all references/bindings |
| 400 |
* within. |
| 401 |
*/ |
| 402 |
constructor(path: NodePath<t.Pattern | t.Scopable>) { |
| 403 |
const { node } = path; |
| 404 |
const cached = scopeCache.get(node); |
| 405 |
// Sometimes, a scopable path is placed higher in the AST tree. |
| 406 |
// In these cases, have to create a new Scope. |
| 407 |
if (cached?.path === path) { |
| 408 |
return cached; |
| 409 |
} |
| 410 |
scopeCache.set(node, this); |
| 411 |
|
| 412 |
this.uid = uid++; |
| 413 |
|
| 414 |
this.block = node; |
| 415 |
this.path = path; |
| 416 |
|
| 417 |
this.labels = new Map(); |
| 418 |
this.inited = false; |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* Globals. |
| 423 |
*/ |
| 424 |
|
| 425 |
static globals = Object.keys(globals.builtin); |
| 426 |
|
| 427 |
/** |
| 428 |
* Variables available in current context. |
| 429 |
*/ |
| 430 |
|
| 431 |
static contextVariables = ["arguments", "undefined", "Infinity", "NaN"]; |
| 432 |
|
| 433 |
get parent() { |
| 434 |
let parent, |
| 435 |
path = this.path; |
| 436 |
do { |
| 437 |
// Skip method scope if coming from inside computed key or decorator expression |
| 438 |
const shouldSkip = path.key === "key" || path.listKey === "decorators"; |
| 439 |
path = path.parentPath; |
| 440 |
if (shouldSkip && path.isMethod()) path = path.parentPath; |
| 441 |
if (path && path.isScope()) parent = path; |
| 442 |
} while (path && !parent); |
| 443 |
|
| 444 |
return parent?.scope; |
| 445 |
} |
| 446 |
|
| 447 |
get parentBlock() { |
| 448 |
return this.path.parent; |
| 449 |
} |
| 450 |
|
| 451 |
get hub() { |
| 452 |
return this.path.hub; |
| 453 |
} |
| 454 |
|
| 455 |
traverse<S>( |
| 456 |
node: t.Node | t.Node[], |
| 457 |
opts: TraverseOptions<S>, |
| 458 |
state: S, |
| 459 |
): void; |
| 460 |
traverse(node: t.Node | t.Node[], opts?: TraverseOptions, state?: any): void; |
| 461 |
/** |
| 462 |
* Traverse node with current scope and path. |
| 463 |
* |
| 464 |
* !!! WARNING !!! |
| 465 |
* This method assumes that `this.path` is the NodePath representing `node`. |
| 466 |
* After running the traversal, the `.parentPath` of the NodePaths |
| 467 |
* corresponding to `node`'s children will be set to `this.path`. |
| 468 |
* |
| 469 |
* There is no good reason to use this method, since the only safe way to use |
| 470 |
* it is equivalent to `scope.path.traverse(opts, state)`. |
| 471 |
*/ |
| 472 |
traverse<S>(node: any, opts: any, state?: S) { |
| 473 |
traverse(node, opts, this, state, this.path); |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* Generate a unique identifier and add it to the current scope. |
| 478 |
*/ |
| 479 |
|
| 480 |
generateDeclaredUidIdentifier(name?: string) { |
| 481 |
const id = this.generateUidIdentifier(name); |
| 482 |
this.push({ id }); |
| 483 |
return cloneNode(id); |
| 484 |
} |
| 485 |
|
| 486 |
/** |
| 487 |
* Generate a unique identifier. |
| 488 |
*/ |
| 489 |
|
| 490 |
generateUidIdentifier(name?: string) { |
| 491 |
return identifier(this.generateUid(name)); |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* Generate a unique `_id1` binding. |
| 496 |
*/ |
| 497 |
|
| 498 |
generateUid(name: string = "temp"): string { |
| 499 |
name = toIdentifier(name) |
| 500 |
.replace(/^_+/, "") |
| 501 |
.replace(/[0-9]+$/g, ""); |
| 502 |
|
| 503 |
let uid; |
| 504 |
let i = 1; |
| 505 |
do { |
| 506 |
uid = this._generateUid(name, i); |
| 507 |
i++; |
| 508 |
} while ( |
| 509 |
this.hasLabel(uid) || |
| 510 |
this.hasBinding(uid) || |
| 511 |
this.hasGlobal(uid) || |
| 512 |
this.hasReference(uid) |
| 513 |
); |
| 514 |
|
| 515 |
const program = this.getProgramParent(); |
| 516 |
program.references[uid] = true; |
| 517 |
program.uids[uid] = true; |
| 518 |
|
| 519 |
return uid; |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* Generate an `_id1`. |
| 524 |
*/ |
| 525 |
|
| 526 |
_generateUid(name: string, i: number) { |
| 527 |
let id = name; |
| 528 |
if (i > 1) id += i; |
| 529 |
return `_${id}`; |
| 530 |
} |
| 531 |
|
| 532 |
generateUidBasedOnNode(node: t.Node, defaultName?: string) { |
| 533 |
const parts: NodePart[] = []; |
| 534 |
gatherNodeParts(node, parts); |
| 535 |
|
| 536 |
let id = parts.join("$"); |
| 537 |
id = id.replace(/^_/, "") || defaultName || "ref"; |
| 538 |
|
| 539 |
return this.generateUid(id.slice(0, 20)); |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* Generate a unique identifier based on a node. |
| 544 |
*/ |
| 545 |
|
| 546 |
generateUidIdentifierBasedOnNode(node: t.Node, defaultName?: string) { |
| 547 |
return identifier(this.generateUidBasedOnNode(node, defaultName)); |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* Determine whether evaluating the specific input `node` is a consequenceless reference. ie. |
| 552 |
* evaluating it wont result in potentially arbitrary code from being ran. The following are |
| 553 |
* allowed and determined not to cause side effects: |
| 554 |
* |
| 555 |
* - `this` expressions |
| 556 |
* - `super` expressions |
| 557 |
* - Bound identifiers |
| 558 |
*/ |
| 559 |
|
| 560 |
isStatic(node: t.Node): boolean { |
| 561 |
if (isThisExpression(node) || isSuper(node) || isTopicReference(node)) { |
| 562 |
return true; |
| 563 |
} |
| 564 |
|
| 565 |
if (isIdentifier(node)) { |
| 566 |
const binding = this.getBinding(node.name); |
| 567 |
if (binding) { |
| 568 |
return binding.constant; |
| 569 |
} else { |
| 570 |
return this.hasBinding(node.name); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
return false; |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* Possibly generate a memoised identifier if it is not static and has consequences. |
| 579 |
*/ |
| 580 |
|
| 581 |
maybeGenerateMemoised(node: t.Node, dontPush?: boolean) { |
| 582 |
if (this.isStatic(node)) { |
| 583 |
return null; |
| 584 |
} else { |
| 585 |
const id = this.generateUidIdentifierBasedOnNode(node); |
| 586 |
if (!dontPush) { |
| 587 |
this.push({ id }); |
| 588 |
return cloneNode(id); |
| 589 |
} |
| 590 |
return id; |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
checkBlockScopedCollisions( |
| 595 |
local: Binding, |
| 596 |
kind: BindingKind, |
| 597 |
name: string, |
| 598 |
id: any, |
| 599 |
) { |
| 600 |
// ignore parameters |
| 601 |
if (kind === "param") return; |
| 602 |
|
| 603 |
// Ignore existing binding if it's the name of the current function or |
| 604 |
// class expression |
| 605 |
if (local.kind === "local") return; |
| 606 |
|
| 607 |
const duplicate = |
| 608 |
// don't allow duplicate bindings to exist alongside |
| 609 |
kind === "let" || |
| 610 |
local.kind === "let" || |
| 611 |
local.kind === "const" || |
| 612 |
local.kind === "module" || |
| 613 |
// don't allow a local of param with a kind of let |
| 614 |
(local.kind === "param" && kind === "const"); |
| 615 |
|
| 616 |
if (duplicate) { |
| 617 |
throw this.hub.buildError( |
| 618 |
id, |
| 619 |
`Duplicate declaration "${name}"`, |
| 620 |
TypeError, |
| 621 |
); |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
rename( |
| 626 |
oldName: string, |
| 627 |
newName?: string, |
| 628 |
// prettier-ignore |
| 629 |
/* Babel 7 - block?: t.Pattern | t.Scopable */ |
| 630 |
) { |
| 631 |
const binding = this.getBinding(oldName); |
| 632 |
if (binding) { |
| 633 |
newName ||= this.generateUidIdentifier(oldName).name; |
| 634 |
const renamer = new Renamer(binding, oldName, newName); |
| 635 |
return process.env.BABEL_8_BREAKING |
| 636 |
? renamer.rename() |
| 637 |
: // @ts-expect-error: babel 7->8 |
| 638 |
renamer.rename(arguments[2]); |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
/** @deprecated Not used in our codebase */ |
| 643 |
_renameFromMap( |
| 644 |
map: Record<string | symbol, unknown>, |
| 645 |
oldName: string | symbol, |
| 646 |
newName: string | symbol, |
| 647 |
value: unknown, |
| 648 |
) { |
| 649 |
if (map[oldName]) { |
| 650 |
map[newName] = value; |
| 651 |
map[oldName] = null; |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
dump() { |
| 656 |
const sep = "-".repeat(60); |
| 657 |
console.log(sep); |
| 658 |
let scope: Scope = this; |
| 659 |
do { |
| 660 |
console.log("#", scope.block.type); |
| 661 |
for (const name of Object.keys(scope.bindings)) { |
| 662 |
const binding = scope.bindings[name]; |
| 663 |
console.log(" -", name, { |
| 664 |
constant: binding.constant, |
| 665 |
references: binding.references, |
| 666 |
violations: binding.constantViolations.length, |
| 667 |
kind: binding.kind, |
| 668 |
}); |
| 669 |
} |
| 670 |
} while ((scope = scope.parent)); |
| 671 |
console.log(sep); |
| 672 |
} |
| 673 |
|
| 674 |
// TODO: (Babel 8) Split i in two parameters, and use an object of flags |
| 675 |
toArray( |
| 676 |
node: t.Node, |
| 677 |
i?: number | boolean, |
| 678 |
arrayLikeIsIterable?: boolean | void, |
| 679 |
) { |
| 680 |
if (isIdentifier(node)) { |
| 681 |
const binding = this.getBinding(node.name); |
| 682 |
if (binding?.constant && binding.path.isGenericType("Array")) { |
| 683 |
return node; |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
if (isArrayExpression(node)) { |
| 688 |
return node; |
| 689 |
} |
| 690 |
|
| 691 |
if (isIdentifier(node, { name: "arguments" })) { |
| 692 |
return callExpression( |
| 693 |
memberExpression( |
| 694 |
memberExpression( |
| 695 |
memberExpression(identifier("Array"), identifier("prototype")), |
| 696 |
identifier("slice"), |
| 697 |
), |
| 698 |
identifier("call"), |
| 699 |
), |
| 700 |
[node], |
| 701 |
); |
| 702 |
} |
| 703 |
|
| 704 |
let helperName; |
| 705 |
const args = [node]; |
| 706 |
if (i === true) { |
| 707 |
// Used in array-spread to create an array. |
| 708 |
helperName = "toConsumableArray"; |
| 709 |
} else if (typeof i === "number") { |
| 710 |
args.push(numericLiteral(i)); |
| 711 |
|
| 712 |
// Used in array-rest to create an array from a subset of an iterable. |
| 713 |
helperName = "slicedToArray"; |
| 714 |
// TODO if (this.hub.isLoose("es6.forOf")) helperName += "-loose"; |
| 715 |
} else { |
| 716 |
// Used in array-rest to create an array |
| 717 |
helperName = "toArray"; |
| 718 |
} |
| 719 |
|
| 720 |
if (arrayLikeIsIterable) { |
| 721 |
args.unshift(this.hub.addHelper(helperName)); |
| 722 |
helperName = "maybeArrayLike"; |
| 723 |
} |
| 724 |
|
| 725 |
// @ts-expect-error todo(flow->ts): t.Node is not valid to use in args, function argument typeneeds to be clarified |
| 726 |
return callExpression(this.hub.addHelper(helperName), args); |
| 727 |
} |
| 728 |
|
| 729 |
hasLabel(name: string) { |
| 730 |
return !!this.getLabel(name); |
| 731 |
} |
| 732 |
|
| 733 |
getLabel(name: string) { |
| 734 |
return this.labels.get(name); |
| 735 |
} |
| 736 |
|
| 737 |
registerLabel(path: NodePath<t.LabeledStatement>) { |
| 738 |
this.labels.set(path.node.label.name, path); |
| 739 |
} |
| 740 |
|
| 741 |
registerDeclaration(path: NodePath) { |
| 742 |
if (path.isLabeledStatement()) { |
| 743 |
this.registerLabel(path); |
| 744 |
} else if (path.isFunctionDeclaration()) { |
| 745 |
this.registerBinding("hoisted", path.get("id"), path); |
| 746 |
} else if (path.isVariableDeclaration()) { |
| 747 |
const declarations = path.get("declarations"); |
| 748 |
const { kind } = path.node; |
| 749 |
for (const declar of declarations) { |
| 750 |
this.registerBinding(kind === "using" ? "const" : kind, declar); |
| 751 |
} |
| 752 |
} else if (path.isClassDeclaration()) { |
| 753 |
if (path.node.declare) return; |
| 754 |
this.registerBinding("let", path); |
| 755 |
} else if (path.isImportDeclaration()) { |
| 756 |
const isTypeDeclaration = |
| 757 |
path.node.importKind === "type" || path.node.importKind === "typeof"; |
| 758 |
const specifiers = path.get("specifiers"); |
| 759 |
for (const specifier of specifiers) { |
| 760 |
const isTypeSpecifier = |
| 761 |
isTypeDeclaration || |
| 762 |
(specifier.isImportSpecifier() && |
| 763 |
(specifier.node.importKind === "type" || |
| 764 |
specifier.node.importKind === "typeof")); |
| 765 |
|
| 766 |
this.registerBinding(isTypeSpecifier ? "unknown" : "module", specifier); |
| 767 |
} |
| 768 |
} else if (path.isExportDeclaration()) { |
| 769 |
// todo: improve babel-types |
| 770 |
const declar = path.get("declaration") as NodePath; |
| 771 |
if ( |
| 772 |
declar.isClassDeclaration() || |
| 773 |
declar.isFunctionDeclaration() || |
| 774 |
declar.isVariableDeclaration() |
| 775 |
) { |
| 776 |
this.registerDeclaration(declar); |
| 777 |
} |
| 778 |
} else { |
| 779 |
this.registerBinding("unknown", path); |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
buildUndefinedNode() { |
| 784 |
return unaryExpression("void", numericLiteral(0), true); |
| 785 |
} |
| 786 |
|
| 787 |
registerConstantViolation(path: NodePath) { |
| 788 |
const ids = path.getBindingIdentifiers(); |
| 789 |
for (const name of Object.keys(ids)) { |
| 790 |
const binding = this.getBinding(name); |
| 791 |
if (binding) binding.reassign(path); |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
registerBinding( |
| 796 |
kind: Binding["kind"], |
| 797 |
path: NodePath, |
| 798 |
bindingPath: NodePath = path, |
| 799 |
) { |
| 800 |
if (!kind) throw new ReferenceError("no `kind`"); |
| 801 |
|
| 802 |
if (path.isVariableDeclaration()) { |
| 803 |
const declarators: Array<NodePath> = path.get("declarations"); |
| 804 |
for (const declar of declarators) { |
| 805 |
this.registerBinding(kind, declar); |
| 806 |
} |
| 807 |
return; |
| 808 |
} |
| 809 |
|
| 810 |
const parent = this.getProgramParent(); |
| 811 |
const ids = path.getOuterBindingIdentifiers(true); |
| 812 |
|
| 813 |
for (const name of Object.keys(ids)) { |
| 814 |
parent.references[name] = true; |
| 815 |
|
| 816 |
for (const id of ids[name]) { |
| 817 |
const local = this.getOwnBinding(name); |
| 818 |
|
| 819 |
if (local) { |
| 820 |
// same identifier so continue safely as we're likely trying to register it |
| 821 |
// multiple times |
| 822 |
if (local.identifier === id) continue; |
| 823 |
|
| 824 |
this.checkBlockScopedCollisions(local, kind, name, id); |
| 825 |
} |
| 826 |
|
| 827 |
// A redeclaration of an existing variable is a modification |
| 828 |
if (local) { |
| 829 |
this.registerConstantViolation(bindingPath); |
| 830 |
} else { |
| 831 |
this.bindings[name] = new Binding({ |
| 832 |
identifier: id, |
| 833 |
scope: this, |
| 834 |
path: bindingPath, |
| 835 |
kind: kind, |
| 836 |
}); |
| 837 |
} |
| 838 |
} |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
addGlobal(node: t.Identifier | t.JSXIdentifier) { |
| 843 |
this.globals[node.name] = node; |
| 844 |
} |
| 845 |
|
| 846 |
hasUid(name: string): boolean { |
| 847 |
let scope: Scope = this; |
| 848 |
|
| 849 |
do { |
| 850 |
if (scope.uids[name]) return true; |
| 851 |
} while ((scope = scope.parent)); |
| 852 |
|
| 853 |
return false; |
| 854 |
} |
| 855 |
|
| 856 |
hasGlobal(name: string): boolean { |
| 857 |
let scope: Scope = this; |
| 858 |
|
| 859 |
do { |
| 860 |
if (scope.globals[name]) return true; |
| 861 |
} while ((scope = scope.parent)); |
| 862 |
|
| 863 |
return false; |
| 864 |
} |
| 865 |
|
| 866 |
hasReference(name: string): boolean { |
| 867 |
return !!this.getProgramParent().references[name]; |
| 868 |
} |
| 869 |
|
| 870 |
isPure(node: t.Node, constantsOnly?: boolean): boolean { |
| 871 |
if (isIdentifier(node)) { |
| 872 |
const binding = this.getBinding(node.name); |
| 873 |
if (!binding) return false; |
| 874 |
if (constantsOnly) return binding.constant; |
| 875 |
return true; |
| 876 |
} else if ( |
| 877 |
isThisExpression(node) || |
| 878 |
isMetaProperty(node) || |
| 879 |
isTopicReference(node) || |
| 880 |
isPrivateName(node) |
| 881 |
) { |
| 882 |
return true; |
| 883 |
} else if (isClass(node)) { |
| 884 |
if (node.superClass && !this.isPure(node.superClass, constantsOnly)) { |
| 885 |
return false; |
| 886 |
} |
| 887 |
if (node.decorators?.length > 0) { |
| 888 |
return false; |
| 889 |
} |
| 890 |
return this.isPure(node.body, constantsOnly); |
| 891 |
} else if (isClassBody(node)) { |
| 892 |
for (const method of node.body) { |
| 893 |
if (!this.isPure(method, constantsOnly)) return false; |
| 894 |
} |
| 895 |
return true; |
| 896 |
} else if (isBinary(node)) { |
| 897 |
return ( |
| 898 |
this.isPure(node.left, constantsOnly) && |
| 899 |
this.isPure(node.right, constantsOnly) |
| 900 |
); |
| 901 |
} else if (isArrayExpression(node) || isTupleExpression(node)) { |
| 902 |
for (const elem of node.elements) { |
| 903 |
if (elem !== null && !this.isPure(elem, constantsOnly)) return false; |
| 904 |
} |
| 905 |
return true; |
| 906 |
} else if (isObjectExpression(node) || isRecordExpression(node)) { |
| 907 |
for (const prop of node.properties) { |
| 908 |
if (!this.isPure(prop, constantsOnly)) return false; |
| 909 |
} |
| 910 |
return true; |
| 911 |
} else if (isMethod(node)) { |
| 912 |
if (node.computed && !this.isPure(node.key, constantsOnly)) return false; |
| 913 |
if (node.decorators?.length > 0) { |
| 914 |
return false; |
| 915 |
} |
| 916 |
return true; |
| 917 |
} else if (isProperty(node)) { |
| 918 |
// @ts-expect-error todo(flow->ts): computed in not present on private properties |
| 919 |
if (node.computed && !this.isPure(node.key, constantsOnly)) return false; |
| 920 |
if (node.decorators?.length > 0) { |
| 921 |
return false; |
| 922 |
} |
| 923 |
if (isObjectProperty(node) || node.static) { |
| 924 |
if (node.value !== null && !this.isPure(node.value, constantsOnly)) { |
| 925 |
return false; |
| 926 |
} |
| 927 |
} |
| 928 |
return true; |
| 929 |
} else if (isUnaryExpression(node)) { |
| 930 |
return this.isPure(node.argument, constantsOnly); |
| 931 |
} else if (isTaggedTemplateExpression(node)) { |
| 932 |
return ( |
| 933 |
matchesPattern(node.tag, "String.raw") && |
| 934 |
!this.hasBinding("String", true) && |
| 935 |
this.isPure(node.quasi, constantsOnly) |
| 936 |
); |
| 937 |
} else if (isTemplateLiteral(node)) { |
| 938 |
for (const expression of node.expressions) { |
| 939 |
if (!this.isPure(expression, constantsOnly)) return false; |
| 940 |
} |
| 941 |
return true; |
| 942 |
} else { |
| 943 |
return isPureish(node); |
| 944 |
} |
| 945 |
} |
| 946 |
|
| 947 |
/** |
| 948 |
* Set some arbitrary data on the current scope. |
| 949 |
*/ |
| 950 |
|
| 951 |
setData(key: string | symbol, val: any) { |
| 952 |
return (this.data[key] = val); |
| 953 |
} |
| 954 |
|
| 955 |
/** |
| 956 |
* Recursively walk up scope tree looking for the data `key`. |
| 957 |
*/ |
| 958 |
|
| 959 |
getData(key: string | symbol): any { |
| 960 |
let scope: Scope = this; |
| 961 |
do { |
| 962 |
const data = scope.data[key]; |
| 963 |
if (data != null) return data; |
| 964 |
} while ((scope = scope.parent)); |
| 965 |
} |
| 966 |
|
| 967 |
/** |
| 968 |
* Recursively walk up scope tree looking for the data `key` and if it exists, |
| 969 |
* remove it. |
| 970 |
*/ |
| 971 |
|
| 972 |
removeData(key: string) { |
| 973 |
let scope: Scope = this; |
| 974 |
do { |
| 975 |
const data = scope.data[key]; |
| 976 |
if (data != null) scope.data[key] = null; |
| 977 |
} while ((scope = scope.parent)); |
| 978 |
} |
| 979 |
|
| 980 |
init() { |
| 981 |
if (!this.inited) { |
| 982 |
this.inited = true; |
| 983 |
this.crawl(); |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
crawl() { |
| 988 |
const path = this.path; |
| 989 |
|
| 990 |
this.references = Object.create(null); |
| 991 |
this.bindings = Object.create(null); |
| 992 |
this.globals = Object.create(null); |
| 993 |
this.uids = Object.create(null); |
| 994 |
this.data = Object.create(null); |
| 995 |
|
| 996 |
const programParent = this.getProgramParent(); |
| 997 |
if (programParent.crawling) return; |
| 998 |
|
| 999 |
const state: CollectVisitorState = { |
| 1000 |
references: [], |
| 1001 |
constantViolations: [], |
| 1002 |
assignments: [], |
| 1003 |
}; |
| 1004 |
|
| 1005 |
this.crawling = true; |
| 1006 |
// traverse does not visit the root node, here we explicitly collect |
| 1007 |
// root node binding info when the root is not a Program. |
| 1008 |
if (path.type !== "Program" && collectorVisitor._exploded) { |
| 1009 |
// @ts-expect-error when collectorVisitor is exploded, `enter` always exists |
| 1010 |
for (const visit of collectorVisitor.enter) { |
| 1011 |
visit(path, state); |
| 1012 |
} |
| 1013 |
const typeVisitors = collectorVisitor[path.type]; |
| 1014 |
if (typeVisitors) { |
| 1015 |
// @ts-expect-error when collectorVisitor is exploded, `enter` always exists |
| 1016 |
for (const visit of typeVisitors.enter) { |
| 1017 |
visit(path, state); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
} |
| 1021 |
path.traverse(collectorVisitor, state); |
| 1022 |
this.crawling = false; |
| 1023 |
|
| 1024 |
// register assignments |
| 1025 |
for (const path of state.assignments) { |
| 1026 |
// register undeclared bindings as globals |
| 1027 |
const ids = path.getBindingIdentifiers(); |
| 1028 |
for (const name of Object.keys(ids)) { |
| 1029 |
if (path.scope.getBinding(name)) continue; |
| 1030 |
programParent.addGlobal(ids[name]); |
| 1031 |
} |
| 1032 |
|
| 1033 |
// register as constant violation |
| 1034 |
path.scope.registerConstantViolation(path); |
| 1035 |
} |
| 1036 |
|
| 1037 |
// register references |
| 1038 |
for (const ref of state.references) { |
| 1039 |
const binding = ref.scope.getBinding(ref.node.name); |
| 1040 |
if (binding) { |
| 1041 |
binding.reference(ref); |
| 1042 |
} else { |
| 1043 |
programParent.addGlobal(ref.node); |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
// register constant violations |
| 1048 |
for (const path of state.constantViolations) { |
| 1049 |
path.scope.registerConstantViolation(path); |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
push(opts: { |
| 1054 |
id: t.LVal; |
| 1055 |
init?: t.Expression; |
| 1056 |
unique?: boolean; |
| 1057 |
_blockHoist?: number | undefined; |
| 1058 |
kind?: "var" | "let" | "const"; |
| 1059 |
}) { |
| 1060 |
let path = this.path; |
| 1061 |
|
| 1062 |
if (path.isPattern()) { |
| 1063 |
path = this.getPatternParent().path; |
| 1064 |
} else if (!path.isBlockStatement() && !path.isProgram()) { |
| 1065 |
path = this.getBlockParent().path; |
| 1066 |
} |
| 1067 |
|
| 1068 |
if (path.isSwitchStatement()) { |
| 1069 |
path = (this.getFunctionParent() || this.getProgramParent()).path; |
| 1070 |
} |
| 1071 |
|
| 1072 |
if (path.isLoop() || path.isCatchClause() || path.isFunction()) { |
| 1073 |
// @ts-expect-error TS can not infer NodePath<Loop> | NodePath<CatchClause> as NodePath<Loop | CatchClause> |
| 1074 |
path.ensureBlock(); |
| 1075 |
// @ts-expect-error todo(flow->ts): improve types |
| 1076 |
path = path.get("body"); |
| 1077 |
} |
| 1078 |
|
| 1079 |
const unique = opts.unique; |
| 1080 |
const kind = opts.kind || "var"; |
| 1081 |
const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist; |
| 1082 |
|
| 1083 |
const dataKey = `declaration:${kind}:${blockHoist}`; |
| 1084 |
let declarPath = !unique && path.getData(dataKey); |
| 1085 |
|
| 1086 |
if (!declarPath) { |
| 1087 |
const declar = variableDeclaration(kind, []); |
| 1088 |
// @ts-expect-error todo(flow->ts): avoid modifying nodes |
| 1089 |
declar._blockHoist = blockHoist; |
| 1090 |
|
| 1091 |
[declarPath] = (path as NodePath<t.BlockStatement>).unshiftContainer( |
| 1092 |
"body", |
| 1093 |
[declar], |
| 1094 |
); |
| 1095 |
if (!unique) path.setData(dataKey, declarPath); |
| 1096 |
} |
| 1097 |
|
| 1098 |
const declarator = variableDeclarator(opts.id, opts.init); |
| 1099 |
const len = declarPath.node.declarations.push(declarator); |
| 1100 |
path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]); |
| 1101 |
} |
| 1102 |
|
| 1103 |
/** |
| 1104 |
* Walk up to the top of the scope tree and get the `Program`. |
| 1105 |
*/ |
| 1106 |
|
| 1107 |
getProgramParent() { |
| 1108 |
let scope: Scope = this; |
| 1109 |
do { |
| 1110 |
if (scope.path.isProgram()) { |
| 1111 |
return scope; |
| 1112 |
} |
| 1113 |
} while ((scope = scope.parent)); |
| 1114 |
throw new Error("Couldn't find a Program"); |
| 1115 |
} |
| 1116 |
|
| 1117 |
/** |
| 1118 |
* Walk up the scope tree until we hit either a Function or return null. |
| 1119 |
*/ |
| 1120 |
|
| 1121 |
getFunctionParent(): Scope | null { |
| 1122 |
let scope: Scope = this; |
| 1123 |
do { |
| 1124 |
if (scope.path.isFunctionParent()) { |
| 1125 |
return scope; |
| 1126 |
} |
| 1127 |
} while ((scope = scope.parent)); |
| 1128 |
return null; |
| 1129 |
} |
| 1130 |
|
| 1131 |
/** |
| 1132 |
* Walk up the scope tree until we hit either a BlockStatement/Loop/Program/Function/Switch or reach the |
| 1133 |
* very top and hit Program. |
| 1134 |
*/ |
| 1135 |
|
| 1136 |
getBlockParent() { |
| 1137 |
let scope: Scope = this; |
| 1138 |
do { |
| 1139 |
if (scope.path.isBlockParent()) { |
| 1140 |
return scope; |
| 1141 |
} |
| 1142 |
} while ((scope = scope.parent)); |
| 1143 |
throw new Error( |
| 1144 |
"We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...", |
| 1145 |
); |
| 1146 |
} |
| 1147 |
|
| 1148 |
/** |
| 1149 |
* Walk up from a pattern scope (function param initializer) until we hit a non-pattern scope, |
| 1150 |
* then returns its block parent |
| 1151 |
* @returns An ancestry scope whose path is a block parent |
| 1152 |
*/ |
| 1153 |
getPatternParent() { |
| 1154 |
let scope: Scope = this; |
| 1155 |
do { |
| 1156 |
if (!scope.path.isPattern()) { |
| 1157 |
return scope.getBlockParent(); |
| 1158 |
} |
| 1159 |
} while ((scope = scope.parent.parent)); |
| 1160 |
throw new Error( |
| 1161 |
"We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...", |
| 1162 |
); |
| 1163 |
} |
| 1164 |
|
| 1165 |
/** |
| 1166 |
* Walks the scope tree and gathers **all** bindings. |
| 1167 |
*/ |
| 1168 |
|
| 1169 |
getAllBindings(): Record<string, Binding> { |
| 1170 |
const ids = Object.create(null); |
| 1171 |
|
| 1172 |
let scope: Scope = this; |
| 1173 |
do { |
| 1174 |
for (const key of Object.keys(scope.bindings)) { |
| 1175 |
if (key in ids === false) { |
| 1176 |
ids[key] = scope.bindings[key]; |
| 1177 |
} |
| 1178 |
} |
| 1179 |
scope = scope.parent; |
| 1180 |
} while (scope); |
| 1181 |
|
| 1182 |
return ids; |
| 1183 |
} |
| 1184 |
|
| 1185 |
/** |
| 1186 |
* Walks the scope tree and gathers all declarations of `kind`. |
| 1187 |
*/ |
| 1188 |
|
| 1189 |
getAllBindingsOfKind(...kinds: string[]): Record<string, Binding> { |
| 1190 |
const ids = Object.create(null); |
| 1191 |
|
| 1192 |
for (const kind of kinds) { |
| 1193 |
let scope: Scope = this; |
| 1194 |
do { |
| 1195 |
for (const name of Object.keys(scope.bindings)) { |
| 1196 |
const binding = scope.bindings[name]; |
| 1197 |
if (binding.kind === kind) ids[name] = binding; |
| 1198 |
} |
| 1199 |
scope = scope.parent; |
| 1200 |
} while (scope); |
| 1201 |
} |
| 1202 |
|
| 1203 |
return ids; |
| 1204 |
} |
| 1205 |
|
| 1206 |
bindingIdentifierEquals(name: string, node: t.Node): boolean { |
| 1207 |
return this.getBindingIdentifier(name) === node; |
| 1208 |
} |
| 1209 |
|
| 1210 |
getBinding(name: string): Binding | undefined { |
| 1211 |
let scope: Scope = this; |
| 1212 |
let previousPath; |
| 1213 |
|
| 1214 |
do { |
| 1215 |
const binding = scope.getOwnBinding(name); |
| 1216 |
if (binding) { |
| 1217 |
// Check if a pattern is a part of parameter expressions. |
| 1218 |
// Note: for performance reason we skip checking previousPath.parentPath.isFunction() |
| 1219 |
// because `scope.path` is validated as scope in packages/babel-types/src/validators/isScope.js |
| 1220 |
// That is, if a scope path is pattern, its parent must be Function/CatchClause |
| 1221 |
|
| 1222 |
// Spec 9.2.10.28: The closure created by this expression should not have visibility of |
| 1223 |
// declarations in the function body. If the binding is not a `param`-kind (as function parameters) |
| 1224 |
// or `local`-kind (as id in function expression), |
| 1225 |
// then it must be defined inside the function body, thus it should be skipped |
| 1226 |
if ( |
| 1227 |
previousPath?.isPattern() && |
| 1228 |
binding.kind !== "param" && |
| 1229 |
binding.kind !== "local" |
| 1230 |
) { |
| 1231 |
// do nothing |
| 1232 |
} else { |
| 1233 |
return binding; |
| 1234 |
} |
| 1235 |
} else if ( |
| 1236 |
!binding && |
| 1237 |
name === "arguments" && |
| 1238 |
scope.path.isFunction() && |
| 1239 |
!scope.path.isArrowFunctionExpression() |
| 1240 |
) { |
| 1241 |
break; |
| 1242 |
} |
| 1243 |
previousPath = scope.path; |
| 1244 |
} while ((scope = scope.parent)); |
| 1245 |
} |
| 1246 |
|
| 1247 |
getOwnBinding(name: string): Binding | undefined { |
| 1248 |
return this.bindings[name]; |
| 1249 |
} |
| 1250 |
|
| 1251 |
// todo: return probably can be undefined… |
| 1252 |
getBindingIdentifier(name: string): t.Identifier { |
| 1253 |
return this.getBinding(name)?.identifier; |
| 1254 |
} |
| 1255 |
|
| 1256 |
// todo: flow->ts return probably can be undefined |
| 1257 |
getOwnBindingIdentifier(name: string): t.Identifier { |
| 1258 |
const binding = this.bindings[name]; |
| 1259 |
return binding?.identifier; |
| 1260 |
} |
| 1261 |
|
| 1262 |
hasOwnBinding(name: string) { |
| 1263 |
return !!this.getOwnBinding(name); |
| 1264 |
} |
| 1265 |
|
| 1266 |
// By default, we consider generated UIDs as bindings. |
| 1267 |
// This is because they are almost always used to declare variables, |
| 1268 |
// and since the scope isn't always up-to-date it's better to assume that |
| 1269 |
// there is a variable with that name. The `noUids` option can be used to |
| 1270 |
// turn off this behavior, for example if you know that the generate UID |
| 1271 |
// was used to declare a variable in a different scope. |
| 1272 |
hasBinding( |
| 1273 |
name: string, |
| 1274 |
opts?: boolean | { noGlobals?: boolean; noUids?: boolean }, |
| 1275 |
) { |
| 1276 |
if (!name) return false; |
| 1277 |
if (this.hasOwnBinding(name)) return true; |
| 1278 |
{ |
| 1279 |
// TODO: Only accept the object form. |
| 1280 |
if (typeof opts === "boolean") opts = { noGlobals: opts }; |
| 1281 |
} |
| 1282 |
if (this.parentHasBinding(name, opts)) return true; |
| 1283 |
if (!opts?.noUids && this.hasUid(name)) return true; |
| 1284 |
if (!opts?.noGlobals && Scope.globals.includes(name)) return true; |
| 1285 |
if (!opts?.noGlobals && Scope.contextVariables.includes(name)) return true; |
| 1286 |
return false; |
| 1287 |
} |
| 1288 |
|
| 1289 |
parentHasBinding( |
| 1290 |
name: string, |
| 1291 |
opts?: { noGlobals?: boolean; noUids?: boolean }, |
| 1292 |
) { |
| 1293 |
return this.parent?.hasBinding(name, opts); |
| 1294 |
} |
| 1295 |
|
| 1296 |
/** |
| 1297 |
* Move a binding of `name` to another `scope`. |
| 1298 |
*/ |
| 1299 |
|
| 1300 |
moveBindingTo(name: string, scope: Scope) { |
| 1301 |
const info = this.getBinding(name); |
| 1302 |
if (info) { |
| 1303 |
info.scope.removeOwnBinding(name); |
| 1304 |
info.scope = scope; |
| 1305 |
scope.bindings[name] = info; |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
removeOwnBinding(name: string) { |
| 1310 |
delete this.bindings[name]; |
| 1311 |
} |
| 1312 |
|
| 1313 |
removeBinding(name: string) { |
| 1314 |
// clear literal binding |
| 1315 |
this.getBinding(name)?.scope.removeOwnBinding(name); |
| 1316 |
|
| 1317 |
// clear uids with this name - https://github.com/babel/babel/issues/2101 |
| 1318 |
let scope: Scope = this; |
| 1319 |
do { |
| 1320 |
if (scope.uids[name]) { |
| 1321 |
scope.uids[name] = false; |
| 1322 |
} |
| 1323 |
} while ((scope = scope.parent)); |
| 1324 |
} |
| 1325 |
} |