index.js 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. } 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, block) {
  399. const binding = this.getBinding(oldName);
  400. if (binding) {
  401. newName = newName || this.generateUidIdentifier(oldName).name;
  402. return new _renamer.default(binding, oldName, newName).rename(block);
  403. }
  404. }
  405. _renameFromMap(map, oldName, newName, value) {
  406. if (map[oldName]) {
  407. map[newName] = value;
  408. map[oldName] = null;
  409. }
  410. }
  411. dump() {
  412. const sep = "-".repeat(60);
  413. console.log(sep);
  414. let scope = this;
  415. do {
  416. console.log("#", scope.block.type);
  417. for (const name of Object.keys(scope.bindings)) {
  418. const binding = scope.bindings[name];
  419. console.log(" -", name, {
  420. constant: binding.constant,
  421. references: binding.references,
  422. violations: binding.constantViolations.length,
  423. kind: binding.kind
  424. });
  425. }
  426. } while (scope = scope.parent);
  427. console.log(sep);
  428. }
  429. toArray(node, i, arrayLikeIsIterable) {
  430. if (isIdentifier(node)) {
  431. const binding = this.getBinding(node.name);
  432. if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
  433. return node;
  434. }
  435. }
  436. if (isArrayExpression(node)) {
  437. return node;
  438. }
  439. if (isIdentifier(node, {
  440. name: "arguments"
  441. })) {
  442. return callExpression(memberExpression(memberExpression(memberExpression(identifier("Array"), identifier("prototype")), identifier("slice")), identifier("call")), [node]);
  443. }
  444. let helperName;
  445. const args = [node];
  446. if (i === true) {
  447. helperName = "toConsumableArray";
  448. } else if (i) {
  449. args.push(numericLiteral(i));
  450. helperName = "slicedToArray";
  451. } else {
  452. helperName = "toArray";
  453. }
  454. if (arrayLikeIsIterable) {
  455. args.unshift(this.hub.addHelper(helperName));
  456. helperName = "maybeArrayLike";
  457. }
  458. return callExpression(this.hub.addHelper(helperName), args);
  459. }
  460. hasLabel(name) {
  461. return !!this.getLabel(name);
  462. }
  463. getLabel(name) {
  464. return this.labels.get(name);
  465. }
  466. registerLabel(path) {
  467. this.labels.set(path.node.label.name, path);
  468. }
  469. registerDeclaration(path) {
  470. if (path.isLabeledStatement()) {
  471. this.registerLabel(path);
  472. } else if (path.isFunctionDeclaration()) {
  473. this.registerBinding("hoisted", path.get("id"), path);
  474. } else if (path.isVariableDeclaration()) {
  475. const declarations = path.get("declarations");
  476. for (const declar of declarations) {
  477. this.registerBinding(path.node.kind, declar);
  478. }
  479. } else if (path.isClassDeclaration()) {
  480. if (path.node.declare) return;
  481. this.registerBinding("let", path);
  482. } else if (path.isImportDeclaration()) {
  483. const specifiers = path.get("specifiers");
  484. for (const specifier of specifiers) {
  485. this.registerBinding("module", specifier);
  486. }
  487. } else if (path.isExportDeclaration()) {
  488. const declar = path.get("declaration");
  489. if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) {
  490. this.registerDeclaration(declar);
  491. }
  492. } else {
  493. this.registerBinding("unknown", path);
  494. }
  495. }
  496. buildUndefinedNode() {
  497. return unaryExpression("void", numericLiteral(0), true);
  498. }
  499. registerConstantViolation(path) {
  500. const ids = path.getBindingIdentifiers();
  501. for (const name of Object.keys(ids)) {
  502. const binding = this.getBinding(name);
  503. if (binding) binding.reassign(path);
  504. }
  505. }
  506. registerBinding(kind, path, bindingPath = path) {
  507. if (!kind) throw new ReferenceError("no `kind`");
  508. if (path.isVariableDeclaration()) {
  509. const declarators = path.get("declarations");
  510. for (const declar of declarators) {
  511. this.registerBinding(kind, declar);
  512. }
  513. return;
  514. }
  515. const parent = this.getProgramParent();
  516. const ids = path.getOuterBindingIdentifiers(true);
  517. for (const name of Object.keys(ids)) {
  518. parent.references[name] = true;
  519. for (const id of ids[name]) {
  520. const local = this.getOwnBinding(name);
  521. if (local) {
  522. if (local.identifier === id) continue;
  523. this.checkBlockScopedCollisions(local, kind, name, id);
  524. }
  525. if (local) {
  526. this.registerConstantViolation(bindingPath);
  527. } else {
  528. this.bindings[name] = new _binding.default({
  529. identifier: id,
  530. scope: this,
  531. path: bindingPath,
  532. kind: kind
  533. });
  534. }
  535. }
  536. }
  537. }
  538. addGlobal(node) {
  539. this.globals[node.name] = node;
  540. }
  541. hasUid(name) {
  542. let scope = this;
  543. do {
  544. if (scope.uids[name]) return true;
  545. } while (scope = scope.parent);
  546. return false;
  547. }
  548. hasGlobal(name) {
  549. let scope = this;
  550. do {
  551. if (scope.globals[name]) return true;
  552. } while (scope = scope.parent);
  553. return false;
  554. }
  555. hasReference(name) {
  556. return !!this.getProgramParent().references[name];
  557. }
  558. isPure(node, constantsOnly) {
  559. if (isIdentifier(node)) {
  560. const binding = this.getBinding(node.name);
  561. if (!binding) return false;
  562. if (constantsOnly) return binding.constant;
  563. return true;
  564. } else if (isThisExpression(node) || isMetaProperty(node) || isTopicReference(node) || isPrivateName(node)) {
  565. return true;
  566. } else if (isClass(node)) {
  567. var _node$decorators;
  568. if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
  569. return false;
  570. }
  571. if (((_node$decorators = node.decorators) == null ? void 0 : _node$decorators.length) > 0) {
  572. return false;
  573. }
  574. return this.isPure(node.body, constantsOnly);
  575. } else if (isClassBody(node)) {
  576. for (const method of node.body) {
  577. if (!this.isPure(method, constantsOnly)) return false;
  578. }
  579. return true;
  580. } else if (isBinary(node)) {
  581. return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
  582. } else if (isArrayExpression(node) || isTupleExpression(node)) {
  583. for (const elem of node.elements) {
  584. if (elem !== null && !this.isPure(elem, constantsOnly)) return false;
  585. }
  586. return true;
  587. } else if (isObjectExpression(node) || isRecordExpression(node)) {
  588. for (const prop of node.properties) {
  589. if (!this.isPure(prop, constantsOnly)) return false;
  590. }
  591. return true;
  592. } else if (isMethod(node)) {
  593. var _node$decorators2;
  594. if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
  595. if (((_node$decorators2 = node.decorators) == null ? void 0 : _node$decorators2.length) > 0) {
  596. return false;
  597. }
  598. return true;
  599. } else if (isProperty(node)) {
  600. var _node$decorators3;
  601. if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
  602. if (((_node$decorators3 = node.decorators) == null ? void 0 : _node$decorators3.length) > 0) {
  603. return false;
  604. }
  605. if (isObjectProperty(node) || node.static) {
  606. if (node.value !== null && !this.isPure(node.value, constantsOnly)) {
  607. return false;
  608. }
  609. }
  610. return true;
  611. } else if (isUnaryExpression(node)) {
  612. return this.isPure(node.argument, constantsOnly);
  613. } else if (isTaggedTemplateExpression(node)) {
  614. return matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
  615. } else if (isTemplateLiteral(node)) {
  616. for (const expression of node.expressions) {
  617. if (!this.isPure(expression, constantsOnly)) return false;
  618. }
  619. return true;
  620. } else {
  621. return isPureish(node);
  622. }
  623. }
  624. setData(key, val) {
  625. return this.data[key] = val;
  626. }
  627. getData(key) {
  628. let scope = this;
  629. do {
  630. const data = scope.data[key];
  631. if (data != null) return data;
  632. } while (scope = scope.parent);
  633. }
  634. removeData(key) {
  635. let scope = this;
  636. do {
  637. const data = scope.data[key];
  638. if (data != null) scope.data[key] = null;
  639. } while (scope = scope.parent);
  640. }
  641. init() {
  642. if (!this.inited) {
  643. this.inited = true;
  644. this.crawl();
  645. }
  646. }
  647. crawl() {
  648. const path = this.path;
  649. this.references = Object.create(null);
  650. this.bindings = Object.create(null);
  651. this.globals = Object.create(null);
  652. this.uids = Object.create(null);
  653. this.data = Object.create(null);
  654. const programParent = this.getProgramParent();
  655. if (programParent.crawling) return;
  656. const state = {
  657. references: [],
  658. constantViolations: [],
  659. assignments: []
  660. };
  661. this.crawling = true;
  662. if (path.type !== "Program" && collectorVisitor._exploded) {
  663. for (const visit of collectorVisitor.enter) {
  664. visit(path, state);
  665. }
  666. const typeVisitors = collectorVisitor[path.type];
  667. if (typeVisitors) {
  668. for (const visit of typeVisitors.enter) {
  669. visit(path, state);
  670. }
  671. }
  672. }
  673. path.traverse(collectorVisitor, state);
  674. this.crawling = false;
  675. for (const path of state.assignments) {
  676. const ids = path.getBindingIdentifiers();
  677. for (const name of Object.keys(ids)) {
  678. if (path.scope.getBinding(name)) continue;
  679. programParent.addGlobal(ids[name]);
  680. }
  681. path.scope.registerConstantViolation(path);
  682. }
  683. for (const ref of state.references) {
  684. const binding = ref.scope.getBinding(ref.node.name);
  685. if (binding) {
  686. binding.reference(ref);
  687. } else {
  688. programParent.addGlobal(ref.node);
  689. }
  690. }
  691. for (const path of state.constantViolations) {
  692. path.scope.registerConstantViolation(path);
  693. }
  694. }
  695. push(opts) {
  696. let path = this.path;
  697. if (path.isPattern()) {
  698. path = this.getPatternParent().path;
  699. } else if (!path.isBlockStatement() && !path.isProgram()) {
  700. path = this.getBlockParent().path;
  701. }
  702. if (path.isSwitchStatement()) {
  703. path = (this.getFunctionParent() || this.getProgramParent()).path;
  704. }
  705. if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
  706. path.ensureBlock();
  707. path = path.get("body");
  708. }
  709. const unique = opts.unique;
  710. const kind = opts.kind || "var";
  711. const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
  712. const dataKey = `declaration:${kind}:${blockHoist}`;
  713. let declarPath = !unique && path.getData(dataKey);
  714. if (!declarPath) {
  715. const declar = variableDeclaration(kind, []);
  716. declar._blockHoist = blockHoist;
  717. [declarPath] = path.unshiftContainer("body", [declar]);
  718. if (!unique) path.setData(dataKey, declarPath);
  719. }
  720. const declarator = variableDeclarator(opts.id, opts.init);
  721. const len = declarPath.node.declarations.push(declarator);
  722. path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
  723. }
  724. getProgramParent() {
  725. let scope = this;
  726. do {
  727. if (scope.path.isProgram()) {
  728. return scope;
  729. }
  730. } while (scope = scope.parent);
  731. throw new Error("Couldn't find a Program");
  732. }
  733. getFunctionParent() {
  734. let scope = this;
  735. do {
  736. if (scope.path.isFunctionParent()) {
  737. return scope;
  738. }
  739. } while (scope = scope.parent);
  740. return null;
  741. }
  742. getBlockParent() {
  743. let scope = this;
  744. do {
  745. if (scope.path.isBlockParent()) {
  746. return scope;
  747. }
  748. } while (scope = scope.parent);
  749. throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
  750. }
  751. getPatternParent() {
  752. let scope = this;
  753. do {
  754. if (!scope.path.isPattern()) {
  755. return scope.getBlockParent();
  756. }
  757. } while (scope = scope.parent.parent);
  758. throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
  759. }
  760. getAllBindings() {
  761. const ids = Object.create(null);
  762. let scope = this;
  763. do {
  764. for (const key of Object.keys(scope.bindings)) {
  765. if (key in ids === false) {
  766. ids[key] = scope.bindings[key];
  767. }
  768. }
  769. scope = scope.parent;
  770. } while (scope);
  771. return ids;
  772. }
  773. getAllBindingsOfKind(...kinds) {
  774. const ids = Object.create(null);
  775. for (const kind of kinds) {
  776. let scope = this;
  777. do {
  778. for (const name of Object.keys(scope.bindings)) {
  779. const binding = scope.bindings[name];
  780. if (binding.kind === kind) ids[name] = binding;
  781. }
  782. scope = scope.parent;
  783. } while (scope);
  784. }
  785. return ids;
  786. }
  787. bindingIdentifierEquals(name, node) {
  788. return this.getBindingIdentifier(name) === node;
  789. }
  790. getBinding(name) {
  791. let scope = this;
  792. let previousPath;
  793. do {
  794. const binding = scope.getOwnBinding(name);
  795. if (binding) {
  796. var _previousPath;
  797. if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {} else {
  798. return binding;
  799. }
  800. } else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
  801. break;
  802. }
  803. previousPath = scope.path;
  804. } while (scope = scope.parent);
  805. }
  806. getOwnBinding(name) {
  807. return this.bindings[name];
  808. }
  809. getBindingIdentifier(name) {
  810. var _this$getBinding;
  811. return (_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.identifier;
  812. }
  813. getOwnBindingIdentifier(name) {
  814. const binding = this.bindings[name];
  815. return binding == null ? void 0 : binding.identifier;
  816. }
  817. hasOwnBinding(name) {
  818. return !!this.getOwnBinding(name);
  819. }
  820. hasBinding(name, noGlobals) {
  821. if (!name) return false;
  822. if (this.hasOwnBinding(name)) return true;
  823. if (this.parentHasBinding(name, noGlobals)) return true;
  824. if (this.hasUid(name)) return true;
  825. if (!noGlobals && Scope.globals.includes(name)) return true;
  826. if (!noGlobals && Scope.contextVariables.includes(name)) return true;
  827. return false;
  828. }
  829. parentHasBinding(name, noGlobals) {
  830. var _this$parent;
  831. return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, noGlobals);
  832. }
  833. moveBindingTo(name, scope) {
  834. const info = this.getBinding(name);
  835. if (info) {
  836. info.scope.removeOwnBinding(name);
  837. info.scope = scope;
  838. scope.bindings[name] = info;
  839. }
  840. }
  841. removeOwnBinding(name) {
  842. delete this.bindings[name];
  843. }
  844. removeBinding(name) {
  845. var _this$getBinding2;
  846. (_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.scope.removeOwnBinding(name);
  847. let scope = this;
  848. do {
  849. if (scope.uids[name]) {
  850. scope.uids[name] = false;
  851. }
  852. } while (scope = scope.parent);
  853. }
  854. }
  855. exports.default = Scope;
  856. Scope.globals = Object.keys(_globals.builtin);
  857. Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];