no-helper-implementation.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = transformWithoutHelper;
  6. var _core = require("@babel/core");
  7. function transformWithoutHelper(loose, path, state) {
  8. const pushComputedProps = loose ? pushComputedPropsLoose : pushComputedPropsSpec;
  9. const {
  10. node
  11. } = path;
  12. const build = pushComputedProps(path, state);
  13. const declar = build.declar;
  14. const loop = build.loop;
  15. const block = loop.body;
  16. path.ensureBlock();
  17. if (declar) {
  18. block.body.push(declar);
  19. }
  20. block.body = block.body.concat(node.body.body);
  21. _core.types.inherits(loop, node);
  22. _core.types.inherits(loop.body, node.body);
  23. if (build.replaceParent) {
  24. path.parentPath.replaceWithMultiple(build.node);
  25. path.remove();
  26. } else {
  27. path.replaceWithMultiple(build.node);
  28. }
  29. }
  30. const buildForOfLoose = (0, _core.template)(`
  31. for (var LOOP_OBJECT = OBJECT,
  32. IS_ARRAY = Array.isArray(LOOP_OBJECT),
  33. INDEX = 0,
  34. LOOP_OBJECT = IS_ARRAY ? LOOP_OBJECT : LOOP_OBJECT[Symbol.iterator]();;) {
  35. INTERMEDIATE;
  36. if (IS_ARRAY) {
  37. if (INDEX >= LOOP_OBJECT.length) break;
  38. ID = LOOP_OBJECT[INDEX++];
  39. } else {
  40. INDEX = LOOP_OBJECT.next();
  41. if (INDEX.done) break;
  42. ID = INDEX.value;
  43. }
  44. }
  45. `);
  46. const buildForOf = (0, _core.template)(`
  47. var ITERATOR_COMPLETION = true;
  48. var ITERATOR_HAD_ERROR_KEY = false;
  49. var ITERATOR_ERROR_KEY = undefined;
  50. try {
  51. for (
  52. var ITERATOR_KEY = OBJECT[Symbol.iterator](), STEP_KEY;
  53. !(ITERATOR_COMPLETION = (STEP_KEY = ITERATOR_KEY.next()).done);
  54. ITERATOR_COMPLETION = true
  55. ) {}
  56. } catch (err) {
  57. ITERATOR_HAD_ERROR_KEY = true;
  58. ITERATOR_ERROR_KEY = err;
  59. } finally {
  60. try {
  61. if (!ITERATOR_COMPLETION && ITERATOR_KEY.return != null) {
  62. ITERATOR_KEY.return();
  63. }
  64. } finally {
  65. if (ITERATOR_HAD_ERROR_KEY) {
  66. throw ITERATOR_ERROR_KEY;
  67. }
  68. }
  69. }
  70. `);
  71. function pushComputedPropsLoose(path, file) {
  72. const {
  73. node,
  74. scope,
  75. parent
  76. } = path;
  77. const {
  78. left
  79. } = node;
  80. let declar, id, intermediate;
  81. if (_core.types.isIdentifier(left) || _core.types.isPattern(left) || _core.types.isMemberExpression(left)) {
  82. id = left;
  83. intermediate = null;
  84. } else if (_core.types.isVariableDeclaration(left)) {
  85. id = scope.generateUidIdentifier("ref");
  86. declar = _core.types.variableDeclaration(left.kind, [_core.types.variableDeclarator(left.declarations[0].id, _core.types.identifier(id.name))]);
  87. intermediate = _core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(id.name))]);
  88. } else {
  89. throw file.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`);
  90. }
  91. const iteratorKey = scope.generateUidIdentifier("iterator");
  92. const isArrayKey = scope.generateUidIdentifier("isArray");
  93. const loop = buildForOfLoose({
  94. LOOP_OBJECT: iteratorKey,
  95. IS_ARRAY: isArrayKey,
  96. OBJECT: node.right,
  97. INDEX: scope.generateUidIdentifier("i"),
  98. ID: id,
  99. INTERMEDIATE: intermediate
  100. });
  101. const isLabeledParent = _core.types.isLabeledStatement(parent);
  102. let labeled;
  103. if (isLabeledParent) {
  104. labeled = _core.types.labeledStatement(parent.label, loop);
  105. }
  106. return {
  107. replaceParent: isLabeledParent,
  108. declar: declar,
  109. node: labeled || loop,
  110. loop: loop
  111. };
  112. }
  113. function pushComputedPropsSpec(path, file) {
  114. const {
  115. node,
  116. scope,
  117. parent
  118. } = path;
  119. const left = node.left;
  120. let declar;
  121. const stepKey = scope.generateUid("step");
  122. const stepValue = _core.types.memberExpression(_core.types.identifier(stepKey), _core.types.identifier("value"));
  123. if (_core.types.isIdentifier(left) || _core.types.isPattern(left) || _core.types.isMemberExpression(left)) {
  124. declar = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, stepValue));
  125. } else if (_core.types.isVariableDeclaration(left)) {
  126. declar = _core.types.variableDeclaration(left.kind, [_core.types.variableDeclarator(left.declarations[0].id, stepValue)]);
  127. } else {
  128. throw file.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`);
  129. }
  130. const template = buildForOf({
  131. ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
  132. ITERATOR_COMPLETION: scope.generateUidIdentifier("iteratorNormalCompletion"),
  133. ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"),
  134. ITERATOR_KEY: scope.generateUidIdentifier("iterator"),
  135. STEP_KEY: _core.types.identifier(stepKey),
  136. OBJECT: node.right
  137. });
  138. const isLabeledParent = _core.types.isLabeledStatement(parent);
  139. const tryBody = template[3].block.body;
  140. const loop = tryBody[0];
  141. if (isLabeledParent) {
  142. tryBody[0] = _core.types.labeledStatement(parent.label, loop);
  143. }
  144. return {
  145. replaceParent: isLabeledParent,
  146. declar: declar,
  147. loop: loop,
  148. node: template
  149. };
  150. }