index.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. isModuleDeclaration,
  32. isModuleSpecifier,
  33. isNullLiteral,
  34. isObjectExpression,
  35. isProperty,
  36. isPureish,
  37. isRegExpLiteral,
  38. isSuper,
  39. isTaggedTemplateExpression,
  40. isTemplateLiteral,
  41. isThisExpression,
  42. isUnaryExpression,
  43. isVariableDeclaration,
  44. matchesPattern,
  45. memberExpression,
  46. numericLiteral,
  47. toIdentifier,
  48. unaryExpression,
  49. variableDeclaration,
  50. variableDeclarator,
  51. isRecordExpression,
  52. isTupleExpression,
  53. isObjectProperty,
  54. isTopicReference,
  55. isMetaProperty,
  56. isPrivateName
  57. } = _t;
  58. function gatherNodeParts(node, parts) {
  59. switch (node == null ? void 0 : node.type) {
  60. default:
  61. if (isModuleDeclaration(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 (i) {
  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 specifiers = path.get("specifiers");
  492. for (const specifier of specifiers) {
  493. this.registerBinding("module", specifier);
  494. }
  495. } else if (path.isExportDeclaration()) {
  496. const declar = path.get("declaration");
  497. if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) {
  498. this.registerDeclaration(declar);
  499. }
  500. } else {
  501. this.registerBinding("unknown", path);
  502. }
  503. }
  504. buildUndefinedNode() {
  505. return unaryExpression("void", numericLiteral(0), true);
  506. }
  507. registerConstantViolation(path) {
  508. const ids = path.getBindingIdentifiers();
  509. for (const name of Object.keys(ids)) {
  510. const binding = this.getBinding(name);
  511. if (binding) binding.reassign(path);
  512. }
  513. }
  514. registerBinding(kind, path, bindingPath = path) {
  515. if (!kind) throw new ReferenceError("no `kind`");
  516. if (path.isVariableDeclaration()) {
  517. const declarators = path.get("declarations");
  518. for (const declar of declarators) {
  519. this.registerBinding(kind, declar);
  520. }
  521. return;
  522. }
  523. const parent = this.getProgramParent();
  524. const ids = path.getOuterBindingIdentifiers(true);
  525. for (const name of Object.keys(ids)) {
  526. parent.references[name] = true;
  527. for (const id of ids[name]) {
  528. const local = this.getOwnBinding(name);
  529. if (local) {
  530. if (local.identifier === id) continue;
  531. this.checkBlockScopedCollisions(local, kind, name, id);
  532. }
  533. if (local) {
  534. this.registerConstantViolation(bindingPath);
  535. } else {
  536. this.bindings[name] = new _binding.default({
  537. identifier: id,
  538. scope: this,
  539. path: bindingPath,
  540. kind: kind
  541. });
  542. }
  543. }
  544. }
  545. }
  546. addGlobal(node) {
  547. this.globals[node.name] = node;
  548. }
  549. hasUid(name) {
  550. let scope = this;
  551. do {
  552. if (scope.uids[name]) return true;
  553. } while (scope = scope.parent);
  554. return false;
  555. }
  556. hasGlobal(name) {
  557. let scope = this;
  558. do {
  559. if (scope.globals[name]) return true;
  560. } while (scope = scope.parent);
  561. return false;
  562. }
  563. hasReference(name) {
  564. return !!this.getProgramParent().references[name];
  565. }
  566. isPure(node, constantsOnly) {
  567. if (isIdentifier(node)) {
  568. const binding = this.getBinding(node.name);
  569. if (!binding) return false;
  570. if (constantsOnly) return binding.constant;
  571. return true;
  572. } else if (isThisExpression(node) || isMetaProperty(node) || isTopicReference(node) || isPrivateName(node)) {
  573. return true;
  574. } else if (isClass(node)) {
  575. var _node$decorators;
  576. if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
  577. return false;
  578. }
  579. if (((_node$decorators = node.decorators) == null ? void 0 : _node$decorators.length) > 0) {
  580. return false;
  581. }
  582. return this.isPure(node.body, constantsOnly);
  583. } else if (isClassBody(node)) {
  584. for (const method of node.body) {
  585. if (!this.isPure(method, constantsOnly)) return false;
  586. }
  587. return true;
  588. } else if (isBinary(node)) {
  589. return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
  590. } else if (isArrayExpression(node) || isTupleExpression(node)) {
  591. for (const elem of node.elements) {
  592. if (elem !== null && !this.isPure(elem, constantsOnly)) return false;
  593. }
  594. return true;
  595. } else if (isObjectExpression(node) || isRecordExpression(node)) {
  596. for (const prop of node.properties) {
  597. if (!this.isPure(prop, constantsOnly)) return false;
  598. }
  599. return true;
  600. } else if (isMethod(node)) {
  601. var _node$decorators2;
  602. if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
  603. if (((_node$decorators2 = node.decorators) == null ? void 0 : _node$decorators2.length) > 0) {
  604. return false;
  605. }
  606. return true;
  607. } else if (isProperty(node)) {
  608. var _node$decorators3;
  609. if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
  610. if (((_node$decorators3 = node.decorators) == null ? void 0 : _node$decorators3.length) > 0) {
  611. return false;
  612. }
  613. if (isObjectProperty(node) || node.static) {
  614. if (node.value !== null && !this.isPure(node.value, constantsOnly)) {
  615. return false;
  616. }
  617. }
  618. return true;
  619. } else if (isUnaryExpression(node)) {
  620. return this.isPure(node.argument, constantsOnly);
  621. } else if (isTaggedTemplateExpression(node)) {
  622. return matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
  623. } else if (isTemplateLiteral(node)) {
  624. for (const expression of node.expressions) {
  625. if (!this.isPure(expression, constantsOnly)) return false;
  626. }
  627. return true;
  628. } else {
  629. return isPureish(node);
  630. }
  631. }
  632. setData(key, val) {
  633. return this.data[key] = val;
  634. }
  635. getData(key) {
  636. let scope = this;
  637. do {
  638. const data = scope.data[key];
  639. if (data != null) return data;
  640. } while (scope = scope.parent);
  641. }
  642. removeData(key) {
  643. let scope = this;
  644. do {
  645. const data = scope.data[key];
  646. if (data != null) scope.data[key] = null;
  647. } while (scope = scope.parent);
  648. }
  649. init() {
  650. if (!this.inited) {
  651. this.inited = true;
  652. this.crawl();
  653. }
  654. }
  655. crawl() {
  656. const path = this.path;
  657. this.references = Object.create(null);
  658. this.bindings = Object.create(null);
  659. this.globals = Object.create(null);
  660. this.uids = Object.create(null);
  661. this.data = Object.create(null);
  662. const programParent = this.getProgramParent();
  663. if (programParent.crawling) return;
  664. const state = {
  665. references: [],
  666. constantViolations: [],
  667. assignments: []
  668. };
  669. this.crawling = true;
  670. if (path.type !== "Program" && collectorVisitor._exploded) {
  671. for (const visit of collectorVisitor.enter) {
  672. visit(path, state);
  673. }
  674. const typeVisitors = collectorVisitor[path.type];
  675. if (typeVisitors) {
  676. for (const visit of typeVisitors.enter) {
  677. visit(path, state);
  678. }
  679. }
  680. }
  681. path.traverse(collectorVisitor, state);
  682. this.crawling = false;
  683. for (const path of state.assignments) {
  684. const ids = path.getBindingIdentifiers();
  685. for (const name of Object.keys(ids)) {
  686. if (path.scope.getBinding(name)) continue;
  687. programParent.addGlobal(ids[name]);
  688. }
  689. path.scope.registerConstantViolation(path);
  690. }
  691. for (const ref of state.references) {
  692. const binding = ref.scope.getBinding(ref.node.name);
  693. if (binding) {
  694. binding.reference(ref);
  695. } else {
  696. programParent.addGlobal(ref.node);
  697. }
  698. }
  699. for (const path of state.constantViolations) {
  700. path.scope.registerConstantViolation(path);
  701. }
  702. }
  703. push(opts) {
  704. let path = this.path;
  705. if (path.isPattern()) {
  706. path = this.getPatternParent().path;
  707. } else if (!path.isBlockStatement() && !path.isProgram()) {
  708. path = this.getBlockParent().path;
  709. }
  710. if (path.isSwitchStatement()) {
  711. path = (this.getFunctionParent() || this.getProgramParent()).path;
  712. }
  713. if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
  714. path.ensureBlock();
  715. path = path.get("body");
  716. }
  717. const unique = opts.unique;
  718. const kind = opts.kind || "var";
  719. const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
  720. const dataKey = `declaration:${kind}:${blockHoist}`;
  721. let declarPath = !unique && path.getData(dataKey);
  722. if (!declarPath) {
  723. const declar = variableDeclaration(kind, []);
  724. declar._blockHoist = blockHoist;
  725. [declarPath] = path.unshiftContainer("body", [declar]);
  726. if (!unique) path.setData(dataKey, declarPath);
  727. }
  728. const declarator = variableDeclarator(opts.id, opts.init);
  729. const len = declarPath.node.declarations.push(declarator);
  730. path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
  731. }
  732. getProgramParent() {
  733. let scope = this;
  734. do {
  735. if (scope.path.isProgram()) {
  736. return scope;
  737. }
  738. } while (scope = scope.parent);
  739. throw new Error("Couldn't find a Program");
  740. }
  741. getFunctionParent() {
  742. let scope = this;
  743. do {
  744. if (scope.path.isFunctionParent()) {
  745. return scope;
  746. }
  747. } while (scope = scope.parent);
  748. return null;
  749. }
  750. getBlockParent() {
  751. let scope = this;
  752. do {
  753. if (scope.path.isBlockParent()) {
  754. return scope;
  755. }
  756. } while (scope = scope.parent);
  757. throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
  758. }
  759. getPatternParent() {
  760. let scope = this;
  761. do {
  762. if (!scope.path.isPattern()) {
  763. return scope.getBlockParent();
  764. }
  765. } while (scope = scope.parent.parent);
  766. throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
  767. }
  768. getAllBindings() {
  769. const ids = Object.create(null);
  770. let scope = this;
  771. do {
  772. for (const key of Object.keys(scope.bindings)) {
  773. if (key in ids === false) {
  774. ids[key] = scope.bindings[key];
  775. }
  776. }
  777. scope = scope.parent;
  778. } while (scope);
  779. return ids;
  780. }
  781. getAllBindingsOfKind(...kinds) {
  782. const ids = Object.create(null);
  783. for (const kind of kinds) {
  784. let scope = this;
  785. do {
  786. for (const name of Object.keys(scope.bindings)) {
  787. const binding = scope.bindings[name];
  788. if (binding.kind === kind) ids[name] = binding;
  789. }
  790. scope = scope.parent;
  791. } while (scope);
  792. }
  793. return ids;
  794. }
  795. bindingIdentifierEquals(name, node) {
  796. return this.getBindingIdentifier(name) === node;
  797. }
  798. getBinding(name) {
  799. let scope = this;
  800. let previousPath;
  801. do {
  802. const binding = scope.getOwnBinding(name);
  803. if (binding) {
  804. var _previousPath;
  805. if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {
  806. } else {
  807. return binding;
  808. }
  809. } else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
  810. break;
  811. }
  812. previousPath = scope.path;
  813. } while (scope = scope.parent);
  814. }
  815. getOwnBinding(name) {
  816. return this.bindings[name];
  817. }
  818. getBindingIdentifier(name) {
  819. var _this$getBinding;
  820. return (_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.identifier;
  821. }
  822. getOwnBindingIdentifier(name) {
  823. const binding = this.bindings[name];
  824. return binding == null ? void 0 : binding.identifier;
  825. }
  826. hasOwnBinding(name) {
  827. return !!this.getOwnBinding(name);
  828. }
  829. hasBinding(name, noGlobals) {
  830. if (!name) return false;
  831. if (this.hasOwnBinding(name)) return true;
  832. if (this.parentHasBinding(name, noGlobals)) return true;
  833. if (this.hasUid(name)) return true;
  834. if (!noGlobals && Scope.globals.includes(name)) return true;
  835. if (!noGlobals && Scope.contextVariables.includes(name)) return true;
  836. return false;
  837. }
  838. parentHasBinding(name, noGlobals) {
  839. var _this$parent;
  840. return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, noGlobals);
  841. }
  842. moveBindingTo(name, scope) {
  843. const info = this.getBinding(name);
  844. if (info) {
  845. info.scope.removeOwnBinding(name);
  846. info.scope = scope;
  847. scope.bindings[name] = info;
  848. }
  849. }
  850. removeOwnBinding(name) {
  851. delete this.bindings[name];
  852. }
  853. removeBinding(name) {
  854. var _this$getBinding2;
  855. (_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.scope.removeOwnBinding(name);
  856. let scope = this;
  857. do {
  858. if (scope.uids[name]) {
  859. scope.uids[name] = false;
  860. }
  861. } while (scope = scope.parent);
  862. }
  863. }
  864. exports.default = Scope;
  865. Scope.globals = Object.keys(_globals.builtin);
  866. Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
  867. //# sourceMappingURL=index.js.map