index.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  7. var _tdz = require("./tdz");
  8. var _core = require("@babel/core");
  9. const DONE = new WeakSet();
  10. var _default = (0, _helperPluginUtils.declare)((api, opts) => {
  11. api.assertVersion(7);
  12. const {
  13. throwIfClosureRequired = false,
  14. tdz: tdzEnabled = false
  15. } = opts;
  16. if (typeof throwIfClosureRequired !== "boolean") {
  17. throw new Error(`.throwIfClosureRequired must be a boolean, or undefined`);
  18. }
  19. if (typeof tdzEnabled !== "boolean") {
  20. throw new Error(`.tdz must be a boolean, or undefined`);
  21. }
  22. return {
  23. name: "transform-block-scoping",
  24. visitor: {
  25. VariableDeclaration(path) {
  26. const {
  27. node,
  28. parent,
  29. scope
  30. } = path;
  31. if (!isBlockScoped(node)) return;
  32. convertBlockScopedToVar(path, null, parent, scope, true);
  33. if (node._tdzThis) {
  34. const nodes = [node];
  35. for (let i = 0; i < node.declarations.length; i++) {
  36. const decl = node.declarations[i];
  37. const assign = _core.types.assignmentExpression("=", _core.types.cloneNode(decl.id), decl.init || scope.buildUndefinedNode());
  38. assign._ignoreBlockScopingTDZ = true;
  39. nodes.push(_core.types.expressionStatement(assign));
  40. decl.init = this.addHelper("temporalUndefined");
  41. }
  42. node._blockHoist = 2;
  43. if (path.isCompletionRecord()) {
  44. nodes.push(_core.types.expressionStatement(scope.buildUndefinedNode()));
  45. }
  46. path.replaceWithMultiple(nodes);
  47. }
  48. },
  49. Loop(path, state) {
  50. const {
  51. parent,
  52. scope
  53. } = path;
  54. path.ensureBlock();
  55. const blockScoping = new BlockScoping(path, path.get("body"), parent, scope, throwIfClosureRequired, tdzEnabled, state);
  56. const replace = blockScoping.run();
  57. if (replace) path.replaceWith(replace);
  58. },
  59. CatchClause(path, state) {
  60. const {
  61. parent,
  62. scope
  63. } = path;
  64. const blockScoping = new BlockScoping(null, path.get("body"), parent, scope, throwIfClosureRequired, tdzEnabled, state);
  65. blockScoping.run();
  66. },
  67. "BlockStatement|SwitchStatement|Program"(path, state) {
  68. if (!ignoreBlock(path)) {
  69. const blockScoping = new BlockScoping(null, path, path.parent, path.scope, throwIfClosureRequired, tdzEnabled, state);
  70. blockScoping.run();
  71. }
  72. }
  73. }
  74. };
  75. });
  76. exports.default = _default;
  77. function ignoreBlock(path) {
  78. return _core.types.isLoop(path.parent) || _core.types.isCatchClause(path.parent);
  79. }
  80. const buildRetCheck = _core.template.statement(`
  81. if (typeof RETURN === "object") return RETURN.v;
  82. `);
  83. function isBlockScoped(node) {
  84. if (!_core.types.isVariableDeclaration(node)) return false;
  85. if (
  86. node[_core.types.BLOCK_SCOPED_SYMBOL]) {
  87. return true;
  88. }
  89. if (node.kind !== "let" && node.kind !== "const" && node.kind !== "using") {
  90. return false;
  91. }
  92. return true;
  93. }
  94. function isInLoop(path) {
  95. const loopOrFunctionParent = path.find(path => path.isLoop() || path.isFunction());
  96. return loopOrFunctionParent == null ? void 0 : loopOrFunctionParent.isLoop();
  97. }
  98. function convertBlockScopedToVar(path, node, parent, scope, moveBindingsToParent = false) {
  99. if (!node) {
  100. node = path.node;
  101. }
  102. if (isInLoop(path) && !_core.types.isFor(parent)) {
  103. for (let i = 0; i < node.declarations.length; i++) {
  104. const declar = node.declarations[i];
  105. declar.init = declar.init || scope.buildUndefinedNode();
  106. }
  107. }
  108. node[_core.types.BLOCK_SCOPED_SYMBOL] = true;
  109. node.kind = "var";
  110. if (moveBindingsToParent) {
  111. const parentScope = scope.getFunctionParent() || scope.getProgramParent();
  112. for (const name of Object.keys(path.getBindingIdentifiers())) {
  113. const binding = scope.getOwnBinding(name);
  114. if (binding) binding.kind = "var";
  115. scope.moveBindingTo(name, parentScope);
  116. }
  117. }
  118. }
  119. function isVar(node) {
  120. return _core.types.isVariableDeclaration(node, {
  121. kind: "var"
  122. }) && !isBlockScoped(node);
  123. }
  124. const letReferenceFunctionVisitor = _core.traverse.visitors.merge([{
  125. ReferencedIdentifier(path, state) {
  126. const ref = state.letReferences.get(path.node.name);
  127. if (!ref) return;
  128. const localBinding = path.scope.getBindingIdentifier(path.node.name);
  129. if (localBinding && localBinding !== ref) return;
  130. state.closurify = true;
  131. }
  132. }, _tdz.visitor]);
  133. const letReferenceBlockVisitor = _core.traverse.visitors.merge([{
  134. Loop: {
  135. enter(path, state) {
  136. state.loopDepth++;
  137. },
  138. exit(path, state) {
  139. state.loopDepth--;
  140. }
  141. },
  142. FunctionParent(path, state) {
  143. if (state.loopDepth > 0) {
  144. path.traverse(letReferenceFunctionVisitor, state);
  145. } else {
  146. path.traverse(_tdz.visitor, state);
  147. }
  148. return path.skip();
  149. }
  150. }, _tdz.visitor]);
  151. const hoistVarDeclarationsVisitor = {
  152. enter(path, self) {
  153. if (path.isForStatement()) {
  154. const {
  155. node
  156. } = path;
  157. if (isVar(node.init)) {
  158. const nodes = self.pushDeclar(node.init);
  159. if (nodes.length === 1) {
  160. node.init = nodes[0];
  161. } else {
  162. node.init = _core.types.sequenceExpression(nodes);
  163. }
  164. }
  165. } else if (path.isForInStatement() || path.isForOfStatement()) {
  166. const {
  167. node
  168. } = path;
  169. if (isVar(node.left)) {
  170. self.pushDeclar(node.left);
  171. node.left = node.left.declarations[0].id;
  172. }
  173. } else if (isVar(path.node)) {
  174. path.replaceWithMultiple(self.pushDeclar(path.node).map(expr => _core.types.expressionStatement(expr)));
  175. } else if (path.isFunction()) {
  176. return path.skip();
  177. }
  178. }
  179. };
  180. const loopLabelVisitor = {
  181. LabeledStatement({
  182. node
  183. }, state) {
  184. state.innerLabels.push(node.label.name);
  185. }
  186. };
  187. const continuationVisitor = {
  188. enter(path, state) {
  189. if (path.isAssignmentExpression() || path.isUpdateExpression()) {
  190. for (const name of Object.keys(path.getBindingIdentifiers())) {
  191. if (state.outsideReferences.get(name) !== path.scope.getBindingIdentifier(name)) {
  192. continue;
  193. }
  194. state.reassignments[name] = true;
  195. }
  196. } else if (path.isReturnStatement()) {
  197. state.returnStatements.push(path);
  198. }
  199. }
  200. };
  201. function loopNodeTo(node) {
  202. if (_core.types.isBreakStatement(node)) {
  203. return "break";
  204. } else if (_core.types.isContinueStatement(node)) {
  205. return "continue";
  206. }
  207. }
  208. const loopVisitor = {
  209. Loop(path, state) {
  210. const oldIgnoreLabeless = state.ignoreLabeless;
  211. state.ignoreLabeless = true;
  212. path.traverse(loopVisitor, state);
  213. state.ignoreLabeless = oldIgnoreLabeless;
  214. path.skip();
  215. },
  216. Function(path) {
  217. path.skip();
  218. },
  219. SwitchCase(path, state) {
  220. const oldInSwitchCase = state.inSwitchCase;
  221. state.inSwitchCase = true;
  222. path.traverse(loopVisitor, state);
  223. state.inSwitchCase = oldInSwitchCase;
  224. path.skip();
  225. },
  226. "BreakStatement|ContinueStatement|ReturnStatement"(path, state) {
  227. const {
  228. node,
  229. scope
  230. } = path;
  231. if (state.loopIgnored.has(node)) return;
  232. let replace;
  233. let loopText = loopNodeTo(node);
  234. if (loopText) {
  235. if (_core.types.isReturnStatement(node)) {
  236. throw new Error("Internal error: unexpected return statement with `loopText`");
  237. }
  238. if (node.label) {
  239. if (state.innerLabels.indexOf(node.label.name) >= 0) {
  240. return;
  241. }
  242. loopText = `${loopText}|${node.label.name}`;
  243. } else {
  244. if (state.ignoreLabeless) return;
  245. if (_core.types.isBreakStatement(node) && state.inSwitchCase) return;
  246. }
  247. state.hasBreakContinue = true;
  248. state.map.set(loopText, node);
  249. replace = _core.types.stringLiteral(loopText);
  250. }
  251. if (_core.types.isReturnStatement(node)) {
  252. state.hasReturn = true;
  253. replace = _core.types.objectExpression([_core.types.objectProperty(_core.types.identifier("v"), node.argument || scope.buildUndefinedNode())]);
  254. }
  255. if (replace) {
  256. replace = _core.types.returnStatement(replace);
  257. state.loopIgnored.add(replace);
  258. path.skip();
  259. path.replaceWith(_core.types.inherits(replace, node));
  260. }
  261. }
  262. };
  263. function isStrict(path) {
  264. return !!path.find(({
  265. node
  266. }) => {
  267. if (_core.types.isProgram(node)) {
  268. if (node.sourceType === "module") return true;
  269. } else if (!_core.types.isBlockStatement(node)) return false;
  270. return node.directives.some(directive => directive.value.value === "use strict");
  271. });
  272. }
  273. class BlockScoping {
  274. constructor(loopPath, blockPath, parent, scope, throwIfClosureRequired, tdzEnabled, state) {
  275. this.parent = void 0;
  276. this.state = void 0;
  277. this.scope = void 0;
  278. this.throwIfClosureRequired = void 0;
  279. this.tdzEnabled = void 0;
  280. this.blockPath = void 0;
  281. this.block = void 0;
  282. this.outsideLetReferences = void 0;
  283. this.hasLetReferences = void 0;
  284. this.letReferences = void 0;
  285. this.body = void 0;
  286. this.loopParent = void 0;
  287. this.loopLabel = void 0;
  288. this.loopPath = void 0;
  289. this.loop = void 0;
  290. this.has = void 0;
  291. this.parent = parent;
  292. this.scope = scope;
  293. this.state = state;
  294. this.throwIfClosureRequired = throwIfClosureRequired;
  295. this.tdzEnabled = tdzEnabled;
  296. this.blockPath = blockPath;
  297. this.block = blockPath.node;
  298. this.outsideLetReferences = new Map();
  299. this.hasLetReferences = false;
  300. this.letReferences = new Map();
  301. this.body = [];
  302. if (loopPath) {
  303. this.loopParent = loopPath.parent;
  304. this.loopLabel = _core.types.isLabeledStatement(this.loopParent) && this.loopParent.label;
  305. this.loopPath = loopPath;
  306. this.loop = loopPath.node;
  307. }
  308. }
  309. run() {
  310. const block = this.block;
  311. if (DONE.has(block)) return;
  312. DONE.add(block);
  313. const needsClosure = this.getLetReferences();
  314. this.checkConstants();
  315. if (_core.types.isFunction(this.parent) || _core.types.isProgram(this.block)) {
  316. this.updateScopeInfo();
  317. return;
  318. }
  319. if (!this.hasLetReferences) return;
  320. if (needsClosure) {
  321. this.wrapClosure();
  322. } else {
  323. this.remap();
  324. }
  325. this.updateScopeInfo(needsClosure);
  326. if (this.loopLabel && !_core.types.isLabeledStatement(this.loopParent)) {
  327. return _core.types.labeledStatement(this.loopLabel, this.loop);
  328. }
  329. }
  330. checkConstants() {
  331. const constBindings = new Map();
  332. for (const scope of new Set([this.scope, this.blockPath.scope])) {
  333. for (const name of Object.keys(scope.bindings)) {
  334. const binding = scope.bindings[name];
  335. if (binding.kind === "const") constBindings.set(name, binding);
  336. }
  337. }
  338. const {
  339. state
  340. } = this;
  341. for (const [name, binding] of constBindings) {
  342. for (const violation of binding.constantViolations) {
  343. const readOnlyError = state.addHelper("readOnlyError");
  344. const throwNode = _core.types.callExpression(readOnlyError, [_core.types.stringLiteral(name)]);
  345. if (violation.isAssignmentExpression()) {
  346. const {
  347. operator
  348. } = violation.node;
  349. if (operator === "=") {
  350. violation.replaceWith(_core.types.sequenceExpression([violation.get("right").node, throwNode]));
  351. } else if (["&&=", "||=", "??="].includes(operator)) {
  352. violation.replaceWith(_core.types.logicalExpression(
  353. operator.slice(0, -1), violation.get("left").node, _core.types.sequenceExpression([violation.get("right").node, throwNode])));
  354. } else {
  355. violation.replaceWith(_core.types.sequenceExpression([_core.types.binaryExpression(
  356. operator.slice(0, -1), violation.get("left").node, violation.get("right").node), throwNode]));
  357. }
  358. } else if (violation.isUpdateExpression()) {
  359. violation.replaceWith(_core.types.sequenceExpression([_core.types.unaryExpression("+", violation.get("argument").node), throwNode]));
  360. } else if (violation.isForXStatement()) {
  361. violation.ensureBlock();
  362. violation.get("left").replaceWith(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(violation.scope.generateUidIdentifier(name))]));
  363. violation.node.body.body.unshift(_core.types.expressionStatement(throwNode));
  364. }
  365. }
  366. }
  367. }
  368. updateScopeInfo(wrappedInClosure) {
  369. const blockScope = this.blockPath.scope;
  370. const parentScope = blockScope.getFunctionParent() || blockScope.getProgramParent();
  371. const letRefs = this.letReferences;
  372. for (const key of letRefs.keys()) {
  373. const ref = letRefs.get(key);
  374. const binding = blockScope.getBinding(ref.name);
  375. if (!binding) continue;
  376. if (binding.kind === "let" || binding.kind === "const") {
  377. binding.kind = "var";
  378. if (wrappedInClosure) {
  379. if (blockScope.hasOwnBinding(ref.name)) {
  380. blockScope.removeBinding(ref.name);
  381. }
  382. } else {
  383. blockScope.moveBindingTo(ref.name, parentScope);
  384. }
  385. }
  386. }
  387. }
  388. remap() {
  389. const letRefs = this.letReferences;
  390. const outsideLetRefs = this.outsideLetReferences;
  391. const scope = this.scope;
  392. const blockPathScope = this.blockPath.scope;
  393. for (const key of letRefs.keys()) {
  394. const ref = letRefs.get(key);
  395. if (scope.parentHasBinding(key) || scope.hasGlobal(key)) {
  396. const binding = scope.getOwnBinding(key);
  397. if (binding) {
  398. const parentBinding = scope.parent.getOwnBinding(key);
  399. if (binding.kind === "hoisted" &&
  400. !binding.path.node.async &&
  401. !binding.path.node.generator && (!parentBinding || isVar(parentBinding.path.parent)) && !isStrict(binding.path.parentPath)) {
  402. continue;
  403. }
  404. scope.rename(ref.name);
  405. }
  406. if (blockPathScope.hasOwnBinding(key)) {
  407. blockPathScope.rename(ref.name);
  408. }
  409. }
  410. }
  411. for (const key of outsideLetRefs.keys()) {
  412. const ref = letRefs.get(key);
  413. if (isInLoop(this.blockPath) && blockPathScope.hasOwnBinding(key)) {
  414. blockPathScope.rename(ref.name);
  415. }
  416. }
  417. }
  418. wrapClosure() {
  419. if (this.throwIfClosureRequired) {
  420. throw this.blockPath.buildCodeFrameError("Compiling let/const in this block would add a closure " + "(throwIfClosureRequired).");
  421. }
  422. const block = this.block;
  423. const outsideRefs = this.outsideLetReferences;
  424. if (this.loop) {
  425. for (const name of Array.from(outsideRefs.keys())) {
  426. const id = outsideRefs.get(name);
  427. if (this.scope.hasGlobal(id.name) || this.scope.parentHasBinding(id.name)) {
  428. outsideRefs.delete(id.name);
  429. this.letReferences.delete(id.name);
  430. this.scope.rename(id.name);
  431. this.letReferences.set(id.name, id);
  432. outsideRefs.set(id.name, id);
  433. }
  434. }
  435. }
  436. this.has = this.checkLoop();
  437. this.hoistVarDeclarations();
  438. const args = Array.from(outsideRefs.values(), node => _core.types.cloneNode(node));
  439. const params = args.map(id => _core.types.cloneNode(id));
  440. const isSwitch = block.type === "SwitchStatement";
  441. const fn = _core.types.functionExpression(null, params, _core.types.blockStatement(isSwitch ? [block] : block.body));
  442. this.addContinuations(fn);
  443. let call = _core.types.callExpression(_core.types.nullLiteral(), args);
  444. let basePath = ".callee";
  445. const hasYield = _core.traverse.hasType(fn.body, "YieldExpression", _core.types.FUNCTION_TYPES);
  446. if (hasYield) {
  447. fn.generator = true;
  448. call = _core.types.yieldExpression(call, true);
  449. basePath = ".argument" + basePath;
  450. }
  451. const hasAsync = _core.traverse.hasType(fn.body, "AwaitExpression", _core.types.FUNCTION_TYPES);
  452. if (hasAsync) {
  453. fn.async = true;
  454. call = _core.types.awaitExpression(call);
  455. basePath = ".argument" + basePath;
  456. }
  457. let placeholderPath;
  458. let index;
  459. if (this.has.hasReturn || this.has.hasBreakContinue) {
  460. const ret = this.scope.generateUid("ret");
  461. this.body.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(ret), call)]));
  462. placeholderPath = "declarations.0.init" + basePath;
  463. index = this.body.length - 1;
  464. this.buildHas(ret);
  465. } else {
  466. this.body.push(_core.types.expressionStatement(call));
  467. placeholderPath = "expression" + basePath;
  468. index = this.body.length - 1;
  469. }
  470. let callPath;
  471. if (isSwitch) {
  472. const {
  473. parentPath,
  474. listKey,
  475. key
  476. } = this.blockPath;
  477. this.blockPath.replaceWithMultiple(this.body);
  478. callPath = parentPath.get(listKey)[key + index];
  479. } else {
  480. block.body = this.body;
  481. callPath = this.blockPath.get("body")[index];
  482. }
  483. const placeholder = callPath.get(placeholderPath);
  484. let fnPath;
  485. if (this.loop) {
  486. const loopId = this.scope.generateUid("loop");
  487. const p = this.loopPath.insertBefore(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(loopId), fn)]));
  488. placeholder.replaceWith(_core.types.identifier(loopId));
  489. fnPath = p[0].get("declarations.0.init");
  490. } else {
  491. placeholder.replaceWith(fn);
  492. fnPath = placeholder;
  493. }
  494. fnPath.unwrapFunctionEnvironment();
  495. }
  496. addContinuations(fn) {
  497. const state = {
  498. reassignments: {},
  499. returnStatements: [],
  500. outsideReferences: this.outsideLetReferences
  501. };
  502. this.scope.traverse(fn, continuationVisitor, state);
  503. for (let i = 0; i < fn.params.length; i++) {
  504. const param = fn.params[i];
  505. if (!state.reassignments[param.name]) continue;
  506. const paramName = param.name;
  507. const newParamName = this.scope.generateUid(param.name);
  508. fn.params[i] = _core.types.identifier(newParamName);
  509. this.scope.rename(paramName, newParamName, fn);
  510. state.returnStatements.forEach(returnStatement => {
  511. returnStatement.insertBefore(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(paramName), _core.types.identifier(newParamName))));
  512. });
  513. fn.body.body.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(paramName), _core.types.identifier(newParamName))));
  514. }
  515. }
  516. getLetReferences() {
  517. const block = this.block;
  518. const declarators = [];
  519. if (this.loop) {
  520. const init = this.loop.left || this.loop.init;
  521. if (isBlockScoped(init)) {
  522. declarators.push(init);
  523. const names = _core.types.getBindingIdentifiers(init);
  524. for (const name of Object.keys(names)) {
  525. this.outsideLetReferences.set(name, names[name]);
  526. }
  527. }
  528. }
  529. const addDeclarationsFromChild = (path, node) => {
  530. if (_core.types.isClassDeclaration(node) || _core.types.isFunctionDeclaration(node) || isBlockScoped(node)) {
  531. if (isBlockScoped(node)) {
  532. convertBlockScopedToVar(path, node, block, this.scope);
  533. }
  534. if (node.type === "VariableDeclaration") {
  535. for (let i = 0; i < node.declarations.length; i++) {
  536. declarators.push(node.declarations[i]);
  537. }
  538. } else {
  539. declarators.push(node);
  540. }
  541. }
  542. if (_core.types.isLabeledStatement(node)) {
  543. addDeclarationsFromChild(path.get("body"), node.body);
  544. }
  545. };
  546. if (block.type === "SwitchStatement") {
  547. const declarPaths = this.blockPath.get("cases");
  548. for (let i = 0; i < block.cases.length; i++) {
  549. const consequents = block.cases[i].consequent;
  550. for (let j = 0; j < consequents.length; j++) {
  551. const declar = consequents[j];
  552. addDeclarationsFromChild(declarPaths[i], declar);
  553. }
  554. }
  555. } else {
  556. const declarPaths = this.blockPath.get("body");
  557. for (let i = 0; i < block.body.length; i++) {
  558. addDeclarationsFromChild(declarPaths[i], declarPaths[i].node);
  559. }
  560. }
  561. for (let i = 0; i < declarators.length; i++) {
  562. const declar = declarators[i];
  563. const keys = _core.types.getBindingIdentifiers(declar, false, true);
  564. for (const key of Object.keys(keys)) {
  565. this.letReferences.set(key, keys[key]);
  566. }
  567. this.hasLetReferences = true;
  568. }
  569. if (!this.hasLetReferences) return;
  570. const state = {
  571. letReferences: this.letReferences,
  572. closurify: false,
  573. loopDepth: 0,
  574. tdzEnabled: this.tdzEnabled,
  575. addHelper: name => this.state.addHelper(name)
  576. };
  577. if (isInLoop(this.blockPath)) {
  578. state.loopDepth++;
  579. }
  580. this.blockPath.traverse(letReferenceBlockVisitor, state);
  581. return state.closurify;
  582. }
  583. checkLoop() {
  584. const state = {
  585. hasBreakContinue: false,
  586. ignoreLabeless: false,
  587. inSwitchCase: false,
  588. innerLabels: [],
  589. hasReturn: false,
  590. isLoop: !!this.loop,
  591. map: new Map(),
  592. loopIgnored: new WeakSet()
  593. };
  594. this.blockPath.traverse(loopLabelVisitor, state);
  595. this.blockPath.traverse(loopVisitor, state);
  596. return state;
  597. }
  598. hoistVarDeclarations() {
  599. this.blockPath.traverse(hoistVarDeclarationsVisitor, this);
  600. }
  601. pushDeclar(node) {
  602. const declars = [];
  603. const names = _core.types.getBindingIdentifiers(node);
  604. for (const name of Object.keys(names)) {
  605. declars.push(_core.types.variableDeclarator(names[name]));
  606. }
  607. this.body.push(_core.types.variableDeclaration(node.kind, declars));
  608. const replace = [];
  609. for (let i = 0; i < node.declarations.length; i++) {
  610. const declar = node.declarations[i];
  611. if (!declar.init) continue;
  612. const expr = _core.types.assignmentExpression("=", _core.types.cloneNode(declar.id), _core.types.cloneNode(declar.init));
  613. replace.push(_core.types.inherits(expr, declar));
  614. }
  615. return replace;
  616. }
  617. buildHas(ret) {
  618. const body = this.body;
  619. const has = this.has;
  620. if (has.hasBreakContinue) {
  621. for (const key of has.map.keys()) {
  622. body.push(_core.types.ifStatement(_core.types.binaryExpression("===", _core.types.identifier(ret), _core.types.stringLiteral(key)), has.map.get(key)));
  623. }
  624. }
  625. if (has.hasReturn) {
  626. body.push(buildRetCheck({
  627. RETURN: _core.types.identifier(ret)
  628. }));
  629. }
  630. }
  631. }
  632. //# sourceMappingURL=index.js.map