Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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