index.js 27 KB

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