index.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _renamer = require("./lib/renamer");
  7. var _index = require("../index");
  8. var _binding = require("./binding");
  9. var _globals = require("globals");
  10. var _t = require("@babel/types");
  11. var _cache = require("../cache");
  12. const {
  13. NOT_LOCAL_BINDING,
  14. callExpression,
  15. cloneNode,
  16. getBindingIdentifiers,
  17. identifier,
  18. isArrayExpression,
  19. isBinary,
  20. isClass,
  21. isClassBody,
  22. isClassDeclaration,
  23. isExportAllDeclaration,
  24. isExportDefaultDeclaration,
  25. isExportNamedDeclaration,
  26. isFunctionDeclaration,
  27. isIdentifier,
  28. isImportDeclaration,
  29. isLiteral,
  30. isMethod,
  31. isModuleSpecifier,
  32. isNullLiteral,
  33. isObjectExpression,
  34. isProperty,
  35. isPureish,
  36. isRegExpLiteral,
  37. isSuper,
  38. isTaggedTemplateExpression,
  39. isTemplateLiteral,
  40. isThisExpression,
  41. isUnaryExpression,
  42. isVariableDeclaration,
  43. matchesPattern,
  44. memberExpression,
  45. numericLiteral,
  46. toIdentifier,
  47. unaryExpression,
  48. variableDeclaration,
  49. variableDeclarator,
  50. isRecordExpression,
  51. isTupleExpression,
  52. isObjectProperty,
  53. isTopicReference,
  54. isMetaProperty,
  55. isPrivateName,
  56. isExportDeclaration
  57. } = _t;
  58. function gatherNodeParts(node, parts) {
  59. switch (node == null ? void 0 : node.type) {
  60. default:
  61. if (isImportDeclaration(node) || isExportDeclaration(node)) {
  62. if ((isExportAllDeclaration(node) || isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.source) {
  63. gatherNodeParts(node.source, parts);
  64. } else if ((isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.specifiers && node.specifiers.length) {
  65. for (const e of node.specifiers) gatherNodeParts(e, parts);
  66. } else if ((isExportDefaultDeclaration(node) || isExportNamedDeclaration(node)) && node.declaration) {
  67. gatherNodeParts(node.declaration, parts);
  68. }
  69. } else if (isModuleSpecifier(node)) {
  70. gatherNodeParts(node.local, parts);
  71. } else if (isLiteral(node) && !isNullLiteral(node) && !isRegExpLiteral(node) && !isTemplateLiteral(node)) {
  72. parts.push(node.value);
  73. }
  74. break;
  75. case "MemberExpression":
  76. case "OptionalMemberExpression":
  77. case "JSXMemberExpression":
  78. gatherNodeParts(node.object, parts);
  79. gatherNodeParts(node.property, parts);
  80. break;
  81. case "Identifier":
  82. case "JSXIdentifier":
  83. parts.push(node.name);
  84. break;
  85. case "CallExpression":
  86. case "OptionalCallExpression":
  87. case "NewExpression":
  88. gatherNodeParts(node.callee, parts);
  89. break;
  90. case "ObjectExpression":
  91. case "ObjectPattern":
  92. for (const e of node.properties) {
  93. gatherNodeParts(e, parts);
  94. }
  95. break;
  96. case "SpreadElement":
  97. case "RestElement":
  98. gatherNodeParts(node.argument, parts);
  99. break;
  100. case "ObjectProperty":
  101. case "ObjectMethod":
  102. case "ClassProperty":
  103. case "ClassMethod":
  104. case "ClassPrivateProperty":
  105. case "ClassPrivateMethod":
  106. gatherNodeParts(node.key, parts);
  107. break;
  108. case "ThisExpression":
  109. parts.push("this");
  110. break;
  111. case "Super":
  112. parts.push("super");
  113. break;
  114. case "Import":
  115. parts.push("import");
  116. break;
  117. case "DoExpression":
  118. parts.push("do");
  119. break;
  120. case "YieldExpression":
  121. parts.push("yield");
  122. gatherNodeParts(node.argument, parts);
  123. break;
  124. case "AwaitExpression":
  125. parts.push("await");
  126. gatherNodeParts(node.argument, parts);
  127. break;
  128. case "AssignmentExpression":
  129. gatherNodeParts(node.left, parts);
  130. break;
  131. case "VariableDeclarator":
  132. gatherNodeParts(node.id, parts);
  133. break;
  134. case "FunctionExpression":
  135. case "FunctionDeclaration":
  136. case "ClassExpression":
  137. case "ClassDeclaration":
  138. gatherNodeParts(node.id, parts);
  139. break;
  140. case "PrivateName":
  141. gatherNodeParts(node.id, parts);
  142. break;
  143. case "ParenthesizedExpression":
  144. gatherNodeParts(node.expression, parts);
  145. break;
  146. case "UnaryExpression":
  147. case "UpdateExpression":
  148. gatherNodeParts(node.argument, parts);
  149. break;
  150. case "MetaProperty":
  151. gatherNodeParts(node.meta, parts);
  152. gatherNodeParts(node.property, parts);
  153. break;
  154. case "JSXElement":
  155. gatherNodeParts(node.openingElement, parts);
  156. break;
  157. case "JSXOpeningElement":
  158. gatherNodeParts(node.name, parts);
  159. break;
  160. case "JSXFragment":
  161. gatherNodeParts(node.openingFragment, parts);
  162. break;
  163. case "JSXOpeningFragment":
  164. parts.push("Fragment");
  165. break;
  166. case "JSXNamespacedName":
  167. gatherNodeParts(node.namespace, parts);
  168. gatherNodeParts(node.name, parts);
  169. break;
  170. }
  171. }
  172. const collectorVisitor = {
  173. ForStatement(path) {
  174. const declar = path.get("init");
  175. if (declar.isVar()) {
  176. const {
  177. scope
  178. } = path;
  179. const parentScope = scope.getFunctionParent() || scope.getProgramParent();
  180. parentScope.registerBinding("var", declar);
  181. }
  182. },
  183. Declaration(path) {
  184. if (path.isBlockScoped()) return;
  185. if (path.isImportDeclaration()) return;
  186. if (path.isExportDeclaration()) return;
  187. const parent = path.scope.getFunctionParent() || path.scope.getProgramParent();
  188. parent.registerDeclaration(path);
  189. },
  190. ImportDeclaration(path) {
  191. const parent = path.scope.getBlockParent();
  192. parent.registerDeclaration(path);
  193. },
  194. ReferencedIdentifier(path, state) {
  195. state.references.push(path);
  196. },
  197. ForXStatement(path, state) {
  198. const left = path.get("left");
  199. if (left.isPattern() || left.isIdentifier()) {
  200. state.constantViolations.push(path);
  201. }
  202. else if (left.isVar()) {
  203. const {
  204. scope
  205. } = path;
  206. const parentScope = scope.getFunctionParent() || scope.getProgramParent();
  207. parentScope.registerBinding("var", left);
  208. }
  209. },
  210. ExportDeclaration: {
  211. exit(path) {
  212. const {
  213. node,
  214. scope
  215. } = path;
  216. if (isExportAllDeclaration(node)) return;
  217. const declar = node.declaration;
  218. if (isClassDeclaration(declar) || isFunctionDeclaration(declar)) {
  219. const id = declar.id;
  220. if (!id) return;
  221. const binding = scope.getBinding(id.name);
  222. binding == null ? void 0 : binding.reference(path);
  223. } else if (isVariableDeclaration(declar)) {
  224. for (const decl of declar.declarations) {
  225. for (const name of Object.keys(getBindingIdentifiers(decl))) {
  226. const binding = scope.getBinding(name);
  227. binding == null ? void 0 : binding.reference(path);
  228. }
  229. }
  230. }
  231. }
  232. },
  233. LabeledStatement(path) {
  234. path.scope.getBlockParent().registerDeclaration(path);
  235. },
  236. AssignmentExpression(path, state) {
  237. state.assignments.push(path);
  238. },
  239. UpdateExpression(path, state) {
  240. state.constantViolations.push(path);
  241. },
  242. UnaryExpression(path, state) {
  243. if (path.node.operator === "delete") {
  244. state.constantViolations.push(path);
  245. }
  246. },
  247. BlockScoped(path) {
  248. let scope = path.scope;
  249. if (scope.path === path) scope = scope.parent;
  250. const parent = scope.getBlockParent();
  251. parent.registerDeclaration(path);
  252. if (path.isClassDeclaration() && path.node.id) {
  253. const id = path.node.id;
  254. const name = id.name;
  255. path.scope.bindings[name] = path.scope.parent.getBinding(name);
  256. }
  257. },
  258. CatchClause(path) {
  259. path.scope.registerBinding("let", path);
  260. },
  261. Function(path) {
  262. const params = path.get("params");
  263. for (const param of params) {
  264. path.scope.registerBinding("param", param);
  265. }
  266. if (path.isFunctionExpression() && path.has("id") &&
  267. !path.get("id").node[NOT_LOCAL_BINDING]) {
  268. path.scope.registerBinding("local", path.get("id"), path);
  269. }
  270. },
  271. ClassExpression(path) {
  272. if (path.has("id") &&
  273. !path.get("id").node[NOT_LOCAL_BINDING]) {
  274. path.scope.registerBinding("local", path);
  275. }
  276. }
  277. };
  278. let uid = 0;
  279. class Scope {
  280. constructor(path) {
  281. this.uid = void 0;
  282. this.path = void 0;
  283. this.block = void 0;
  284. this.labels = void 0;
  285. this.inited = void 0;
  286. this.bindings = void 0;
  287. this.references = void 0;
  288. this.globals = void 0;
  289. this.uids = void 0;
  290. this.data = void 0;
  291. this.crawling = void 0;
  292. const {
  293. node
  294. } = path;
  295. const cached = _cache.scope.get(node);
  296. if ((cached == null ? void 0 : cached.path) === path) {
  297. return cached;
  298. }
  299. _cache.scope.set(node, this);
  300. this.uid = uid++;
  301. this.block = node;
  302. this.path = path;
  303. this.labels = new Map();
  304. this.inited = false;
  305. }
  306. get parent() {
  307. var _parent;
  308. let parent,
  309. path = this.path;
  310. do {
  311. const shouldSkip = path.key === "key" || path.listKey === "decorators";
  312. path = path.parentPath;
  313. if (shouldSkip && path.isMethod()) path = path.parentPath;
  314. if (path && path.isScope()) parent = path;
  315. } while (path && !parent);
  316. return (_parent = parent) == null ? void 0 : _parent.scope;
  317. }
  318. get parentBlock() {
  319. return this.path.parent;
  320. }
  321. get hub() {
  322. return this.path.hub;
  323. }
  324. traverse(node, opts, state) {
  325. (0, _index.default)(node, opts, this, state, this.path);
  326. }
  327. generateDeclaredUidIdentifier(name) {
  328. const id = this.generateUidIdentifier(name);
  329. this.push({
  330. id
  331. });
  332. return cloneNode(id);
  333. }
  334. generateUidIdentifier(name) {
  335. return identifier(this.generateUid(name));
  336. }
  337. generateUid(name = "temp") {
  338. name = toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
  339. let uid;
  340. let i = 1;
  341. do {
  342. uid = this._generateUid(name, i);
  343. i++;
  344. } while (this.hasLabel(uid) || this.hasBinding(uid) || this.hasGlobal(uid) || this.hasReference(uid));
  345. const program = this.getProgramParent();
  346. program.references[uid] = true;
  347. program.uids[uid] = true;
  348. return uid;
  349. }
  350. _generateUid(name, i) {
  351. let id = name;
  352. if (i > 1) id += i;
  353. return `_${id}`;
  354. }
  355. generateUidBasedOnNode(node, defaultName) {
  356. const parts = [];
  357. gatherNodeParts(node, parts);
  358. let id = parts.join("$");
  359. id = id.replace(/^_/, "") || defaultName || "ref";
  360. return this.generateUid(id.slice(0, 20));
  361. }
  362. generateUidIdentifierBasedOnNode(node, defaultName) {
  363. return identifier(this.generateUidBasedOnNode(node, defaultName));
  364. }
  365. isStatic(node) {
  366. if (isThisExpression(node) || isSuper(node) || isTopicReference(node)) {
  367. return true;
  368. }
  369. if (isIdentifier(node)) {
  370. const binding = this.getBinding(node.name);
  371. if (binding) {
  372. return binding.constant;
  373. } else {
  374. return this.hasBinding(node.name);
  375. }
  376. }
  377. return false;
  378. }
  379. maybeGenerateMemoised(node, dontPush) {
  380. if (this.isStatic(node)) {
  381. return null;
  382. } else {
  383. const id = this.generateUidIdentifierBasedOnNode(node);
  384. if (!dontPush) {
  385. this.push({
  386. id
  387. });
  388. return cloneNode(id);
  389. }
  390. return id;
  391. }
  392. }
  393. checkBlockScopedCollisions(local, kind, name, id) {
  394. if (kind === "param") return;
  395. if (local.kind === "local") return;
  396. const duplicate =
  397. kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" ||
  398. local.kind === "param" && kind === "const";
  399. if (duplicate) {
  400. throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
  401. }
  402. }
  403. rename(oldName, newName, block) {
  404. const binding = this.getBinding(oldName);
  405. if (binding) {
  406. newName = newName || this.generateUidIdentifier(oldName).name;
  407. return new _renamer.default(binding, oldName, newName).rename(block);
  408. }
  409. }
  410. _renameFromMap(map, oldName, newName, value) {
  411. if (map[oldName]) {
  412. map[newName] = value;
  413. map[oldName] = null;
  414. }
  415. }
  416. dump() {
  417. const sep = "-".repeat(60);
  418. console.log(sep);
  419. let scope = this;
  420. do {
  421. console.log("#", scope.block.type);
  422. for (const name of Object.keys(scope.bindings)) {
  423. const binding = scope.bindings[name];
  424. console.log(" -", name, {
  425. constant: binding.constant,
  426. references: binding.references,
  427. violations: binding.constantViolations.length,
  428. kind: binding.kind
  429. });
  430. }
  431. } while (scope = scope.parent);
  432. console.log(sep);
  433. }
  434. toArray(node, i, arrayLikeIsIterable) {
  435. if (isIdentifier(node)) {
  436. const binding = this.getBinding(node.name);
  437. if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
  438. return node;
  439. }
  440. }
  441. if (isArrayExpression(node)) {
  442. return node;
  443. }
  444. if (isIdentifier(node, {
  445. name: "arguments"
  446. })) {
  447. return callExpression(memberExpression(memberExpression(memberExpression(identifier("Array"), identifier("prototype")), identifier("slice")), identifier("call")), [node]);
  448. }
  449. let helperName;
  450. const args = [node];
  451. if (i === true) {
  452. helperName = "toConsumableArray";
  453. } else if (typeof i === "number") {
  454. args.push(numericLiteral(i));
  455. helperName = "slicedToArray";
  456. } else {
  457. helperName = "toArray";
  458. }
  459. if (arrayLikeIsIterable) {
  460. args.unshift(this.hub.addHelper(helperName));
  461. helperName = "maybeArrayLike";
  462. }
  463. return callExpression(this.hub.addHelper(helperName), args);
  464. }
  465. hasLabel(name) {
  466. return !!this.getLabel(name);
  467. }
  468. getLabel(name) {
  469. return this.labels.get(name);
  470. }
  471. registerLabel(path) {
  472. this.labels.set(path.node.label.name, path);
  473. }
  474. registerDeclaration(path) {
  475. if (path.isLabeledStatement()) {
  476. this.registerLabel(path);
  477. } else if (path.isFunctionDeclaration()) {
  478. this.registerBinding("hoisted", path.get("id"), path);
  479. } else if (path.isVariableDeclaration()) {
  480. const declarations = path.get("declarations");
  481. const {
  482. kind
  483. } = path.node;
  484. for (const declar of declarations) {
  485. this.registerBinding(kind === "using" ? "const" : kind, declar);
  486. }
  487. } else if (path.isClassDeclaration()) {
  488. if (path.node.declare) return;
  489. this.registerBinding("let", path);
  490. } else if (path.isImportDeclaration()) {
  491. const isTypeDeclaration = path.node.importKind === "type" || path.node.importKind === "typeof";
  492. const specifiers = path.get("specifiers");
  493. for (const specifier of specifiers) {
  494. const isTypeSpecifier = isTypeDeclaration || specifier.isImportSpecifier() && (specifier.node.importKind === "type" || specifier.node.importKind === "typeof");
  495. this.registerBinding(isTypeSpecifier ? "unknown" : "module", specifier);
  496. }
  497. } else if (path.isExportDeclaration()) {
  498. const declar = path.get("declaration");
  499. if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) {
  500. this.registerDeclaration(declar);
  501. }
  502. } else {
  503. this.registerBinding("unknown", path);
  504. }
  505. }
  506. buildUndefinedNode() {
  507. return unaryExpression("void", numericLiteral(0), true);
  508. }
  509. registerConstantViolation(path) {
  510. const ids = path.getBindingIdentifiers();
  511. for (const name of Object.keys(ids)) {
  512. const binding = this.getBinding(name);
  513. if (binding) binding.reassign(path);
  514. }
  515. }
  516. registerBinding(kind, path, bindingPath = path) {
  517. if (!kind) throw new ReferenceError("no `kind`");
  518. if (path.isVariableDeclaration()) {
  519. const declarators = path.get("declarations");
  520. for (const declar of declarators) {
  521. this.registerBinding(kind, declar);
  522. }
  523. return;
  524. }
  525. const parent = this.getProgramParent();
  526. const ids = path.getOuterBindingIdentifiers(true);
  527. for (const name of Object.keys(ids)) {
  528. parent.references[name] = true;
  529. for (const id of ids[name]) {
  530. const local = this.getOwnBinding(name);
  531. if (local) {
  532. if (local.identifier === id) continue;
  533. this.checkBlockScopedCollisions(local, kind, name, id);
  534. }
  535. if (local) {
  536. this.registerConstantViolation(bindingPath);
  537. } else {
  538. this.bindings[name] = new _binding.default({
  539. identifier: id,
  540. scope: this,
  541. path: bindingPath,
  542. kind: kind
  543. });
  544. }
  545. }
  546. }
  547. }
  548. addGlobal(node) {
  549. this.globals[node.name] = node;
  550. }
  551. hasUid(name) {
  552. let scope = this;
  553. do {
  554. if (scope.uids[name]) return true;
  555. } while (scope = scope.parent);
  556. return false;
  557. }
  558. hasGlobal(name) {
  559. let scope = this;
  560. do {
  561. if (scope.globals[name]) return true;
  562. } while (scope = scope.parent);
  563. return false;
  564. }
  565. hasReference(name) {
  566. return !!this.getProgramParent().references[name];
  567. }
  568. isPure(node, constantsOnly) {
  569. if (isIdentifier(node)) {
  570. const binding = this.getBinding(node.name);
  571. if (!binding) return false;
  572. if (constantsOnly) return binding.constant;
  573. return true;
  574. } else if (isThisExpression(node) || isMetaProperty(node) || isTopicReference(node) || isPrivateName(node)) {
  575. return true;
  576. } else if (isClass(node)) {
  577. var _node$decorators;
  578. if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
  579. return false;
  580. }
  581. if (((_node$decorators = node.decorators) == null ? void 0 : _node$decorators.length) > 0) {
  582. return false;
  583. }
  584. return this.isPure(node.body, constantsOnly);
  585. } else if (isClassBody(node)) {
  586. for (const method of node.body) {
  587. if (!this.isPure(method, constantsOnly)) return false;
  588. }
  589. return true;
  590. } else if (isBinary(node)) {
  591. return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
  592. } else if (isArrayExpression(node) || isTupleExpression(node)) {
  593. for (const elem of node.elements) {
  594. if (elem !== null && !this.isPure(elem, constantsOnly)) return false;
  595. }
  596. return true;
  597. } else if (isObjectExpression(node) || isRecordExpression(node)) {
  598. for (const prop of node.properties) {
  599. if (!this.isPure(prop, constantsOnly)) return false;
  600. }
  601. return true;
  602. } else if (isMethod(node)) {
  603. var _node$decorators2;
  604. if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
  605. if (((_node$decorators2 = node.decorators) == null ? void 0 : _node$decorators2.length) > 0) {
  606. return false;
  607. }
  608. return true;
  609. } else if (isProperty(node)) {
  610. var _node$decorators3;
  611. if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
  612. if (((_node$decorators3 = node.decorators) == null ? void 0 : _node$decorators3.length) > 0) {
  613. return false;
  614. }
  615. if (isObjectProperty(node) || node.static) {
  616. if (node.value !== null && !this.isPure(node.value, constantsOnly)) {
  617. return false;
  618. }
  619. }
  620. return true;
  621. } else if (isUnaryExpression(node)) {
  622. return this.isPure(node.argument, constantsOnly);
  623. } else if (isTaggedTemplateExpression(node)) {
  624. return matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
  625. } else if (isTemplateLiteral(node)) {
  626. for (const expression of node.expressions) {
  627. if (!this.isPure(expression, constantsOnly)) return false;
  628. }
  629. return true;
  630. } else {
  631. return isPureish(node);
  632. }
  633. }
  634. setData(key, val) {
  635. return this.data[key] = val;
  636. }
  637. getData(key) {
  638. let scope = this;
  639. do {
  640. const data = scope.data[key];
  641. if (data != null) return data;
  642. } while (scope = scope.parent);
  643. }
  644. removeData(key) {
  645. let scope = this;
  646. do {
  647. const data = scope.data[key];
  648. if (data != null) scope.data[key] = null;
  649. } while (scope = scope.parent);
  650. }
  651. init() {
  652. if (!this.inited) {
  653. this.inited = true;
  654. this.crawl();
  655. }
  656. }
  657. crawl() {
  658. const path = this.path;
  659. this.references = Object.create(null);
  660. this.bindings = Object.create(null);
  661. this.globals = Object.create(null);
  662. this.uids = Object.create(null);
  663. this.data = Object.create(null);
  664. const programParent = this.getProgramParent();
  665. if (programParent.crawling) return;
  666. const state = {
  667. references: [],
  668. constantViolations: [],
  669. assignments: []
  670. };
  671. this.crawling = true;
  672. if (path.type !== "Program" && collectorVisitor._exploded) {
  673. for (const visit of collectorVisitor.enter) {
  674. visit(path, state);
  675. }
  676. const typeVisitors = collectorVisitor[path.type];
  677. if (typeVisitors) {
  678. for (const visit of typeVisitors.enter) {
  679. visit(path, state);
  680. }
  681. }
  682. }
  683. path.traverse(collectorVisitor, state);
  684. this.crawling = false;
  685. for (const path of state.assignments) {
  686. const ids = path.getBindingIdentifiers();
  687. for (const name of Object.keys(ids)) {
  688. if (path.scope.getBinding(name)) continue;
  689. programParent.addGlobal(ids[name]);
  690. }
  691. path.scope.registerConstantViolation(path);
  692. }
  693. for (const ref of state.references) {
  694. const binding = ref.scope.getBinding(ref.node.name);
  695. if (binding) {
  696. binding.reference(ref);
  697. } else {
  698. programParent.addGlobal(ref.node);
  699. }
  700. }
  701. for (const path of state.constantViolations) {
  702. path.scope.registerConstantViolation(path);
  703. }
  704. }
  705. push(opts) {
  706. let path = this.path;
  707. if (path.isPattern()) {
  708. path = this.getPatternParent().path;
  709. } else if (!path.isBlockStatement() && !path.isProgram()) {
  710. path = this.getBlockParent().path;
  711. }
  712. if (path.isSwitchStatement()) {
  713. path = (this.getFunctionParent() || this.getProgramParent()).path;
  714. }
  715. if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
  716. path.ensureBlock();
  717. path = path.get("body");
  718. }
  719. const unique = opts.unique;
  720. const kind = opts.kind || "var";
  721. const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
  722. const dataKey = `declaration:${kind}:${blockHoist}`;
  723. let declarPath = !unique && path.getData(dataKey);
  724. if (!declarPath) {
  725. const declar = variableDeclaration(kind, []);
  726. declar._blockHoist = blockHoist;
  727. [declarPath] = path.unshiftContainer("body", [declar]);
  728. if (!unique) path.setData(dataKey, declarPath);
  729. }
  730. const declarator = variableDeclarator(opts.id, opts.init);
  731. const len = declarPath.node.declarations.push(declarator);
  732. path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
  733. }
  734. getProgramParent() {
  735. let scope = this;
  736. do {
  737. if (scope.path.isProgram()) {
  738. return scope;
  739. }
  740. } while (scope = scope.parent);
  741. throw new Error("Couldn't find a Program");
  742. }
  743. getFunctionParent() {
  744. let scope = this;
  745. do {
  746. if (scope.path.isFunctionParent()) {
  747. return scope;
  748. }
  749. } while (scope = scope.parent);
  750. return null;
  751. }
  752. getBlockParent() {
  753. let scope = this;
  754. do {
  755. if (scope.path.isBlockParent()) {
  756. return scope;
  757. }
  758. } while (scope = scope.parent);
  759. throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
  760. }
  761. getPatternParent() {
  762. let scope = this;
  763. do {
  764. if (!scope.path.isPattern()) {
  765. return scope.getBlockParent();
  766. }
  767. } while (scope = scope.parent.parent);
  768. throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
  769. }
  770. getAllBindings() {
  771. const ids = Object.create(null);
  772. let scope = this;
  773. do {
  774. for (const key of Object.keys(scope.bindings)) {
  775. if (key in ids === false) {
  776. ids[key] = scope.bindings[key];
  777. }
  778. }
  779. scope = scope.parent;
  780. } while (scope);
  781. return ids;
  782. }
  783. getAllBindingsOfKind(...kinds) {
  784. const ids = Object.create(null);
  785. for (const kind of kinds) {
  786. let scope = this;
  787. do {
  788. for (const name of Object.keys(scope.bindings)) {
  789. const binding = scope.bindings[name];
  790. if (binding.kind === kind) ids[name] = binding;
  791. }
  792. scope = scope.parent;
  793. } while (scope);
  794. }
  795. return ids;
  796. }
  797. bindingIdentifierEquals(name, node) {
  798. return this.getBindingIdentifier(name) === node;
  799. }
  800. getBinding(name) {
  801. let scope = this;
  802. let previousPath;
  803. do {
  804. const binding = scope.getOwnBinding(name);
  805. if (binding) {
  806. var _previousPath;
  807. if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {
  808. } else {
  809. return binding;
  810. }
  811. } else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
  812. break;
  813. }
  814. previousPath = scope.path;
  815. } while (scope = scope.parent);
  816. }
  817. getOwnBinding(name) {
  818. return this.bindings[name];
  819. }
  820. getBindingIdentifier(name) {
  821. var _this$getBinding;
  822. return (_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.identifier;
  823. }
  824. getOwnBindingIdentifier(name) {
  825. const binding = this.bindings[name];
  826. return binding == null ? void 0 : binding.identifier;
  827. }
  828. hasOwnBinding(name) {
  829. return !!this.getOwnBinding(name);
  830. }
  831. hasBinding(name, opts) {
  832. var _opts, _opts2, _opts3;
  833. if (!name) return false;
  834. if (this.hasOwnBinding(name)) return true;
  835. {
  836. if (typeof opts === "boolean") opts = {
  837. noGlobals: opts
  838. };
  839. }
  840. if (this.parentHasBinding(name, opts)) return true;
  841. if (!((_opts = opts) != null && _opts.noUids) && this.hasUid(name)) return true;
  842. if (!((_opts2 = opts) != null && _opts2.noGlobals) && Scope.globals.includes(name)) return true;
  843. if (!((_opts3 = opts) != null && _opts3.noGlobals) && Scope.contextVariables.includes(name)) return true;
  844. return false;
  845. }
  846. parentHasBinding(name, opts) {
  847. var _this$parent;
  848. return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, opts);
  849. }
  850. moveBindingTo(name, scope) {
  851. const info = this.getBinding(name);
  852. if (info) {
  853. info.scope.removeOwnBinding(name);
  854. info.scope = scope;
  855. scope.bindings[name] = info;
  856. }
  857. }
  858. removeOwnBinding(name) {
  859. delete this.bindings[name];
  860. }
  861. removeBinding(name) {
  862. var _this$getBinding2;
  863. (_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.scope.removeOwnBinding(name);
  864. let scope = this;
  865. do {
  866. if (scope.uids[name]) {
  867. scope.uids[name] = false;
  868. }
  869. } while (scope = scope.parent);
  870. }
  871. }
  872. exports.default = Scope;
  873. Scope.globals = Object.keys(_globals.builtin);
  874. Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
  875. //# sourceMappingURL=index.js.map