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