index.js 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
  254. path.scope.registerBinding("local", path.get("id"), path);
  255. }
  256. const params = path.get("params");
  257. for (const param of params) {
  258. path.scope.registerBinding("param", param);
  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 === "let" || 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. this.registerBinding("let", path);
  473. } else if (path.isImportDeclaration()) {
  474. const specifiers = path.get("specifiers");
  475. for (const specifier of specifiers) {
  476. this.registerBinding("module", specifier);
  477. }
  478. } else if (path.isExportDeclaration()) {
  479. const declar = path.get("declaration");
  480. if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) {
  481. this.registerDeclaration(declar);
  482. }
  483. } else {
  484. this.registerBinding("unknown", path);
  485. }
  486. }
  487. buildUndefinedNode() {
  488. return unaryExpression("void", numericLiteral(0), true);
  489. }
  490. registerConstantViolation(path) {
  491. const ids = path.getBindingIdentifiers();
  492. for (const name of Object.keys(ids)) {
  493. const binding = this.getBinding(name);
  494. if (binding) binding.reassign(path);
  495. }
  496. }
  497. registerBinding(kind, path, bindingPath = path) {
  498. if (!kind) throw new ReferenceError("no `kind`");
  499. if (path.isVariableDeclaration()) {
  500. const declarators = path.get("declarations");
  501. for (const declar of declarators) {
  502. this.registerBinding(kind, declar);
  503. }
  504. return;
  505. }
  506. const parent = this.getProgramParent();
  507. const ids = path.getOuterBindingIdentifiers(true);
  508. for (const name of Object.keys(ids)) {
  509. parent.references[name] = true;
  510. for (const id of ids[name]) {
  511. const local = this.getOwnBinding(name);
  512. if (local) {
  513. if (local.identifier === id) continue;
  514. this.checkBlockScopedCollisions(local, kind, name, id);
  515. }
  516. if (local) {
  517. this.registerConstantViolation(bindingPath);
  518. } else {
  519. this.bindings[name] = new _binding.default({
  520. identifier: id,
  521. scope: this,
  522. path: bindingPath,
  523. kind: kind
  524. });
  525. }
  526. }
  527. }
  528. }
  529. addGlobal(node) {
  530. this.globals[node.name] = node;
  531. }
  532. hasUid(name) {
  533. let scope = this;
  534. do {
  535. if (scope.uids[name]) return true;
  536. } while (scope = scope.parent);
  537. return false;
  538. }
  539. hasGlobal(name) {
  540. let scope = this;
  541. do {
  542. if (scope.globals[name]) return true;
  543. } while (scope = scope.parent);
  544. return false;
  545. }
  546. hasReference(name) {
  547. return !!this.getProgramParent().references[name];
  548. }
  549. isPure(node, constantsOnly) {
  550. if (isIdentifier(node)) {
  551. const binding = this.getBinding(node.name);
  552. if (!binding) return false;
  553. if (constantsOnly) return binding.constant;
  554. return true;
  555. } else if (isClass(node)) {
  556. if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
  557. return false;
  558. }
  559. return this.isPure(node.body, constantsOnly);
  560. } else if (isClassBody(node)) {
  561. for (const method of node.body) {
  562. if (!this.isPure(method, constantsOnly)) return false;
  563. }
  564. return true;
  565. } else if (isBinary(node)) {
  566. return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
  567. } else if (isArrayExpression(node)) {
  568. for (const elem of node.elements) {
  569. if (!this.isPure(elem, constantsOnly)) return false;
  570. }
  571. return true;
  572. } else if (isObjectExpression(node)) {
  573. for (const prop of node.properties) {
  574. if (!this.isPure(prop, constantsOnly)) return false;
  575. }
  576. return true;
  577. } else if (isMethod(node)) {
  578. if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
  579. if (node.kind === "get" || node.kind === "set") return false;
  580. return true;
  581. } else if (isProperty(node)) {
  582. if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
  583. return this.isPure(node.value, constantsOnly);
  584. } else if (isUnaryExpression(node)) {
  585. return this.isPure(node.argument, constantsOnly);
  586. } else if (isTaggedTemplateExpression(node)) {
  587. return matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
  588. } else if (isTemplateLiteral(node)) {
  589. for (const expression of node.expressions) {
  590. if (!this.isPure(expression, constantsOnly)) return false;
  591. }
  592. return true;
  593. } else {
  594. return isPureish(node);
  595. }
  596. }
  597. setData(key, val) {
  598. return this.data[key] = val;
  599. }
  600. getData(key) {
  601. let scope = this;
  602. do {
  603. const data = scope.data[key];
  604. if (data != null) return data;
  605. } while (scope = scope.parent);
  606. }
  607. removeData(key) {
  608. let scope = this;
  609. do {
  610. const data = scope.data[key];
  611. if (data != null) scope.data[key] = null;
  612. } while (scope = scope.parent);
  613. }
  614. init() {
  615. if (!this.inited) {
  616. this.inited = true;
  617. this.crawl();
  618. }
  619. }
  620. crawl() {
  621. const path = this.path;
  622. this.references = Object.create(null);
  623. this.bindings = Object.create(null);
  624. this.globals = Object.create(null);
  625. this.uids = Object.create(null);
  626. this.data = Object.create(null);
  627. const programParent = this.getProgramParent();
  628. if (programParent.crawling) return;
  629. const state = {
  630. references: [],
  631. constantViolations: [],
  632. assignments: []
  633. };
  634. this.crawling = true;
  635. if (path.type !== "Program" && collectorVisitor._exploded) {
  636. for (const visit of collectorVisitor.enter) {
  637. visit(path, state);
  638. }
  639. const typeVisitors = collectorVisitor[path.type];
  640. if (typeVisitors) {
  641. for (const visit of typeVisitors.enter) {
  642. visit(path, state);
  643. }
  644. }
  645. }
  646. path.traverse(collectorVisitor, state);
  647. this.crawling = false;
  648. for (const path of state.assignments) {
  649. const ids = path.getBindingIdentifiers();
  650. for (const name of Object.keys(ids)) {
  651. if (path.scope.getBinding(name)) continue;
  652. programParent.addGlobal(ids[name]);
  653. }
  654. path.scope.registerConstantViolation(path);
  655. }
  656. for (const ref of state.references) {
  657. const binding = ref.scope.getBinding(ref.node.name);
  658. if (binding) {
  659. binding.reference(ref);
  660. } else {
  661. programParent.addGlobal(ref.node);
  662. }
  663. }
  664. for (const path of state.constantViolations) {
  665. path.scope.registerConstantViolation(path);
  666. }
  667. }
  668. push(opts) {
  669. let path = this.path;
  670. if (!path.isBlockStatement() && !path.isProgram()) {
  671. path = this.getBlockParent().path;
  672. }
  673. if (path.isSwitchStatement()) {
  674. path = (this.getFunctionParent() || this.getProgramParent()).path;
  675. }
  676. if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
  677. path.ensureBlock();
  678. path = path.get("body");
  679. }
  680. const unique = opts.unique;
  681. const kind = opts.kind || "var";
  682. const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
  683. const dataKey = `declaration:${kind}:${blockHoist}`;
  684. let declarPath = !unique && path.getData(dataKey);
  685. if (!declarPath) {
  686. const declar = variableDeclaration(kind, []);
  687. declar._blockHoist = blockHoist;
  688. [declarPath] = path.unshiftContainer("body", [declar]);
  689. if (!unique) path.setData(dataKey, declarPath);
  690. }
  691. const declarator = variableDeclarator(opts.id, opts.init);
  692. declarPath.node.declarations.push(declarator);
  693. this.registerBinding(kind, declarPath.get("declarations").pop());
  694. }
  695. getProgramParent() {
  696. let scope = this;
  697. do {
  698. if (scope.path.isProgram()) {
  699. return scope;
  700. }
  701. } while (scope = scope.parent);
  702. throw new Error("Couldn't find a Program");
  703. }
  704. getFunctionParent() {
  705. let scope = this;
  706. do {
  707. if (scope.path.isFunctionParent()) {
  708. return scope;
  709. }
  710. } while (scope = scope.parent);
  711. return null;
  712. }
  713. getBlockParent() {
  714. let scope = this;
  715. do {
  716. if (scope.path.isBlockParent()) {
  717. return scope;
  718. }
  719. } while (scope = scope.parent);
  720. throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
  721. }
  722. getAllBindings() {
  723. const ids = Object.create(null);
  724. let scope = this;
  725. do {
  726. for (const key of Object.keys(scope.bindings)) {
  727. if (key in ids === false) {
  728. ids[key] = scope.bindings[key];
  729. }
  730. }
  731. scope = scope.parent;
  732. } while (scope);
  733. return ids;
  734. }
  735. getAllBindingsOfKind(...kinds) {
  736. const ids = Object.create(null);
  737. for (const kind of kinds) {
  738. let scope = this;
  739. do {
  740. for (const name of Object.keys(scope.bindings)) {
  741. const binding = scope.bindings[name];
  742. if (binding.kind === kind) ids[name] = binding;
  743. }
  744. scope = scope.parent;
  745. } while (scope);
  746. }
  747. return ids;
  748. }
  749. bindingIdentifierEquals(name, node) {
  750. return this.getBindingIdentifier(name) === node;
  751. }
  752. getBinding(name) {
  753. let scope = this;
  754. let previousPath;
  755. do {
  756. const binding = scope.getOwnBinding(name);
  757. if (binding) {
  758. var _previousPath;
  759. if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param") {} else {
  760. return binding;
  761. }
  762. }
  763. previousPath = scope.path;
  764. } while (scope = scope.parent);
  765. }
  766. getOwnBinding(name) {
  767. return this.bindings[name];
  768. }
  769. getBindingIdentifier(name) {
  770. var _this$getBinding;
  771. return (_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.identifier;
  772. }
  773. getOwnBindingIdentifier(name) {
  774. const binding = this.bindings[name];
  775. return binding == null ? void 0 : binding.identifier;
  776. }
  777. hasOwnBinding(name) {
  778. return !!this.getOwnBinding(name);
  779. }
  780. hasBinding(name, noGlobals) {
  781. if (!name) return false;
  782. if (this.hasOwnBinding(name)) return true;
  783. if (this.parentHasBinding(name, noGlobals)) return true;
  784. if (this.hasUid(name)) return true;
  785. if (!noGlobals && Scope.globals.includes(name)) return true;
  786. if (!noGlobals && Scope.contextVariables.includes(name)) return true;
  787. return false;
  788. }
  789. parentHasBinding(name, noGlobals) {
  790. var _this$parent;
  791. return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, noGlobals);
  792. }
  793. moveBindingTo(name, scope) {
  794. const info = this.getBinding(name);
  795. if (info) {
  796. info.scope.removeOwnBinding(name);
  797. info.scope = scope;
  798. scope.bindings[name] = info;
  799. }
  800. }
  801. removeOwnBinding(name) {
  802. delete this.bindings[name];
  803. }
  804. removeBinding(name) {
  805. var _this$getBinding2;
  806. (_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.scope.removeOwnBinding(name);
  807. let scope = this;
  808. do {
  809. if (scope.uids[name]) {
  810. scope.uids[name] = false;
  811. }
  812. } while (scope = scope.parent);
  813. }
  814. }
  815. exports.default = Scope;
  816. Scope.globals = Object.keys(_globals.builtin);
  817. Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];