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