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