项目原始demo,不改动
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
Це архівний репозитарій. Ви можете переглядати і клонувати файли, але не можете робити пуш або відкривати питання/запити.
 
 
 
 

1013 рядки
28 KiB

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