index.js 24 KB

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