index.js 7.5 KB

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