index.js 24 KB

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