index.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. const {
  149. node,
  150. parent
  151. } = path;
  152. if (path.isForStatement()) {
  153. if (isVar(node.init, node)) {
  154. const nodes = self.pushDeclar(node.init);
  155. if (nodes.length === 1) {
  156. node.init = nodes[0];
  157. } else {
  158. node.init = _core.types.sequenceExpression(nodes);
  159. }
  160. }
  161. } else if (path.isFor()) {
  162. if (isVar(node.left, node)) {
  163. self.pushDeclar(node.left);
  164. node.left = node.left.declarations[0].id;
  165. }
  166. } else if (isVar(node, parent)) {
  167. path.replaceWithMultiple(self.pushDeclar(node).map(expr => _core.types.expressionStatement(expr)));
  168. } else if (path.isFunction()) {
  169. return path.skip();
  170. }
  171. }
  172. };
  173. const loopLabelVisitor = {
  174. LabeledStatement({
  175. node
  176. }, state) {
  177. state.innerLabels.push(node.label.name);
  178. }
  179. };
  180. const continuationVisitor = {
  181. enter(path, state) {
  182. if (path.isAssignmentExpression() || path.isUpdateExpression()) {
  183. for (const name of Object.keys(path.getBindingIdentifiers())) {
  184. if (state.outsideReferences.get(name) !== path.scope.getBindingIdentifier(name)) {
  185. continue;
  186. }
  187. state.reassignments[name] = true;
  188. }
  189. } else if (path.isReturnStatement()) {
  190. state.returnStatements.push(path);
  191. }
  192. }
  193. };
  194. function loopNodeTo(node) {
  195. if (_core.types.isBreakStatement(node)) {
  196. return "break";
  197. } else if (_core.types.isContinueStatement(node)) {
  198. return "continue";
  199. }
  200. }
  201. const loopVisitor = {
  202. Loop(path, state) {
  203. const oldIgnoreLabeless = state.ignoreLabeless;
  204. state.ignoreLabeless = true;
  205. path.traverse(loopVisitor, state);
  206. state.ignoreLabeless = oldIgnoreLabeless;
  207. path.skip();
  208. },
  209. Function(path) {
  210. path.skip();
  211. },
  212. SwitchCase(path, state) {
  213. const oldInSwitchCase = state.inSwitchCase;
  214. state.inSwitchCase = true;
  215. path.traverse(loopVisitor, state);
  216. state.inSwitchCase = oldInSwitchCase;
  217. path.skip();
  218. },
  219. "BreakStatement|ContinueStatement|ReturnStatement"(path, state) {
  220. const {
  221. node,
  222. scope
  223. } = path;
  224. if (node[this.LOOP_IGNORE]) return;
  225. let replace;
  226. let loopText = loopNodeTo(node);
  227. if (loopText) {
  228. if (node.label) {
  229. if (state.innerLabels.indexOf(node.label.name) >= 0) {
  230. return;
  231. }
  232. loopText = `${loopText}|${node.label.name}`;
  233. } else {
  234. if (state.ignoreLabeless) return;
  235. if (_core.types.isBreakStatement(node) && state.inSwitchCase) return;
  236. }
  237. state.hasBreakContinue = true;
  238. state.map[loopText] = node;
  239. replace = _core.types.stringLiteral(loopText);
  240. }
  241. if (path.isReturnStatement()) {
  242. state.hasReturn = true;
  243. replace = _core.types.objectExpression([_core.types.objectProperty(_core.types.identifier("v"), node.argument || scope.buildUndefinedNode())]);
  244. }
  245. if (replace) {
  246. replace = _core.types.returnStatement(replace);
  247. replace[this.LOOP_IGNORE] = true;
  248. path.skip();
  249. path.replaceWith(_core.types.inherits(replace, node));
  250. }
  251. }
  252. };
  253. function isStrict(path) {
  254. return !!path.find(({
  255. node
  256. }) => {
  257. if (_core.types.isProgram(node)) {
  258. if (node.sourceType === "module") return true;
  259. } else if (!_core.types.isBlockStatement(node)) return false;
  260. return node.directives.some(directive => directive.value.value === "use strict");
  261. });
  262. }
  263. class BlockScoping {
  264. constructor(loopPath, blockPath, parent, scope, throwIfClosureRequired, tdzEnabled, state) {
  265. this.parent = parent;
  266. this.scope = scope;
  267. this.state = state;
  268. this.throwIfClosureRequired = throwIfClosureRequired;
  269. this.tdzEnabled = tdzEnabled;
  270. this.blockPath = blockPath;
  271. this.block = blockPath.node;
  272. this.outsideLetReferences = new Map();
  273. this.hasLetReferences = false;
  274. this.letReferences = new Map();
  275. this.body = [];
  276. if (loopPath) {
  277. this.loopParent = loopPath.parent;
  278. this.loopLabel = _core.types.isLabeledStatement(this.loopParent) && this.loopParent.label;
  279. this.loopPath = loopPath;
  280. this.loop = loopPath.node;
  281. }
  282. }
  283. run() {
  284. const block = this.block;
  285. if (DONE.has(block)) return;
  286. DONE.add(block);
  287. const needsClosure = this.getLetReferences();
  288. this.checkConstants();
  289. if (_core.types.isFunction(this.parent) || _core.types.isProgram(this.block)) {
  290. this.updateScopeInfo();
  291. return;
  292. }
  293. if (!this.hasLetReferences) return;
  294. if (needsClosure) {
  295. this.wrapClosure();
  296. } else {
  297. this.remap();
  298. }
  299. this.updateScopeInfo(needsClosure);
  300. if (this.loopLabel && !_core.types.isLabeledStatement(this.loopParent)) {
  301. return _core.types.labeledStatement(this.loopLabel, this.loop);
  302. }
  303. }
  304. checkConstants() {
  305. const scope = this.scope;
  306. const state = this.state;
  307. for (const name of Object.keys(scope.bindings)) {
  308. const binding = scope.bindings[name];
  309. if (binding.kind !== "const") continue;
  310. for (const violation of binding.constantViolations) {
  311. const readOnlyError = state.addHelper("readOnlyError");
  312. const throwNode = _core.types.callExpression(readOnlyError, [_core.types.stringLiteral(name)]);
  313. if (violation.isAssignmentExpression()) {
  314. const {
  315. operator
  316. } = violation.node;
  317. if (operator === "=") {
  318. violation.replaceWith(_core.types.sequenceExpression([violation.get("right").node, throwNode]));
  319. } else if (["&&=", "||=", "??="].includes(operator)) {
  320. violation.replaceWith(_core.types.logicalExpression(operator.slice(0, -1), violation.get("left").node, _core.types.sequenceExpression([violation.get("right").node, throwNode])));
  321. } else {
  322. violation.replaceWith(_core.types.sequenceExpression([_core.types.binaryExpression(operator.slice(0, -1), violation.get("left").node, violation.get("right").node), throwNode]));
  323. }
  324. } else if (violation.isUpdateExpression()) {
  325. violation.replaceWith(_core.types.sequenceExpression([_core.types.unaryExpression("+", violation.get("argument").node), throwNode]));
  326. } else if (violation.isForXStatement()) {
  327. violation.ensureBlock();
  328. violation.get("left").replaceWith(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(violation.scope.generateUidIdentifier(name))]));
  329. violation.node.body.body.unshift(_core.types.expressionStatement(throwNode));
  330. }
  331. }
  332. }
  333. }
  334. updateScopeInfo(wrappedInClosure) {
  335. const blockScope = this.blockPath.scope;
  336. const parentScope = blockScope.getFunctionParent() || blockScope.getProgramParent();
  337. const letRefs = this.letReferences;
  338. for (const key of letRefs.keys()) {
  339. const ref = letRefs.get(key);
  340. const binding = blockScope.getBinding(ref.name);
  341. if (!binding) continue;
  342. if (binding.kind === "let" || binding.kind === "const") {
  343. binding.kind = "var";
  344. if (wrappedInClosure) {
  345. if (blockScope.hasOwnBinding(ref.name)) {
  346. blockScope.removeBinding(ref.name);
  347. }
  348. } else {
  349. blockScope.moveBindingTo(ref.name, parentScope);
  350. }
  351. }
  352. }
  353. }
  354. remap() {
  355. const letRefs = this.letReferences;
  356. const outsideLetRefs = this.outsideLetReferences;
  357. const scope = this.scope;
  358. const blockPathScope = this.blockPath.scope;
  359. for (const key of letRefs.keys()) {
  360. const ref = letRefs.get(key);
  361. if (scope.parentHasBinding(key) || scope.hasGlobal(key)) {
  362. const binding = scope.getOwnBinding(key);
  363. if (binding) {
  364. const parentBinding = scope.parent.getOwnBinding(key);
  365. if (binding.kind === "hoisted" && !binding.path.node.async && !binding.path.node.generator && (!parentBinding || isVar(parentBinding.path.parent)) && !isStrict(binding.path.parentPath)) {
  366. continue;
  367. }
  368. scope.rename(ref.name);
  369. }
  370. if (blockPathScope.hasOwnBinding(key)) {
  371. blockPathScope.rename(ref.name);
  372. }
  373. }
  374. }
  375. for (const key of outsideLetRefs.keys()) {
  376. const ref = letRefs.get(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 Array.from(outsideRefs.keys())) {
  390. const id = outsideRefs.get(name);
  391. if (this.scope.hasGlobal(id.name) || this.scope.parentHasBinding(id.name)) {
  392. outsideRefs.delete(id.name);
  393. this.letReferences.delete(id.name);
  394. this.scope.rename(id.name);
  395. this.letReferences.set(id.name, id);
  396. outsideRefs.set(id.name, id);
  397. }
  398. }
  399. }
  400. this.has = this.checkLoop();
  401. this.hoistVarDeclarations();
  402. const args = Array.from(outsideRefs.values(), node => _core.types.cloneNode(node));
  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. const declarators = [];
  483. if (this.loop) {
  484. const init = this.loop.left || this.loop.init;
  485. if (isBlockScoped(init)) {
  486. declarators.push(init);
  487. const names = _core.types.getBindingIdentifiers(init);
  488. for (const name of Object.keys(names)) {
  489. this.outsideLetReferences.set(name, names[name]);
  490. }
  491. }
  492. }
  493. const addDeclarationsFromChild = (path, node) => {
  494. node = node || path.node;
  495. if (_core.types.isClassDeclaration(node) || _core.types.isFunctionDeclaration(node) || isBlockScoped(node)) {
  496. if (isBlockScoped(node)) {
  497. convertBlockScopedToVar(path, node, block, this.scope);
  498. }
  499. if (node.declarations) {
  500. for (let i = 0; i < node.declarations.length; i++) {
  501. declarators.push(node.declarations[i]);
  502. }
  503. } else {
  504. declarators.push(node);
  505. }
  506. }
  507. if (_core.types.isLabeledStatement(node)) {
  508. addDeclarationsFromChild(path.get("body"), node.body);
  509. }
  510. };
  511. if (block.body) {
  512. const declarPaths = this.blockPath.get("body");
  513. for (let i = 0; i < block.body.length; i++) {
  514. addDeclarationsFromChild(declarPaths[i]);
  515. }
  516. }
  517. if (block.cases) {
  518. const declarPaths = this.blockPath.get("cases");
  519. for (let i = 0; i < block.cases.length; i++) {
  520. const consequents = block.cases[i].consequent;
  521. for (let j = 0; j < consequents.length; j++) {
  522. const declar = consequents[j];
  523. addDeclarationsFromChild(declarPaths[i], declar);
  524. }
  525. }
  526. }
  527. for (let i = 0; i < declarators.length; i++) {
  528. const declar = declarators[i];
  529. const keys = _core.types.getBindingIdentifiers(declar, false, true);
  530. for (const key of Object.keys(keys)) {
  531. this.letReferences.set(key, keys[key]);
  532. }
  533. this.hasLetReferences = true;
  534. }
  535. if (!this.hasLetReferences) return;
  536. const state = {
  537. letReferences: this.letReferences,
  538. closurify: false,
  539. loopDepth: 0,
  540. tdzEnabled: this.tdzEnabled,
  541. addHelper: name => this.state.addHelper(name)
  542. };
  543. if (isInLoop(this.blockPath)) {
  544. state.loopDepth++;
  545. }
  546. this.blockPath.traverse(letReferenceBlockVisitor, state);
  547. return state.closurify;
  548. }
  549. checkLoop() {
  550. const state = {
  551. hasBreakContinue: false,
  552. ignoreLabeless: false,
  553. inSwitchCase: false,
  554. innerLabels: [],
  555. hasReturn: false,
  556. isLoop: !!this.loop,
  557. map: {},
  558. LOOP_IGNORE: Symbol()
  559. };
  560. this.blockPath.traverse(loopLabelVisitor, state);
  561. this.blockPath.traverse(loopVisitor, state);
  562. return state;
  563. }
  564. hoistVarDeclarations() {
  565. this.blockPath.traverse(hoistVarDeclarationsVisitor, this);
  566. }
  567. pushDeclar(node) {
  568. const declars = [];
  569. const names = _core.types.getBindingIdentifiers(node);
  570. for (const name of Object.keys(names)) {
  571. declars.push(_core.types.variableDeclarator(names[name]));
  572. }
  573. this.body.push(_core.types.variableDeclaration(node.kind, declars));
  574. const replace = [];
  575. for (let i = 0; i < node.declarations.length; i++) {
  576. const declar = node.declarations[i];
  577. if (!declar.init) continue;
  578. const expr = _core.types.assignmentExpression("=", _core.types.cloneNode(declar.id), _core.types.cloneNode(declar.init));
  579. replace.push(_core.types.inherits(expr, declar));
  580. }
  581. return replace;
  582. }
  583. buildHas(ret) {
  584. const body = this.body;
  585. const has = this.has;
  586. if (has.hasBreakContinue) {
  587. for (const key of Object.keys(has.map)) {
  588. body.push(_core.types.ifStatement(_core.types.binaryExpression("===", _core.types.identifier(ret), _core.types.stringLiteral(key)), has.map[key]));
  589. }
  590. }
  591. if (has.hasReturn) {
  592. body.push(buildRetCheck({
  593. RETURN: _core.types.identifier(ret)
  594. }));
  595. }
  596. }
  597. }