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