index.js 7.8 KB

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