index.js 25 KB

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