index.js 24 KB

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