index.js 7.4 KB

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