index.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _includes() {
  7. const data = _interopRequireDefault(require("lodash/includes"));
  8. _includes = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _repeat() {
  14. const data = _interopRequireDefault(require("lodash/repeat"));
  15. _repeat = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. var _renamer = _interopRequireDefault(require("./lib/renamer"));
  21. var _index = _interopRequireDefault(require("../index"));
  22. function _defaults() {
  23. const data = _interopRequireDefault(require("lodash/defaults"));
  24. _defaults = function () {
  25. return data;
  26. };
  27. return data;
  28. }
  29. var _binding = _interopRequireDefault(require("./binding"));
  30. function _globals() {
  31. const data = _interopRequireDefault(require("globals"));
  32. _globals = function () {
  33. return data;
  34. };
  35. return data;
  36. }
  37. function t() {
  38. const data = _interopRequireWildcard(require("@babel/types"));
  39. t = function () {
  40. return data;
  41. };
  42. return data;
  43. }
  44. var _cache = require("../cache");
  45. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  46. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  47. function gatherNodeParts(node, parts) {
  48. if (t().isModuleDeclaration(node)) {
  49. if (node.source) {
  50. gatherNodeParts(node.source, parts);
  51. } else if (node.specifiers && node.specifiers.length) {
  52. for (const specifier of node.specifiers) {
  53. gatherNodeParts(specifier, parts);
  54. }
  55. } else if (node.declaration) {
  56. gatherNodeParts(node.declaration, parts);
  57. }
  58. } else if (t().isModuleSpecifier(node)) {
  59. gatherNodeParts(node.local, parts);
  60. } else if (t().isMemberExpression(node)) {
  61. gatherNodeParts(node.object, parts);
  62. gatherNodeParts(node.property, parts);
  63. } else if (t().isIdentifier(node)) {
  64. parts.push(node.name);
  65. } else if (t().isLiteral(node)) {
  66. parts.push(node.value);
  67. } else if (t().isCallExpression(node)) {
  68. gatherNodeParts(node.callee, parts);
  69. } else if (t().isObjectExpression(node) || t().isObjectPattern(node)) {
  70. for (const prop of node.properties) {
  71. gatherNodeParts(prop.key || prop.argument, parts);
  72. }
  73. } else if (t().isPrivateName(node)) {
  74. gatherNodeParts(node.id, parts);
  75. } else if (t().isThisExpression(node)) {
  76. parts.push("this");
  77. } else if (t().isSuper(node)) {
  78. parts.push("super");
  79. }
  80. }
  81. const collectorVisitor = {
  82. For(path) {
  83. for (const key of t().FOR_INIT_KEYS) {
  84. const declar = path.get(key);
  85. if (declar.isVar()) {
  86. const parentScope = path.scope.getFunctionParent() || path.scope.getProgramParent();
  87. parentScope.registerBinding("var", declar);
  88. }
  89. }
  90. },
  91. Declaration(path) {
  92. if (path.isBlockScoped()) return;
  93. if (path.isExportDeclaration() && path.get("declaration").isDeclaration()) {
  94. return;
  95. }
  96. const parent = path.scope.getFunctionParent() || path.scope.getProgramParent();
  97. parent.registerDeclaration(path);
  98. },
  99. ReferencedIdentifier(path, state) {
  100. state.references.push(path);
  101. },
  102. ForXStatement(path, state) {
  103. const left = path.get("left");
  104. if (left.isPattern() || left.isIdentifier()) {
  105. state.constantViolations.push(path);
  106. }
  107. },
  108. ExportDeclaration: {
  109. exit(path) {
  110. const {
  111. node,
  112. scope
  113. } = path;
  114. const declar = node.declaration;
  115. if (t().isClassDeclaration(declar) || t().isFunctionDeclaration(declar)) {
  116. const id = declar.id;
  117. if (!id) return;
  118. const binding = scope.getBinding(id.name);
  119. if (binding) binding.reference(path);
  120. } else if (t().isVariableDeclaration(declar)) {
  121. for (const decl of declar.declarations) {
  122. for (const name of Object.keys(t().getBindingIdentifiers(decl))) {
  123. const binding = scope.getBinding(name);
  124. if (binding) binding.reference(path);
  125. }
  126. }
  127. }
  128. }
  129. },
  130. LabeledStatement(path) {
  131. path.scope.getProgramParent().addGlobal(path.node);
  132. path.scope.getBlockParent().registerDeclaration(path);
  133. },
  134. AssignmentExpression(path, state) {
  135. state.assignments.push(path);
  136. },
  137. UpdateExpression(path, state) {
  138. state.constantViolations.push(path);
  139. },
  140. UnaryExpression(path, state) {
  141. if (path.node.operator === "delete") {
  142. state.constantViolations.push(path);
  143. }
  144. },
  145. BlockScoped(path) {
  146. let scope = path.scope;
  147. if (scope.path === path) scope = scope.parent;
  148. scope.getBlockParent().registerDeclaration(path);
  149. },
  150. ClassDeclaration(path) {
  151. const id = path.node.id;
  152. if (!id) return;
  153. const name = id.name;
  154. path.scope.bindings[name] = path.scope.getBinding(name);
  155. },
  156. Block(path) {
  157. const paths = path.get("body");
  158. for (const bodyPath of paths) {
  159. if (bodyPath.isFunctionDeclaration()) {
  160. path.scope.getBlockParent().registerDeclaration(bodyPath);
  161. }
  162. }
  163. }
  164. };
  165. let uid = 0;
  166. class Scope {
  167. constructor(path) {
  168. const {
  169. node
  170. } = path;
  171. const cached = _cache.scope.get(node);
  172. if (cached && cached.path === path) {
  173. return cached;
  174. }
  175. _cache.scope.set(node, this);
  176. this.uid = uid++;
  177. this.block = node;
  178. this.path = path;
  179. this.labels = new Map();
  180. }
  181. get parent() {
  182. const parent = this.path.findParent(p => p.isScope());
  183. return parent && parent.scope;
  184. }
  185. get parentBlock() {
  186. return this.path.parent;
  187. }
  188. get hub() {
  189. return this.path.hub;
  190. }
  191. traverse(node, opts, state) {
  192. (0, _index.default)(node, opts, this, state, this.path);
  193. }
  194. generateDeclaredUidIdentifier(name) {
  195. const id = this.generateUidIdentifier(name);
  196. this.push({
  197. id
  198. });
  199. return t().cloneNode(id);
  200. }
  201. generateUidIdentifier(name) {
  202. return t().identifier(this.generateUid(name));
  203. }
  204. generateUid(name = "temp") {
  205. name = t().toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
  206. let uid;
  207. let i = 0;
  208. do {
  209. uid = this._generateUid(name, i);
  210. i++;
  211. } while (this.hasLabel(uid) || this.hasBinding(uid) || this.hasGlobal(uid) || this.hasReference(uid));
  212. const program = this.getProgramParent();
  213. program.references[uid] = true;
  214. program.uids[uid] = true;
  215. return uid;
  216. }
  217. _generateUid(name, i) {
  218. let id = name;
  219. if (i > 1) id += i;
  220. return `_${id}`;
  221. }
  222. generateUidBasedOnNode(parent, defaultName) {
  223. let node = parent;
  224. if (t().isAssignmentExpression(parent)) {
  225. node = parent.left;
  226. } else if (t().isVariableDeclarator(parent)) {
  227. node = parent.id;
  228. } else if (t().isObjectProperty(node) || t().isObjectMethod(node)) {
  229. node = node.key;
  230. }
  231. const parts = [];
  232. gatherNodeParts(node, parts);
  233. let id = parts.join("$");
  234. id = id.replace(/^_/, "") || defaultName || "ref";
  235. return this.generateUid(id.slice(0, 20));
  236. }
  237. generateUidIdentifierBasedOnNode(parent, defaultName) {
  238. return t().identifier(this.generateUidBasedOnNode(parent, defaultName));
  239. }
  240. isStatic(node) {
  241. if (t().isThisExpression(node) || t().isSuper(node)) {
  242. return true;
  243. }
  244. if (t().isIdentifier(node)) {
  245. const binding = this.getBinding(node.name);
  246. if (binding) {
  247. return binding.constant;
  248. } else {
  249. return this.hasBinding(node.name);
  250. }
  251. }
  252. return false;
  253. }
  254. maybeGenerateMemoised(node, dontPush) {
  255. if (this.isStatic(node)) {
  256. return null;
  257. } else {
  258. const id = this.generateUidIdentifierBasedOnNode(node);
  259. if (!dontPush) {
  260. this.push({
  261. id
  262. });
  263. return t().cloneNode(id);
  264. }
  265. return id;
  266. }
  267. }
  268. checkBlockScopedCollisions(local, kind, name, id) {
  269. if (kind === "param") return;
  270. if (local.kind === "local") return;
  271. const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && (kind === "let" || kind === "const");
  272. if (duplicate) {
  273. throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
  274. }
  275. }
  276. rename(oldName, newName, block) {
  277. const binding = this.getBinding(oldName);
  278. if (binding) {
  279. newName = newName || this.generateUidIdentifier(oldName).name;
  280. return new _renamer.default(binding, oldName, newName).rename(block);
  281. }
  282. }
  283. _renameFromMap(map, oldName, newName, value) {
  284. if (map[oldName]) {
  285. map[newName] = value;
  286. map[oldName] = null;
  287. }
  288. }
  289. dump() {
  290. const sep = (0, _repeat().default)("-", 60);
  291. console.log(sep);
  292. let scope = this;
  293. do {
  294. console.log("#", scope.block.type);
  295. for (const name of Object.keys(scope.bindings)) {
  296. const binding = scope.bindings[name];
  297. console.log(" -", name, {
  298. constant: binding.constant,
  299. references: binding.references,
  300. violations: binding.constantViolations.length,
  301. kind: binding.kind
  302. });
  303. }
  304. } while (scope = scope.parent);
  305. console.log(sep);
  306. }
  307. toArray(node, i) {
  308. if (t().isIdentifier(node)) {
  309. const binding = this.getBinding(node.name);
  310. if (binding && binding.constant && binding.path.isGenericType("Array")) {
  311. return node;
  312. }
  313. }
  314. if (t().isArrayExpression(node)) {
  315. return node;
  316. }
  317. if (t().isIdentifier(node, {
  318. name: "arguments"
  319. })) {
  320. return t().callExpression(t().memberExpression(t().memberExpression(t().memberExpression(t().identifier("Array"), t().identifier("prototype")), t().identifier("slice")), t().identifier("call")), [node]);
  321. }
  322. let helperName;
  323. const args = [node];
  324. if (i === true) {
  325. helperName = "toConsumableArray";
  326. } else if (i) {
  327. args.push(t().numericLiteral(i));
  328. helperName = "slicedToArray";
  329. } else {
  330. helperName = "toArray";
  331. }
  332. return t().callExpression(this.hub.addHelper(helperName), args);
  333. }
  334. hasLabel(name) {
  335. return !!this.getLabel(name);
  336. }
  337. getLabel(name) {
  338. return this.labels.get(name);
  339. }
  340. registerLabel(path) {
  341. this.labels.set(path.node.label.name, path);
  342. }
  343. registerDeclaration(path) {
  344. if (path.isLabeledStatement()) {
  345. this.registerLabel(path);
  346. } else if (path.isFunctionDeclaration()) {
  347. this.registerBinding("hoisted", path.get("id"), path);
  348. } else if (path.isVariableDeclaration()) {
  349. const declarations = path.get("declarations");
  350. for (const declar of declarations) {
  351. this.registerBinding(path.node.kind, declar);
  352. }
  353. } else if (path.isClassDeclaration()) {
  354. this.registerBinding("let", path);
  355. } else if (path.isImportDeclaration()) {
  356. const specifiers = path.get("specifiers");
  357. for (const specifier of specifiers) {
  358. this.registerBinding("module", specifier);
  359. }
  360. } else if (path.isExportDeclaration()) {
  361. const declar = path.get("declaration");
  362. if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) {
  363. this.registerDeclaration(declar);
  364. }
  365. } else {
  366. this.registerBinding("unknown", path);
  367. }
  368. }
  369. buildUndefinedNode() {
  370. if (this.hasBinding("undefined")) {
  371. return t().unaryExpression("void", t().numericLiteral(0), true);
  372. } else {
  373. return t().identifier("undefined");
  374. }
  375. }
  376. registerConstantViolation(path) {
  377. const ids = path.getBindingIdentifiers();
  378. for (const name of Object.keys(ids)) {
  379. const binding = this.getBinding(name);
  380. if (binding) binding.reassign(path);
  381. }
  382. }
  383. registerBinding(kind, path, bindingPath = path) {
  384. if (!kind) throw new ReferenceError("no `kind`");
  385. if (path.isVariableDeclaration()) {
  386. const declarators = path.get("declarations");
  387. for (const declar of declarators) {
  388. this.registerBinding(kind, declar);
  389. }
  390. return;
  391. }
  392. const parent = this.getProgramParent();
  393. const ids = path.getOuterBindingIdentifiers(true);
  394. for (const name of Object.keys(ids)) {
  395. for (const id of ids[name]) {
  396. const local = this.getOwnBinding(name);
  397. if (local) {
  398. if (local.identifier === id) continue;
  399. this.checkBlockScopedCollisions(local, kind, name, id);
  400. }
  401. parent.references[name] = true;
  402. if (local) {
  403. this.registerConstantViolation(bindingPath);
  404. } else {
  405. this.bindings[name] = new _binding.default({
  406. identifier: id,
  407. scope: this,
  408. path: bindingPath,
  409. kind: kind
  410. });
  411. }
  412. }
  413. }
  414. }
  415. addGlobal(node) {
  416. this.globals[node.name] = node;
  417. }
  418. hasUid(name) {
  419. let scope = this;
  420. do {
  421. if (scope.uids[name]) return true;
  422. } while (scope = scope.parent);
  423. return false;
  424. }
  425. hasGlobal(name) {
  426. let scope = this;
  427. do {
  428. if (scope.globals[name]) return true;
  429. } while (scope = scope.parent);
  430. return false;
  431. }
  432. hasReference(name) {
  433. let scope = this;
  434. do {
  435. if (scope.references[name]) return true;
  436. } while (scope = scope.parent);
  437. return false;
  438. }
  439. isPure(node, constantsOnly) {
  440. if (t().isIdentifier(node)) {
  441. const binding = this.getBinding(node.name);
  442. if (!binding) return false;
  443. if (constantsOnly) return binding.constant;
  444. return true;
  445. } else if (t().isClass(node)) {
  446. if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
  447. return false;
  448. }
  449. return this.isPure(node.body, constantsOnly);
  450. } else if (t().isClassBody(node)) {
  451. for (const method of node.body) {
  452. if (!this.isPure(method, constantsOnly)) return false;
  453. }
  454. return true;
  455. } else if (t().isBinary(node)) {
  456. return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
  457. } else if (t().isArrayExpression(node)) {
  458. for (const elem of node.elements) {
  459. if (!this.isPure(elem, constantsOnly)) return false;
  460. }
  461. return true;
  462. } else if (t().isObjectExpression(node)) {
  463. for (const prop of node.properties) {
  464. if (!this.isPure(prop, constantsOnly)) return false;
  465. }
  466. return true;
  467. } else if (t().isClassMethod(node)) {
  468. if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
  469. if (node.kind === "get" || node.kind === "set") return false;
  470. return true;
  471. } else if (t().isProperty(node)) {
  472. if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
  473. return this.isPure(node.value, constantsOnly);
  474. } else if (t().isUnaryExpression(node)) {
  475. return this.isPure(node.argument, constantsOnly);
  476. } else if (t().isTaggedTemplateExpression(node)) {
  477. return t().matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
  478. } else if (t().isTemplateLiteral(node)) {
  479. for (const expression of node.expressions) {
  480. if (!this.isPure(expression, constantsOnly)) return false;
  481. }
  482. return true;
  483. } else {
  484. return t().isPureish(node);
  485. }
  486. }
  487. setData(key, val) {
  488. return this.data[key] = val;
  489. }
  490. getData(key) {
  491. let scope = this;
  492. do {
  493. const data = scope.data[key];
  494. if (data != null) return data;
  495. } while (scope = scope.parent);
  496. }
  497. removeData(key) {
  498. let scope = this;
  499. do {
  500. const data = scope.data[key];
  501. if (data != null) scope.data[key] = null;
  502. } while (scope = scope.parent);
  503. }
  504. init() {
  505. if (!this.references) this.crawl();
  506. }
  507. crawl() {
  508. const path = this.path;
  509. this.references = Object.create(null);
  510. this.bindings = Object.create(null);
  511. this.globals = Object.create(null);
  512. this.uids = Object.create(null);
  513. this.data = Object.create(null);
  514. if (path.isLoop()) {
  515. for (const key of t().FOR_INIT_KEYS) {
  516. const node = path.get(key);
  517. if (node.isBlockScoped()) this.registerBinding(node.node.kind, node);
  518. }
  519. }
  520. if (path.isFunctionExpression() && path.has("id")) {
  521. if (!path.get("id").node[t().NOT_LOCAL_BINDING]) {
  522. this.registerBinding("local", path.get("id"), path);
  523. }
  524. }
  525. if (path.isClassExpression() && path.has("id")) {
  526. if (!path.get("id").node[t().NOT_LOCAL_BINDING]) {
  527. this.registerBinding("local", path);
  528. }
  529. }
  530. if (path.isFunction()) {
  531. const params = path.get("params");
  532. for (const param of params) {
  533. this.registerBinding("param", param);
  534. }
  535. }
  536. if (path.isCatchClause()) {
  537. this.registerBinding("let", path);
  538. }
  539. const parent = this.getProgramParent();
  540. if (parent.crawling) return;
  541. const state = {
  542. references: [],
  543. constantViolations: [],
  544. assignments: []
  545. };
  546. this.crawling = true;
  547. path.traverse(collectorVisitor, state);
  548. this.crawling = false;
  549. for (const path of state.assignments) {
  550. const ids = path.getBindingIdentifiers();
  551. let programParent;
  552. for (const name of Object.keys(ids)) {
  553. if (path.scope.getBinding(name)) continue;
  554. programParent = programParent || path.scope.getProgramParent();
  555. programParent.addGlobal(ids[name]);
  556. }
  557. path.scope.registerConstantViolation(path);
  558. }
  559. for (const ref of state.references) {
  560. const binding = ref.scope.getBinding(ref.node.name);
  561. if (binding) {
  562. binding.reference(ref);
  563. } else {
  564. ref.scope.getProgramParent().addGlobal(ref.node);
  565. }
  566. }
  567. for (const path of state.constantViolations) {
  568. path.scope.registerConstantViolation(path);
  569. }
  570. }
  571. push(opts) {
  572. let path = this.path;
  573. if (!path.isBlockStatement() && !path.isProgram()) {
  574. path = this.getBlockParent().path;
  575. }
  576. if (path.isSwitchStatement()) {
  577. path = (this.getFunctionParent() || this.getProgramParent()).path;
  578. }
  579. if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
  580. path.ensureBlock();
  581. path = path.get("body");
  582. }
  583. const unique = opts.unique;
  584. const kind = opts.kind || "var";
  585. const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
  586. const dataKey = `declaration:${kind}:${blockHoist}`;
  587. let declarPath = !unique && path.getData(dataKey);
  588. if (!declarPath) {
  589. const declar = t().variableDeclaration(kind, []);
  590. declar._blockHoist = blockHoist;
  591. [declarPath] = path.unshiftContainer("body", [declar]);
  592. if (!unique) path.setData(dataKey, declarPath);
  593. }
  594. const declarator = t().variableDeclarator(opts.id, opts.init);
  595. declarPath.node.declarations.push(declarator);
  596. this.registerBinding(kind, declarPath.get("declarations").pop());
  597. }
  598. getProgramParent() {
  599. let scope = this;
  600. do {
  601. if (scope.path.isProgram()) {
  602. return scope;
  603. }
  604. } while (scope = scope.parent);
  605. throw new Error("Couldn't find a Program");
  606. }
  607. getFunctionParent() {
  608. let scope = this;
  609. do {
  610. if (scope.path.isFunctionParent()) {
  611. return scope;
  612. }
  613. } while (scope = scope.parent);
  614. return null;
  615. }
  616. getBlockParent() {
  617. let scope = this;
  618. do {
  619. if (scope.path.isBlockParent()) {
  620. return scope;
  621. }
  622. } while (scope = scope.parent);
  623. throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
  624. }
  625. getAllBindings() {
  626. const ids = Object.create(null);
  627. let scope = this;
  628. do {
  629. (0, _defaults().default)(ids, scope.bindings);
  630. scope = scope.parent;
  631. } while (scope);
  632. return ids;
  633. }
  634. getAllBindingsOfKind() {
  635. const ids = Object.create(null);
  636. for (const kind of arguments) {
  637. let scope = this;
  638. do {
  639. for (const name of Object.keys(scope.bindings)) {
  640. const binding = scope.bindings[name];
  641. if (binding.kind === kind) ids[name] = binding;
  642. }
  643. scope = scope.parent;
  644. } while (scope);
  645. }
  646. return ids;
  647. }
  648. bindingIdentifierEquals(name, node) {
  649. return this.getBindingIdentifier(name) === node;
  650. }
  651. getBinding(name) {
  652. let scope = this;
  653. do {
  654. const binding = scope.getOwnBinding(name);
  655. if (binding) return binding;
  656. } while (scope = scope.parent);
  657. }
  658. getOwnBinding(name) {
  659. return this.bindings[name];
  660. }
  661. getBindingIdentifier(name) {
  662. const info = this.getBinding(name);
  663. return info && info.identifier;
  664. }
  665. getOwnBindingIdentifier(name) {
  666. const binding = this.bindings[name];
  667. return binding && binding.identifier;
  668. }
  669. hasOwnBinding(name) {
  670. return !!this.getOwnBinding(name);
  671. }
  672. hasBinding(name, noGlobals) {
  673. if (!name) return false;
  674. if (this.hasOwnBinding(name)) return true;
  675. if (this.parentHasBinding(name, noGlobals)) return true;
  676. if (this.hasUid(name)) return true;
  677. if (!noGlobals && (0, _includes().default)(Scope.globals, name)) return true;
  678. if (!noGlobals && (0, _includes().default)(Scope.contextVariables, name)) return true;
  679. return false;
  680. }
  681. parentHasBinding(name, noGlobals) {
  682. return this.parent && this.parent.hasBinding(name, noGlobals);
  683. }
  684. moveBindingTo(name, scope) {
  685. const info = this.getBinding(name);
  686. if (info) {
  687. info.scope.removeOwnBinding(name);
  688. info.scope = scope;
  689. scope.bindings[name] = info;
  690. }
  691. }
  692. removeOwnBinding(name) {
  693. delete this.bindings[name];
  694. }
  695. removeBinding(name) {
  696. const info = this.getBinding(name);
  697. if (info) {
  698. info.scope.removeOwnBinding(name);
  699. }
  700. let scope = this;
  701. do {
  702. if (scope.uids[name]) {
  703. scope.uids[name] = false;
  704. }
  705. } while (scope = scope.parent);
  706. }
  707. }
  708. exports.default = Scope;
  709. Scope.globals = Object.keys(_globals().default.builtin);
  710. Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];