index.js 21 KB

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