index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 _core = require("@babel/core");
  8. var _noHelperImplementation = require("./no-helper-implementation");
  9. var _default = (0, _helperPluginUtils.declare)((api, options) => {
  10. var _options$assumeArray, _options$allowArrayLi, _api$assumption;
  11. api.assertVersion(7);
  12. {
  13. const {
  14. assumeArray,
  15. allowArrayLike,
  16. loose
  17. } = options;
  18. if (loose === true && assumeArray === true) {
  19. throw new Error(`The loose and assumeArray options cannot be used together in @babel/plugin-transform-for-of`);
  20. }
  21. if (assumeArray === true && allowArrayLike === true) {
  22. throw new Error(`The assumeArray and allowArrayLike options cannot be used together in @babel/plugin-transform-for-of`);
  23. }
  24. if (allowArrayLike && /^7\.\d\./.test(api.version)) {
  25. throw new Error(`The allowArrayLike is only supported when using @babel/core@^7.10.0`);
  26. }
  27. }
  28. const iterableIsArray = (_options$assumeArray = options.assumeArray) != null ? _options$assumeArray : !options.loose && api.assumption("iterableIsArray");
  29. const arrayLikeIsIterable = (_options$allowArrayLi = options.allowArrayLike) != null ? _options$allowArrayLi : api.assumption("arrayLikeIsIterable");
  30. const skipteratorClosing = (_api$assumption = api.assumption("skipForOfIteratorClosing")) != null ? _api$assumption : options.loose;
  31. if (iterableIsArray && arrayLikeIsIterable) {
  32. throw new Error(`The "iterableIsArray" and "arrayLikeIsIterable" assumptions are not compatible.`);
  33. }
  34. if (iterableIsArray) {
  35. return {
  36. name: "transform-for-of",
  37. visitor: {
  38. ForOfStatement(path) {
  39. const {
  40. scope
  41. } = path;
  42. const {
  43. left,
  44. right,
  45. await: isAwait
  46. } = path.node;
  47. if (isAwait) {
  48. return;
  49. }
  50. const i = scope.generateUidIdentifier("i");
  51. let array = scope.maybeGenerateMemoised(right, true);
  52. const inits = [_core.types.variableDeclarator(i, _core.types.numericLiteral(0))];
  53. if (array) {
  54. inits.push(_core.types.variableDeclarator(array, right));
  55. } else {
  56. array = right;
  57. }
  58. const item = _core.types.memberExpression(_core.types.cloneNode(array), _core.types.cloneNode(i), true);
  59. let assignment;
  60. if (_core.types.isVariableDeclaration(left)) {
  61. assignment = left;
  62. assignment.declarations[0].init = item;
  63. } else {
  64. assignment = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, item));
  65. }
  66. let blockBody;
  67. const body = path.get("body");
  68. if (body.isBlockStatement() && Object.keys(path.getBindingIdentifiers()).some(id => body.scope.hasOwnBinding(id))) {
  69. blockBody = _core.types.blockStatement([assignment, body.node]);
  70. } else {
  71. blockBody = _core.types.toBlock(body.node);
  72. blockBody.body.unshift(assignment);
  73. }
  74. path.replaceWith(_core.types.forStatement(_core.types.variableDeclaration("let", inits), _core.types.binaryExpression("<", _core.types.cloneNode(i), _core.types.memberExpression(_core.types.cloneNode(array), _core.types.identifier("length"))), _core.types.updateExpression("++", _core.types.cloneNode(i)), blockBody));
  75. }
  76. }
  77. };
  78. }
  79. const buildForOfArray = (0, _core.template)`
  80. for (var KEY = 0, NAME = ARR; KEY < NAME.length; KEY++) BODY;
  81. `;
  82. const buildForOfNoIteratorClosing = _core.template.statements`
  83. for (var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT, ARRAY_LIKE_IS_ITERABLE), STEP_KEY;
  84. !(STEP_KEY = ITERATOR_HELPER()).done;) BODY;
  85. `;
  86. const buildForOf = _core.template.statements`
  87. var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT, ARRAY_LIKE_IS_ITERABLE), STEP_KEY;
  88. try {
  89. for (ITERATOR_HELPER.s(); !(STEP_KEY = ITERATOR_HELPER.n()).done;) BODY;
  90. } catch (err) {
  91. ITERATOR_HELPER.e(err);
  92. } finally {
  93. ITERATOR_HELPER.f();
  94. }
  95. `;
  96. const builder = skipteratorClosing ? {
  97. build: buildForOfNoIteratorClosing,
  98. helper: "createForOfIteratorHelperLoose",
  99. getContainer: nodes => nodes
  100. } : {
  101. build: buildForOf,
  102. helper: "createForOfIteratorHelper",
  103. getContainer: nodes => nodes[1].block.body
  104. };
  105. function _ForOfStatementArray(path) {
  106. const {
  107. node,
  108. scope
  109. } = path;
  110. const right = scope.generateUidIdentifierBasedOnNode(node.right, "arr");
  111. const iterationKey = scope.generateUidIdentifier("i");
  112. const loop = buildForOfArray({
  113. BODY: node.body,
  114. KEY: iterationKey,
  115. NAME: right,
  116. ARR: node.right
  117. });
  118. _core.types.inherits(loop, node);
  119. _core.types.ensureBlock(loop);
  120. const iterationValue = _core.types.memberExpression(_core.types.cloneNode(right), _core.types.cloneNode(iterationKey), true);
  121. const left = node.left;
  122. if (_core.types.isVariableDeclaration(left)) {
  123. left.declarations[0].init = iterationValue;
  124. loop.body.body.unshift(left);
  125. } else {
  126. loop.body.body.unshift(_core.types.expressionStatement(_core.types.assignmentExpression("=", left, iterationValue)));
  127. }
  128. return loop;
  129. }
  130. return {
  131. name: "transform-for-of",
  132. visitor: {
  133. ForOfStatement(path, state) {
  134. const right = path.get("right");
  135. if (right.isArrayExpression() || right.isGenericType("Array") || _core.types.isArrayTypeAnnotation(right.getTypeAnnotation())) {
  136. path.replaceWith(_ForOfStatementArray(path));
  137. return;
  138. }
  139. if (!state.availableHelper(builder.helper)) {
  140. (0, _noHelperImplementation.default)(skipteratorClosing, path, state);
  141. return;
  142. }
  143. const {
  144. node,
  145. parent,
  146. scope
  147. } = path;
  148. const left = node.left;
  149. let declar;
  150. const stepKey = scope.generateUid("step");
  151. const stepValue = _core.types.memberExpression(_core.types.identifier(stepKey), _core.types.identifier("value"));
  152. if (_core.types.isVariableDeclaration(left)) {
  153. declar = _core.types.variableDeclaration(left.kind, [_core.types.variableDeclarator(left.declarations[0].id, stepValue)]);
  154. } else {
  155. declar = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, stepValue));
  156. }
  157. path.ensureBlock();
  158. node.body.body.unshift(declar);
  159. const nodes = builder.build({
  160. CREATE_ITERATOR_HELPER: state.addHelper(builder.helper),
  161. ITERATOR_HELPER: scope.generateUidIdentifier("iterator"),
  162. ARRAY_LIKE_IS_ITERABLE: arrayLikeIsIterable ? _core.types.booleanLiteral(true) : null,
  163. STEP_KEY: _core.types.identifier(stepKey),
  164. OBJECT: node.right,
  165. BODY: node.body
  166. });
  167. const container = builder.getContainer(nodes);
  168. _core.types.inherits(container[0], node);
  169. _core.types.inherits(container[0].body, node.body);
  170. if (_core.types.isLabeledStatement(parent)) {
  171. container[0] = _core.types.labeledStatement(parent.label, container[0]);
  172. path.parentPath.replaceWithMultiple(nodes);
  173. path.skip();
  174. } else {
  175. path.replaceWithMultiple(nodes);
  176. }
  177. }
  178. }
  179. };
  180. });
  181. exports.default = _default;