| 1 | import { type PluginPass, template, types as t } from "@babel/core"; |
| 2 | import type { NodePath } from "@babel/traverse"; |
| 3 | |
| 4 | // This is the legacy implementation, which inlines all the code. |
| 5 | // It must be kept for compatibility reasons. |
| 6 | // TODO (Babel 8): Remove this code. |
| 7 | |
| 8 | export default function transformWithoutHelper( |
| 9 | loose: boolean | void, |
| 10 | path: NodePath<t.ForOfStatement>, |
| 11 | state: PluginPass, |
| 12 | ) { |
| 13 | const pushComputedProps = loose |
| 14 | ? pushComputedPropsLoose |
| 15 | : pushComputedPropsSpec; |
| 16 | |
| 17 | const { node } = path; |
| 18 | const build = pushComputedProps(path, state); |
| 19 | const declar = build.declar; |
| 20 | const loop = build.loop; |
| 21 | const block = loop.body as t.BlockStatement; |
| 22 | |
| 23 | // ensure that it's a block so we can take all its statements |
| 24 | path.ensureBlock(); |
| 25 | |
| 26 | // add the value declaration to the new loop body |
| 27 | if (declar) { |
| 28 | block.body.push(declar); |
| 29 | } |
| 30 | |
| 31 | // push the rest of the original loop body onto our new body |
| 32 | block.body.push(...(node.body as t.BlockStatement).body); |
| 33 | |
| 34 | t.inherits(loop, node); |
| 35 | t.inherits(loop.body, node.body); |
| 36 | |
| 37 | if (build.replaceParent) { |
| 38 | path.parentPath.replaceWithMultiple(build.node); |
| 39 | path.remove(); |
| 40 | } else { |
| 41 | path.replaceWithMultiple(build.node); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | const buildForOfLoose = template.statement(` |
| 46 | for (var LOOP_OBJECT = OBJECT, |
| 47 | IS_ARRAY = Array.isArray(LOOP_OBJECT), |
| 48 | INDEX = 0, |
| 49 | LOOP_OBJECT = IS_ARRAY ? LOOP_OBJECT : LOOP_OBJECT[Symbol.iterator]();;) { |
| 50 | INTERMEDIATE; |
| 51 | if (IS_ARRAY) { |
| 52 | if (INDEX >= LOOP_OBJECT.length) break; |
| 53 | ID = LOOP_OBJECT[INDEX++]; |
| 54 | } else { |
| 55 | INDEX = LOOP_OBJECT.next(); |
| 56 | if (INDEX.done) break; |
| 57 | ID = INDEX.value; |
| 58 | } |
| 59 | } |
| 60 | `); |
| 61 | |
| 62 | const buildForOf = template.statements(` |
| 63 | var ITERATOR_COMPLETION = true; |
| 64 | var ITERATOR_HAD_ERROR_KEY = false; |
| 65 | var ITERATOR_ERROR_KEY = undefined; |
| 66 | try { |
| 67 | for ( |
| 68 | var ITERATOR_KEY = OBJECT[Symbol.iterator](), STEP_KEY; |
| 69 | !(ITERATOR_COMPLETION = (STEP_KEY = ITERATOR_KEY.next()).done); |
| 70 | ITERATOR_COMPLETION = true |
| 71 | ) {} |
| 72 | } catch (err) { |
| 73 | ITERATOR_HAD_ERROR_KEY = true; |
| 74 | ITERATOR_ERROR_KEY = err; |
| 75 | } finally { |
| 76 | try { |
| 77 | if (!ITERATOR_COMPLETION && ITERATOR_KEY.return != null) { |
| 78 | ITERATOR_KEY.return(); |
| 79 | } |
| 80 | } finally { |
| 81 | if (ITERATOR_HAD_ERROR_KEY) { |
| 82 | throw ITERATOR_ERROR_KEY; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | `); |
| 87 | |
| 88 | function pushComputedPropsLoose( |
| 89 | path: NodePath<t.ForOfStatement>, |
| 90 | state: PluginPass, |
| 91 | ) { |
| 92 | const { node, scope, parent } = path; |
| 93 | const { left } = node; |
| 94 | let declar, id, intermediate; |
| 95 | |
| 96 | if (t.isIdentifier(left) || t.isPattern(left) || t.isMemberExpression(left)) { |
| 97 | // for (i of test), for ({ i } of test) |
| 98 | id = left; |
| 99 | intermediate = null; |
| 100 | } else if (t.isVariableDeclaration(left)) { |
| 101 | // for (let i of test) |
| 102 | id = scope.generateUidIdentifier("ref"); |
| 103 | declar = t.variableDeclaration(left.kind, [ |
| 104 | t.variableDeclarator(left.declarations[0].id, t.identifier(id.name)), |
| 105 | ]); |
| 106 | intermediate = t.variableDeclaration("var", [ |
| 107 | t.variableDeclarator(t.identifier(id.name)), |
| 108 | ]); |
| 109 | } else { |
| 110 | throw state.buildCodeFrameError( |
| 111 | left, |
| 112 | `Unknown node type ${left.type} in ForStatement`, |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | const iteratorKey = scope.generateUidIdentifier("iterator"); |
| 117 | const isArrayKey = scope.generateUidIdentifier("isArray"); |
| 118 | |
| 119 | const loop = buildForOfLoose({ |
| 120 | LOOP_OBJECT: iteratorKey, |
| 121 | IS_ARRAY: isArrayKey, |
| 122 | OBJECT: node.right, |
| 123 | INDEX: scope.generateUidIdentifier("i"), |
| 124 | ID: id, |
| 125 | INTERMEDIATE: intermediate, |
| 126 | }) as t.ForStatement; |
| 127 | |
| 128 | // |
| 129 | const isLabeledParent = t.isLabeledStatement(parent); |
| 130 | let labeled; |
| 131 | |
| 132 | if (isLabeledParent) { |
| 133 | labeled = t.labeledStatement(parent.label, loop); |
| 134 | } |
| 135 | |
| 136 | return { |
| 137 | replaceParent: isLabeledParent, |
| 138 | declar: declar, |
| 139 | node: labeled || loop, |
| 140 | loop: loop, |
| 141 | }; |
| 142 | } |
| 143 | |
| 144 | function pushComputedPropsSpec( |
| 145 | path: NodePath<t.ForOfStatement>, |
| 146 | state: PluginPass, |
| 147 | ) { |
| 148 | const { node, scope, parent } = path; |
| 149 | const left = node.left; |
| 150 | let declar; |
| 151 | |
| 152 | const stepKey = scope.generateUid("step"); |
| 153 | const stepValue = t.memberExpression( |
| 154 | t.identifier(stepKey), |
| 155 | t.identifier("value"), |
| 156 | ); |
| 157 | |
| 158 | if (t.isIdentifier(left) || t.isPattern(left) || t.isMemberExpression(left)) { |
| 159 | // for (i of test), for ({ i } of test) |
| 160 | declar = t.expressionStatement( |
| 161 | t.assignmentExpression("=", left, stepValue), |
| 162 | ); |
| 163 | } else if (t.isVariableDeclaration(left)) { |
| 164 | // for (let i of test) |
| 165 | declar = t.variableDeclaration(left.kind, [ |
| 166 | t.variableDeclarator(left.declarations[0].id, stepValue), |
| 167 | ]); |
| 168 | } else { |
| 169 | throw state.buildCodeFrameError( |
| 170 | left, |
| 171 | `Unknown node type ${left.type} in ForStatement`, |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | const template = buildForOf({ |
| 176 | ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"), |
| 177 | ITERATOR_COMPLETION: scope.generateUidIdentifier( |
| 178 | "iteratorNormalCompletion", |
| 179 | ), |
| 180 | ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"), |
| 181 | ITERATOR_KEY: scope.generateUidIdentifier("iterator"), |
| 182 | STEP_KEY: t.identifier(stepKey), |
| 183 | OBJECT: node.right, |
| 184 | }); |
| 185 | |
| 186 | const isLabeledParent = t.isLabeledStatement(parent); |
| 187 | |
| 188 | const tryBody = (template[3] as t.TryStatement).block.body; |
| 189 | const loop = tryBody[0] as t.ForStatement; |
| 190 | |
| 191 | if (isLabeledParent) { |
| 192 | tryBody[0] = t.labeledStatement(parent.label, loop); |
| 193 | } |
| 194 | |
| 195 | // |
| 196 | |
| 197 | return { |
| 198 | replaceParent: isLabeledParent, |
| 199 | declar: declar, |
| 200 | loop: loop, |
| 201 | node: template, |
| 202 | }; |
| 203 | } |