index.js 22 KB

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