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