index.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. {
  404. renamer.rename(arguments[2]);
  405. }
  406. }
  407. }
  408. _renameFromMap(map, oldName, newName, value) {
  409. if (map[oldName]) {
  410. map[newName] = value;
  411. map[oldName] = null;
  412. }
  413. }
  414. dump() {
  415. const sep = "-".repeat(60);
  416. console.log(sep);
  417. let scope = this;
  418. do {
  419. console.log("#", scope.block.type);
  420. for (const name of Object.keys(scope.bindings)) {
  421. const binding = scope.bindings[name];
  422. console.log(" -", name, {
  423. constant: binding.constant,
  424. references: binding.references,
  425. violations: binding.constantViolations.length,
  426. kind: binding.kind
  427. });
  428. }
  429. } while (scope = scope.parent);
  430. console.log(sep);
  431. }
  432. toArray(node, i, arrayLikeIsIterable) {
  433. if (isIdentifier(node)) {
  434. const binding = this.getBinding(node.name);
  435. if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
  436. return node;
  437. }
  438. }
  439. if (isArrayExpression(node)) {
  440. return node;
  441. }
  442. if (isIdentifier(node, {
  443. name: "arguments"
  444. })) {
  445. return callExpression(memberExpression(memberExpression(memberExpression(identifier("Array"), identifier("prototype")), identifier("slice")), identifier("call")), [node]);
  446. }
  447. let helperName;
  448. const args = [node];
  449. if (i === true) {
  450. helperName = "toConsumableArray";
  451. } else if (typeof i === "number") {
  452. args.push(numericLiteral(i));
  453. helperName = "slicedToArray";
  454. } else {
  455. helperName = "toArray";
  456. }
  457. if (arrayLikeIsIterable) {
  458. args.unshift(this.hub.addHelper(helperName));
  459. helperName = "maybeArrayLike";
  460. }
  461. return callExpression(this.hub.addHelper(helperName), args);
  462. }
  463. hasLabel(name) {
  464. return !!this.getLabel(name);
  465. }
  466. getLabel(name) {
  467. return this.labels.get(name);
  468. }
  469. registerLabel(path) {
  470. this.labels.set(path.node.label.name, path);
  471. }
  472. registerDeclaration(path) {
  473. if (path.isLabeledStatement()) {
  474. this.registerLabel(path);
  475. } else if (path.isFunctionDeclaration()) {
  476. this.registerBinding("hoisted", path.get("id"), path);
  477. } else if (path.isVariableDeclaration()) {
  478. const declarations = path.get("declarations");
  479. const {
  480. kind
  481. } = path.node;
  482. for (const declar of declarations) {
  483. this.registerBinding(kind === "using" ? "const" : kind, declar);
  484. }
  485. } else if (path.isClassDeclaration()) {
  486. if (path.node.declare) return;
  487. this.registerBinding("let", path);
  488. } else if (path.isImportDeclaration()) {
  489. const isTypeDeclaration = path.node.importKind === "type" || path.node.importKind === "typeof";
  490. const specifiers = path.get("specifiers");
  491. for (const specifier of specifiers) {
  492. const isTypeSpecifier = isTypeDeclaration || specifier.isImportSpecifier() && (specifier.node.importKind === "type" || specifier.node.importKind === "typeof");
  493. this.registerBinding(isTypeSpecifier ? "unknown" : "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") {} else {
  806. return binding;
  807. }
  808. } else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
  809. break;
  810. }
  811. previousPath = scope.path;
  812. } while (scope = scope.parent);
  813. }
  814. getOwnBinding(name) {
  815. return this.bindings[name];
  816. }
  817. getBindingIdentifier(name) {
  818. var _this$getBinding;
  819. return (_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.identifier;
  820. }
  821. getOwnBindingIdentifier(name) {
  822. const binding = this.bindings[name];
  823. return binding == null ? void 0 : binding.identifier;
  824. }
  825. hasOwnBinding(name) {
  826. return !!this.getOwnBinding(name);
  827. }
  828. hasBinding(name, opts) {
  829. var _opts, _opts2, _opts3;
  830. if (!name) return false;
  831. if (this.hasOwnBinding(name)) return true;
  832. {
  833. if (typeof opts === "boolean") opts = {
  834. noGlobals: opts
  835. };
  836. }
  837. if (this.parentHasBinding(name, opts)) return true;
  838. if (!((_opts = opts) != null && _opts.noUids) && this.hasUid(name)) return true;
  839. if (!((_opts2 = opts) != null && _opts2.noGlobals) && Scope.globals.includes(name)) return true;
  840. if (!((_opts3 = opts) != null && _opts3.noGlobals) && Scope.contextVariables.includes(name)) return true;
  841. return false;
  842. }
  843. parentHasBinding(name, opts) {
  844. var _this$parent;
  845. return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, opts);
  846. }
  847. moveBindingTo(name, scope) {
  848. const info = this.getBinding(name);
  849. if (info) {
  850. info.scope.removeOwnBinding(name);
  851. info.scope = scope;
  852. scope.bindings[name] = info;
  853. }
  854. }
  855. removeOwnBinding(name) {
  856. delete this.bindings[name];
  857. }
  858. removeBinding(name) {
  859. var _this$getBinding2;
  860. (_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.scope.removeOwnBinding(name);
  861. let scope = this;
  862. do {
  863. if (scope.uids[name]) {
  864. scope.uids[name] = false;
  865. }
  866. } while (scope = scope.parent);
  867. }
  868. }
  869. exports.default = Scope;
  870. Scope.globals = Object.keys(_globals.builtin);
  871. Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
  872. //# sourceMappingURL=index.js.map