You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
8.7 KiB

4 years ago
  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 _default = (0, _helperPluginUtils.declare)((api, options) => {
  9. api.assertVersion(7);
  10. const {
  11. loose,
  12. assumeArray
  13. } = options;
  14. if (loose === true && assumeArray === true) {
  15. throw new Error(`The loose and assumeArray options cannot be used together in @babel/plugin-transform-for-of`);
  16. }
  17. if (assumeArray) {
  18. return {
  19. name: "transform-for-of",
  20. visitor: {
  21. ForOfStatement(path) {
  22. const {
  23. scope
  24. } = path;
  25. const {
  26. left,
  27. right,
  28. body
  29. } = path.node;
  30. const i = scope.generateUidIdentifier("i");
  31. let array = scope.maybeGenerateMemoised(right, true);
  32. const inits = [_core.types.variableDeclarator(i, _core.types.numericLiteral(0))];
  33. if (array) {
  34. inits.push(_core.types.variableDeclarator(array, right));
  35. } else {
  36. array = right;
  37. }
  38. const item = _core.types.memberExpression(_core.types.cloneNode(array), _core.types.cloneNode(i), true);
  39. let assignment;
  40. if (_core.types.isVariableDeclaration(left)) {
  41. assignment = left;
  42. assignment.declarations[0].init = item;
  43. } else {
  44. assignment = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, item));
  45. }
  46. const block = _core.types.toBlock(body);
  47. block.body.unshift(assignment);
  48. 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)), block));
  49. }
  50. }
  51. };
  52. }
  53. const pushComputedProps = loose ? pushComputedPropsLoose : pushComputedPropsSpec;
  54. const buildForOfArray = (0, _core.template)(`
  55. for (var KEY = 0, NAME = ARR; KEY < NAME.length; KEY++) BODY;
  56. `);
  57. const buildForOfLoose = (0, _core.template)(`
  58. for (var LOOP_OBJECT = OBJECT,
  59. IS_ARRAY = Array.isArray(LOOP_OBJECT),
  60. INDEX = 0,
  61. LOOP_OBJECT = IS_ARRAY ? LOOP_OBJECT : LOOP_OBJECT[Symbol.iterator]();;) {
  62. INTERMEDIATE;
  63. if (IS_ARRAY) {
  64. if (INDEX >= LOOP_OBJECT.length) break;
  65. ID = LOOP_OBJECT[INDEX++];
  66. } else {
  67. INDEX = LOOP_OBJECT.next();
  68. if (INDEX.done) break;
  69. ID = INDEX.value;
  70. }
  71. }
  72. `);
  73. const buildForOf = (0, _core.template)(`
  74. var ITERATOR_COMPLETION = true;
  75. var ITERATOR_HAD_ERROR_KEY = false;
  76. var ITERATOR_ERROR_KEY = undefined;
  77. try {
  78. for (
  79. var ITERATOR_KEY = OBJECT[Symbol.iterator](), STEP_KEY;
  80. !(ITERATOR_COMPLETION = (STEP_KEY = ITERATOR_KEY.next()).done);
  81. ITERATOR_COMPLETION = true
  82. ) {}
  83. } catch (err) {
  84. ITERATOR_HAD_ERROR_KEY = true;
  85. ITERATOR_ERROR_KEY = err;
  86. } finally {
  87. try {
  88. if (!ITERATOR_COMPLETION && ITERATOR_KEY.return != null) {
  89. ITERATOR_KEY.return();
  90. }
  91. } finally {
  92. if (ITERATOR_HAD_ERROR_KEY) {
  93. throw ITERATOR_ERROR_KEY;
  94. }
  95. }
  96. }
  97. `);
  98. function _ForOfStatementArray(path) {
  99. const {
  100. node,
  101. scope
  102. } = path;
  103. const right = scope.generateUidIdentifierBasedOnNode(node.right, "arr");
  104. const iterationKey = scope.generateUidIdentifier("i");
  105. let loop = buildForOfArray({
  106. BODY: node.body,
  107. KEY: iterationKey,
  108. NAME: right,
  109. ARR: node.right
  110. });
  111. _core.types.inherits(loop, node);
  112. _core.types.ensureBlock(loop);
  113. const iterationValue = _core.types.memberExpression(_core.types.cloneNode(right), _core.types.cloneNode(iterationKey), true);
  114. const left = node.left;
  115. if (_core.types.isVariableDeclaration(left)) {
  116. left.declarations[0].init = iterationValue;
  117. loop.body.body.unshift(left);
  118. } else {
  119. loop.body.body.unshift(_core.types.expressionStatement(_core.types.assignmentExpression("=", left, iterationValue)));
  120. }
  121. if (path.parentPath.isLabeledStatement()) {
  122. loop = _core.types.labeledStatement(path.parentPath.node.label, loop);
  123. }
  124. return [loop];
  125. }
  126. function replaceWithArray(path) {
  127. if (path.parentPath.isLabeledStatement()) {
  128. path.parentPath.replaceWithMultiple(_ForOfStatementArray(path));
  129. } else {
  130. path.replaceWithMultiple(_ForOfStatementArray(path));
  131. }
  132. }
  133. return {
  134. name: "transform-for-of",
  135. visitor: {
  136. ForOfStatement(path, state) {
  137. const right = path.get("right");
  138. if (right.isArrayExpression() || right.isGenericType("Array") || _core.types.isArrayTypeAnnotation(right.getTypeAnnotation())) {
  139. replaceWithArray(path);
  140. return;
  141. }
  142. const {
  143. node
  144. } = path;
  145. const build = pushComputedProps(path, state);
  146. const declar = build.declar;
  147. const loop = build.loop;
  148. const block = loop.body;
  149. path.ensureBlock();
  150. if (declar) {
  151. block.body.push(declar);
  152. }
  153. block.body = block.body.concat(node.body.body);
  154. _core.types.inherits(loop, node);
  155. _core.types.inherits(loop.body, node.body);
  156. if (build.replaceParent) {
  157. path.parentPath.replaceWithMultiple(build.node);
  158. path.remove();
  159. } else {
  160. path.replaceWithMultiple(build.node);
  161. }
  162. }
  163. }
  164. };
  165. function pushComputedPropsLoose(path, file) {
  166. const {
  167. node,
  168. scope,
  169. parent
  170. } = path;
  171. const {
  172. left
  173. } = node;
  174. let declar, id, intermediate;
  175. if (_core.types.isIdentifier(left) || _core.types.isPattern(left) || _core.types.isMemberExpression(left)) {
  176. id = left;
  177. intermediate = null;
  178. } else if (_core.types.isVariableDeclaration(left)) {
  179. id = scope.generateUidIdentifier("ref");
  180. declar = _core.types.variableDeclaration(left.kind, [_core.types.variableDeclarator(left.declarations[0].id, _core.types.identifier(id.name))]);
  181. intermediate = _core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(id.name))]);
  182. } else {
  183. throw file.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`);
  184. }
  185. const iteratorKey = scope.generateUidIdentifier("iterator");
  186. const isArrayKey = scope.generateUidIdentifier("isArray");
  187. const loop = buildForOfLoose({
  188. LOOP_OBJECT: iteratorKey,
  189. IS_ARRAY: isArrayKey,
  190. OBJECT: node.right,
  191. INDEX: scope.generateUidIdentifier("i"),
  192. ID: id,
  193. INTERMEDIATE: intermediate
  194. });
  195. const isLabeledParent = _core.types.isLabeledStatement(parent);
  196. let labeled;
  197. if (isLabeledParent) {
  198. labeled = _core.types.labeledStatement(parent.label, loop);
  199. }
  200. return {
  201. replaceParent: isLabeledParent,
  202. declar: declar,
  203. node: labeled || loop,
  204. loop: loop
  205. };
  206. }
  207. function pushComputedPropsSpec(path, file) {
  208. const {
  209. node,
  210. scope,
  211. parent
  212. } = path;
  213. const left = node.left;
  214. let declar;
  215. const stepKey = scope.generateUid("step");
  216. const stepValue = _core.types.memberExpression(_core.types.identifier(stepKey), _core.types.identifier("value"));
  217. if (_core.types.isIdentifier(left) || _core.types.isPattern(left) || _core.types.isMemberExpression(left)) {
  218. declar = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, stepValue));
  219. } else if (_core.types.isVariableDeclaration(left)) {
  220. declar = _core.types.variableDeclaration(left.kind, [_core.types.variableDeclarator(left.declarations[0].id, stepValue)]);
  221. } else {
  222. throw file.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`);
  223. }
  224. const template = buildForOf({
  225. ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
  226. ITERATOR_COMPLETION: scope.generateUidIdentifier("iteratorNormalCompletion"),
  227. ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"),
  228. ITERATOR_KEY: scope.generateUidIdentifier("iterator"),
  229. STEP_KEY: _core.types.identifier(stepKey),
  230. OBJECT: node.right
  231. });
  232. const isLabeledParent = _core.types.isLabeledStatement(parent);
  233. const tryBody = template[3].block.body;
  234. const loop = tryBody[0];
  235. if (isLabeledParent) {
  236. tryBody[0] = _core.types.labeledStatement(parent.label, loop);
  237. }
  238. return {
  239. replaceParent: isLabeledParent,
  240. declar: declar,
  241. loop: loop,
  242. node: template
  243. };
  244. }
  245. });
  246. exports.default = _default;