index.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. } else if (left.isVar()) {
  202. const {
  203. scope
  204. } = path;
  205. const parentScope = scope.getFunctionParent() || scope.getProgramParent();
  206. parentScope.registerBinding("var", left);
  207. }
  208. },
  209. ExportDeclaration: {
  210. exit(path) {
  211. const {
  212. node,
  213. scope
  214. } = path;
  215. if (isExportAllDeclaration(node)) return;
  216. const declar = node.declaration;
  217. if (isClassDeclaration(declar) || isFunctionDeclaration(declar)) {
  218. const id = declar.id;
  219. if (!id) return;
  220. const binding = scope.getBinding(id.name);
  221. binding == null ? void 0 : binding.reference(path);
  222. } else if (isVariableDeclaration(declar)) {
  223. for (const decl of declar.declarations) {
  224. for (const name of Object.keys(getBindingIdentifiers(decl))) {
  225. const binding = scope.getBinding(name);
  226. binding == null ? void 0 : binding.reference(path);
  227. }
  228. }
  229. }
  230. }
  231. },
  232. LabeledStatement(path) {
  233. path.scope.getBlockParent().registerDeclaration(path);
  234. },
  235. AssignmentExpression(path, state) {
  236. state.assignments.push(path);
  237. },
  238. UpdateExpression(path, state) {
  239. state.constantViolations.push(path);
  240. },
  241. UnaryExpression(path, state) {
  242. if (path.node.operator === "delete") {
  243. state.constantViolations.push(path);
  244. }
  245. },
  246. BlockScoped(path) {
  247. let scope = path.scope;
  248. if (scope.path === path) scope = scope.parent;
  249. const parent = scope.getBlockParent();
  250. parent.registerDeclaration(path);
  251. if (path.isClassDeclaration() && path.node.id) {
  252. const id = path.node.id;
  253. const name = id.name;
  254. path.scope.bindings[name] = path.scope.parent.getBinding(name);
  255. }
  256. },
  257. CatchClause(path) {
  258. path.scope.registerBinding("let", path);
  259. },
  260. Function(path) {
  261. const params = path.get("params");
  262. for (const param of params) {
  263. path.scope.registerBinding("param", param);
  264. }
  265. if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
  266. path.scope.registerBinding("local", path.get("id"), path);
  267. }
  268. },
  269. ClassExpression(path) {
  270. if (path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
  271. path.scope.registerBinding("local", path);
  272. }
  273. }
  274. };
  275. let uid = 0;
  276. class Scope {
  277. constructor(path) {
  278. this.uid = void 0;
  279. this.path = void 0;
  280. this.block = void 0;
  281. this.labels = void 0;
  282. this.inited = void 0;
  283. this.bindings = void 0;
  284. this.references = void 0;
  285. this.globals = void 0;
  286. this.uids = void 0;
  287. this.data = void 0;
  288. this.crawling = void 0;
  289. const {
  290. node
  291. } = path;
  292. const cached = _cache.scope.get(node);
  293. if ((cached == null ? void 0 : cached.path) === path) {
  294. return cached;
  295. }
  296. _cache.scope.set(node, this);
  297. this.uid = uid++;
  298. this.block = node;
  299. this.path = path;
  300. this.labels = new Map();
  301. this.inited = false;
  302. }
  303. get parent() {
  304. var _parent;
  305. let parent,
  306. path = this.path;
  307. do {
  308. const shouldSkip = path.key === "key" || path.listKey === "decorators";
  309. path = path.parentPath;
  310. if (shouldSkip && path.isMethod()) path = path.parentPath;
  311. if (path && path.isScope()) parent = path;
  312. } while (path && !parent);
  313. return (_parent = parent) == null ? void 0 : _parent.scope;
  314. }
  315. get parentBlock() {
  316. return this.path.parent;
  317. }
  318. get hub() {
  319. return this.path.hub;
  320. }
  321. traverse(node, opts, state) {
  322. (0, _index.default)(node, opts, this, state, this.path);
  323. }
  324. generateDeclaredUidIdentifier(name) {
  325. const id = this.generateUidIdentifier(name);
  326. this.push({
  327. id
  328. });
  329. return cloneNode(id);
  330. }
  331. generateUidIdentifier(name) {
  332. return identifier(this.generateUid(name));
  333. }
  334. generateUid(name = "temp") {
  335. name = toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
  336. let uid;
  337. let i = 1;
  338. do {
  339. uid = this._generateUid(name, i);
  340. i++;
  341. } while (this.hasLabel(uid) || this.hasBinding(uid) || this.hasGlobal(uid) || this.hasReference(uid));
  342. const program = this.getProgramParent();
  343. program.references[uid] = true;
  344. program.uids[uid] = true;
  345. return uid;
  346. }
  347. _generateUid(name, i) {
  348. let id = name;
  349. if (i > 1) id += i;
  350. return `_${id}`;
  351. }
  352. generateUidBasedOnNode(node, defaultName) {
  353. const parts = [];
  354. gatherNodeParts(node, parts);
  355. let id = parts.join("$");
  356. id = id.replace(/^_/, "") || defaultName || "ref";
  357. return this.generateUid(id.slice(0, 20));
  358. }
  359. generateUidIdentifierBasedOnNode(node, defaultName) {
  360. return identifier(this.generateUidBasedOnNode(node, defaultName));
  361. }
  362. isStatic(node) {
  363. if (isThisExpression(node) || isSuper(node) || isTopicReference(node)) {
  364. return true;
  365. }
  366. if (isIdentifier(node)) {
  367. const binding = this.getBinding(node.name);
  368. if (binding) {
  369. return binding.constant;
  370. } else {
  371. return this.hasBinding(node.name);
  372. }
  373. }
  374. return false;
  375. }
  376. maybeGenerateMemoised(node, dontPush) {
  377. if (this.isStatic(node)) {
  378. return null;
  379. } else {
  380. const id = this.generateUidIdentifierBasedOnNode(node);
  381. if (!dontPush) {
  382. this.push({
  383. id
  384. });
  385. return cloneNode(id);
  386. }
  387. return id;
  388. }
  389. }
  390. checkBlockScopedCollisions(local, kind, name, id) {
  391. if (kind === "param") return;
  392. if (local.kind === "local") return;
  393. const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && kind === "const";
  394. if (duplicate) {
  395. throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
  396. }
  397. }
  398. rename(oldName, newName) {
  399. const binding = this.getBinding(oldName);
  400. if (binding) {
  401. newName || (newName = this.generateUidIdentifier(oldName).name);
  402. const renamer = new _renamer.default(binding, oldName, newName);
  403. return renamer.rename(arguments[2]);
  404. }
  405. }
  406. _renameFromMap(map, oldName, newName, value) {
  407. if (map[oldName]) {
  408. map[newName] = value;
  409. map[oldName] = null;
  410. }
  411. }
  412. dump() {
  413. const sep = "-".repeat(60);
  414. console.log(sep);
  415. let scope = this;
  416. do {
  417. console.log("#", scope.block.type);
  418. for (const name of Object.keys(scope.bindings)) {
  419. const binding = scope.bindings[name];
  420. console.log(" -", name, {
  421. constant: binding.constant,
  422. references: binding.references,
  423. violations: binding.constantViolations.length,
  424. kind: binding.kind
  425. });
  426. }
  427. } while (scope = scope.parent);
  428. console.log(sep);
  429. }
  430. toArray(node, i, arrayLikeIsIterable) {
  431. if (isIdentifier(node)) {
  432. const binding = this.getBinding(node.name);
  433. if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
  434. return node;
  435. }
  436. }
  437. if (isArrayExpression(node)) {
  438. return node;
  439. }
  440. if (isIdentifier(node, {
  441. name: "arguments"
  442. })) {
  443. return callExpression(memberExpression(memberExpression(memberExpression(identifier("Array"), identifier("prototype")), identifier("slice")), identifier("call")), [node]);
  444. }
  445. let helperName;
  446. const args = [node];
  447. if (i === true) {
  448. helperName = "toConsumableArray";
  449. } else if (typeof i === "number") {
  450. args.push(numericLiteral(i));
  451. helperName = "slicedToArray";
  452. } else {
  453. helperName = "toArray";
  454. }
  455. if (arrayLikeIsIterable) {
  456. args.unshift(this.hub.addHelper(helperName));
  457. helperName = "maybeArrayLike";
  458. }
  459. return callExpression(this.hub.addHelper(helperName), args);
  460. }
  461. hasLabel(name) {
  462. return !!this.getLabel(name);
  463. }
  464. getLabel(name) {
  465. return this.labels.get(name);
  466. }
  467. registerLabel(path) {
  468. this.labels.set(path.node.label.name, path);
  469. }
  470. registerDeclaration(path) {
  471. if (path.isLabeledStatement()) {
  472. this.registerLabel(path);
  473. } else if (path.isFunctionDeclaration()) {
  474. this.registerBinding("hoisted", path.get("id"), path);
  475. } else if (path.isVariableDeclaration()) {
  476. const declarations = path.get("declarations");
  477. const {
  478. kind
  479. } = path.node;
  480. for (const declar of declarations) {
  481. this.registerBinding(kind === "using" ? "const" : kind, declar);
  482. }
  483. } else if (path.isClassDeclaration()) {
  484. if (path.node.declare) return;
  485. this.registerBinding("let", path);
  486. } else if (path.isImportDeclaration()) {
  487. const isTypeDeclaration = path.node.importKind === "type" || path.node.importKind === "typeof";
  488. const specifiers = path.get("specifiers");
  489. for (const specifier of specifiers) {
  490. const isTypeSpecifier = isTypeDeclaration || specifier.isImportSpecifier() && (specifier.node.importKind === "type" || specifier.node.importKind === "typeof");
  491. this.registerBinding(isTypeSpecifier ? "unknown" : "module", specifier);
  492. }
  493. } else if (path.isExportDeclaration()) {
  494. const declar = path.get("declaration");
  495. if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) {
  496. this.registerDeclaration(declar);
  497. }
  498. } else {
  499. this.registerBinding("unknown", path);
  500. }
  501. }
  502. buildUndefinedNode() {
  503. return unaryExpression("void", numericLiteral(0), true);
  504. }
  505. registerConstantViolation(path) {
  506. const ids = path.getBindingIdentifiers();
  507. for (const name of Object.keys(ids)) {
  508. const binding = this.getBinding(name);
  509. if (binding) binding.reassign(path);
  510. }
  511. }
  512. registerBinding(kind, path, bindingPath = path) {
  513. if (!kind) throw new ReferenceError("no `kind`");
  514. if (path.isVariableDeclaration()) {
  515. const declarators = path.get("declarations");
  516. for (const declar of declarators) {
  517. this.registerBinding(kind, declar);
  518. }
  519. return;
  520. }
  521. const parent = this.getProgramParent();
  522. const ids = path.getOuterBindingIdentifiers(true);
  523. for (const name of Object.keys(ids)) {
  524. parent.references[name] = true;
  525. for (const id of ids[name]) {
  526. const local = this.getOwnBinding(name);
  527. if (local) {
  528. if (local.identifier === id) continue;
  529. this.checkBlockScopedCollisions(local, kind, name, id);
  530. }
  531. if (local) {
  532. this.registerConstantViolation(bindingPath);
  533. } else {
  534. this.bindings[name] = new _binding.default({
  535. identifier: id,
  536. scope: this,
  537. path: bindingPath,
  538. kind: kind
  539. });
  540. }
  541. }
  542. }
  543. }
  544. addGlobal(node) {
  545. this.globals[node.name] = node;
  546. }
  547. hasUid(name) {
  548. let scope = this;
  549. do {
  550. if (scope.uids[name]) return true;
  551. } while (scope = scope.parent);
  552. return false;
  553. }
  554. hasGlobal(name) {
  555. let scope = this;
  556. do {
  557. if (scope.globals[name]) return true;
  558. } while (scope = scope.parent);
  559. return false;
  560. }
  561. hasReference(name) {
  562. return !!this.getProgramParent().references[name];
  563. }
  564. isPure(node, constantsOnly) {
  565. if (isIdentifier(node)) {
  566. const binding = this.getBinding(node.name);
  567. if (!binding) return false;
  568. if (constantsOnly) return binding.constant;
  569. return true;
  570. } else if (isThisExpression(node) || isMetaProperty(node) || isTopicReference(node) || isPrivateName(node)) {
  571. return true;
  572. } else if (isClass(node)) {
  573. var _node$decorators;
  574. if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
  575. return false;
  576. }
  577. if (((_node$decorators = node.decorators) == null ? void 0 : _node$decorators.length) > 0) {
  578. return false;
  579. }
  580. return this.isPure(node.body, constantsOnly);
  581. } else if (isClassBody(node)) {
  582. for (const method of node.body) {
  583. if (!this.isPure(method, constantsOnly)) return false;
  584. }
  585. return true;
  586. } else if (isBinary(node)) {
  587. return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
  588. } else if (isArrayExpression(node) || isTupleExpression(node)) {
  589. for (const elem of node.elements) {
  590. if (elem !== null && !this.isPure(elem, constantsOnly)) return false;
  591. }
  592. return true;
  593. } else if (isObjectExpression(node) || isRecordExpression(node)) {
  594. for (const prop of node.properties) {
  595. if (!this.isPure(prop, constantsOnly)) return false;
  596. }
  597. return true;
  598. } else if (isMethod(node)) {
  599. var _node$decorators2;
  600. if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
  601. if (((_node$decorators2 = node.decorators) == null ? void 0 : _node$decorators2.length) > 0) {
  602. return false;
  603. }
  604. return true;
  605. } else if (isProperty(node)) {
  606. var _node$decorators3;
  607. if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
  608. if (((_node$decorators3 = node.decorators) == null ? void 0 : _node$decorators3.length) > 0) {
  609. return false;
  610. }
  611. if (isObjectProperty(node) || node.static) {
  612. if (node.value !== null && !this.isPure(node.value, constantsOnly)) {
  613. return false;
  614. }
  615. }
  616. return true;
  617. } else if (isUnaryExpression(node)) {
  618. return this.isPure(node.argument, constantsOnly);
  619. } else if (isTaggedTemplateExpression(node)) {
  620. return matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
  621. } else if (isTemplateLiteral(node)) {
  622. for (const expression of node.expressions) {
  623. if (!this.isPure(expression, constantsOnly)) return false;
  624. }
  625. return true;
  626. } else {
  627. return isPureish(node);
  628. }
  629. }
  630. setData(key, val) {
  631. return this.data[key] = val;
  632. }
  633. getData(key) {
  634. let scope = this;
  635. do {
  636. const data = scope.data[key];
  637. if (data != null) return data;
  638. } while (scope = scope.parent);
  639. }
  640. removeData(key) {
  641. let scope = this;
  642. do {
  643. const data = scope.data[key];
  644. if (data != null) scope.data[key] = null;
  645. } while (scope = scope.parent);
  646. }
  647. init() {
  648. if (!this.inited) {
  649. this.inited = true;
  650. this.crawl();
  651. }
  652. }
  653. crawl() {
  654. const path = this.path;
  655. this.references = Object.create(null);
  656. this.bindings = Object.create(null);
  657. this.globals = Object.create(null);
  658. this.uids = Object.create(null);
  659. this.data = Object.create(null);
  660. const programParent = this.getProgramParent();
  661. if (programParent.crawling) return;
  662. const state = {
  663. references: [],
  664. constantViolations: [],
  665. assignments: []
  666. };
  667. this.crawling = true;
  668. if (path.type !== "Program" && collectorVisitor._exploded) {
  669. for (const visit of collectorVisitor.enter) {
  670. visit(path, state);
  671. }
  672. const typeVisitors = collectorVisitor[path.type];
  673. if (typeVisitors) {
  674. for (const visit of typeVisitors.enter) {
  675. visit(path, state);
  676. }
  677. }
  678. }
  679. path.traverse(collectorVisitor, state);
  680. this.crawling = false;
  681. for (const path of state.assignments) {
  682. const ids = path.getBindingIdentifiers();
  683. for (const name of Object.keys(ids)) {
  684. if (path.scope.getBinding(name)) continue;
  685. programParent.addGlobal(ids[name]);
  686. }
  687. path.scope.registerConstantViolation(path);
  688. }
  689. for (const ref of state.references) {
  690. const binding = ref.scope.getBinding(ref.node.name);
  691. if (binding) {
  692. binding.reference(ref);
  693. } else {
  694. programParent.addGlobal(ref.node);
  695. }
  696. }
  697. for (const path of state.constantViolations) {
  698. path.scope.registerConstantViolation(path);
  699. }
  700. }
  701. push(opts) {
  702. let path = this.path;
  703. if (path.isPattern()) {
  704. path = this.getPatternParent().path;
  705. } else if (!path.isBlockStatement() && !path.isProgram()) {
  706. path = this.getBlockParent().path;
  707. }
  708. if (path.isSwitchStatement()) {
  709. path = (this.getFunctionParent() || this.getProgramParent()).path;
  710. }
  711. if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
  712. path.ensureBlock();
  713. path = path.get("body");
  714. }
  715. const unique = opts.unique;
  716. const kind = opts.kind || "var";
  717. const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
  718. const dataKey = `declaration:${kind}:${blockHoist}`;
  719. let declarPath = !unique && path.getData(dataKey);
  720. if (!declarPath) {
  721. const declar = variableDeclaration(kind, []);
  722. declar._blockHoist = blockHoist;
  723. [declarPath] = path.unshiftContainer("body", [declar]);
  724. if (!unique) path.setData(dataKey, declarPath);
  725. }
  726. const declarator = variableDeclarator(opts.id, opts.init);
  727. const len = declarPath.node.declarations.push(declarator);
  728. path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
  729. }
  730. getProgramParent() {
  731. let scope = this;
  732. do {
  733. if (scope.path.isProgram()) {
  734. return scope;
  735. }
  736. } while (scope = scope.parent);
  737. throw new Error("Couldn't find a Program");
  738. }
  739. getFunctionParent() {
  740. let scope = this;
  741. do {
  742. if (scope.path.isFunctionParent()) {
  743. return scope;
  744. }
  745. } while (scope = scope.parent);
  746. return null;
  747. }
  748. getBlockParent() {
  749. let scope = this;
  750. do {
  751. if (scope.path.isBlockParent()) {
  752. return scope;
  753. }
  754. } while (scope = scope.parent);
  755. throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
  756. }
  757. getPatternParent() {
  758. let scope = this;
  759. do {
  760. if (!scope.path.isPattern()) {
  761. return scope.getBlockParent();
  762. }
  763. } while (scope = scope.parent.parent);
  764. throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
  765. }
  766. getAllBindings() {
  767. const ids = Object.create(null);
  768. let scope = this;
  769. do {
  770. for (const key of Object.keys(scope.bindings)) {
  771. if (key in ids === false) {
  772. ids[key] = scope.bindings[key];
  773. }
  774. }
  775. scope = scope.parent;
  776. } while (scope);
  777. return ids;
  778. }
  779. getAllBindingsOfKind(...kinds) {
  780. const ids = Object.create(null);
  781. for (const kind of kinds) {
  782. let scope = this;
  783. do {
  784. for (const name of Object.keys(scope.bindings)) {
  785. const binding = scope.bindings[name];
  786. if (binding.kind === kind) ids[name] = binding;
  787. }
  788. scope = scope.parent;
  789. } while (scope);
  790. }
  791. return ids;
  792. }
  793. bindingIdentifierEquals(name, node) {
  794. return this.getBindingIdentifier(name) === node;
  795. }
  796. getBinding(name) {
  797. let scope = this;
  798. let previousPath;
  799. do {
  800. const binding = scope.getOwnBinding(name);
  801. if (binding) {
  802. var _previousPath;
  803. if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {} else {
  804. return binding;
  805. }
  806. } else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
  807. break;
  808. }
  809. previousPath = scope.path;
  810. } while (scope = scope.parent);
  811. }
  812. getOwnBinding(name) {
  813. return this.bindings[name];
  814. }
  815. getBindingIdentifier(name) {
  816. var _this$getBinding;
  817. return (_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.identifier;
  818. }
  819. getOwnBindingIdentifier(name) {
  820. const binding = this.bindings[name];
  821. return binding == null ? void 0 : binding.identifier;
  822. }
  823. hasOwnBinding(name) {
  824. return !!this.getOwnBinding(name);
  825. }
  826. hasBinding(name, opts) {
  827. var _opts, _opts2, _opts3;
  828. if (!name) return false;
  829. if (this.hasOwnBinding(name)) return true;
  830. {
  831. if (typeof opts === "boolean") opts = {
  832. noGlobals: opts
  833. };
  834. }
  835. if (this.parentHasBinding(name, opts)) return true;
  836. if (!((_opts = opts) != null && _opts.noUids) && this.hasUid(name)) return true;
  837. if (!((_opts2 = opts) != null && _opts2.noGlobals) && Scope.globals.includes(name)) return true;
  838. if (!((_opts3 = opts) != null && _opts3.noGlobals) && Scope.contextVariables.includes(name)) return true;
  839. return false;
  840. }
  841. parentHasBinding(name, opts) {
  842. var _this$parent;
  843. return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, opts);
  844. }
  845. moveBindingTo(name, scope) {
  846. const info = this.getBinding(name);
  847. if (info) {
  848. info.scope.removeOwnBinding(name);
  849. info.scope = scope;
  850. scope.bindings[name] = info;
  851. }
  852. }
  853. removeOwnBinding(name) {
  854. delete this.bindings[name];
  855. }
  856. removeBinding(name) {
  857. var _this$getBinding2;
  858. (_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.scope.removeOwnBinding(name);
  859. let scope = this;
  860. do {
  861. if (scope.uids[name]) {
  862. scope.uids[name] = false;
  863. }
  864. } while (scope = scope.parent);
  865. }
  866. }
  867. exports.default = Scope;
  868. Scope.globals = Object.keys(_globals.builtin);
  869. Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
  870. //# sourceMappingURL=index.js.map