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