Befor generating
This commit is contained in:
1 parent
3383f4bf4a
commit
1aa1b5f625
6756 files changed
+649946
-1
No files matched your search
+190
@@ -0,0 +1,190 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
||||
var _core = require("@babel/core");
|
||||
var _noHelperImplementation = require("./no-helper-implementation.js");
|
||||
var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers");
|
||||
function buildLoopBody(path, declar, newBody) {
|
||||
let block;
|
||||
const bodyPath = path.get("body");
|
||||
const body = newBody != null ? newBody : bodyPath.node;
|
||||
if (_core.types.isBlockStatement(body) && Object.keys(path.getBindingIdentifiers()).some(id => bodyPath.scope.hasOwnBinding(id))) {
|
||||
block = _core.types.blockStatement([declar, body]);
|
||||
} else {
|
||||
block = _core.types.toBlock(body);
|
||||
block.body.unshift(declar);
|
||||
}
|
||||
return block;
|
||||
}
|
||||
var _default = exports.default = (0, _helperPluginUtils.declare)((api, options) => {
|
||||
var _options$assumeArray, _options$allowArrayLi, _api$assumption;
|
||||
api.assertVersion(7);
|
||||
{
|
||||
const {
|
||||
assumeArray,
|
||||
allowArrayLike,
|
||||
loose
|
||||
} = options;
|
||||
if (loose === true && assumeArray === true) {
|
||||
throw new Error(`The loose and assumeArray options cannot be used together in @babel/plugin-transform-for-of`);
|
||||
}
|
||||
if (assumeArray === true && allowArrayLike === true) {
|
||||
throw new Error(`The assumeArray and allowArrayLike options cannot be used together in @babel/plugin-transform-for-of`);
|
||||
}
|
||||
if (allowArrayLike && /^7\.\d\./.test(api.version)) {
|
||||
throw new Error(`The allowArrayLike is only supported when using @babel/core@^7.10.0`);
|
||||
}
|
||||
}
|
||||
const iterableIsArray = (_options$assumeArray = options.assumeArray) != null ? _options$assumeArray : !options.loose && api.assumption("iterableIsArray");
|
||||
const arrayLikeIsIterable = (_options$allowArrayLi = options.allowArrayLike) != null ? _options$allowArrayLi : api.assumption("arrayLikeIsIterable");
|
||||
const skipIteratorClosing = (_api$assumption = api.assumption("skipForOfIteratorClosing")) != null ? _api$assumption : options.loose;
|
||||
if (iterableIsArray && arrayLikeIsIterable) {
|
||||
throw new Error(`The "iterableIsArray" and "arrayLikeIsIterable" assumptions are not compatible.`);
|
||||
}
|
||||
if (iterableIsArray) {
|
||||
return {
|
||||
name: "transform-for-of",
|
||||
visitor: {
|
||||
ForOfStatement(path) {
|
||||
const {
|
||||
scope
|
||||
} = path;
|
||||
const {
|
||||
left,
|
||||
await: isAwait
|
||||
} = path.node;
|
||||
if (isAwait) {
|
||||
return;
|
||||
}
|
||||
const right = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes)(path.node.right);
|
||||
const i = scope.generateUidIdentifier("i");
|
||||
let array = scope.maybeGenerateMemoised(right, true);
|
||||
if (!array && _core.types.isIdentifier(right) && path.get("body").scope.hasOwnBinding(right.name)) {
|
||||
array = scope.generateUidIdentifier("arr");
|
||||
}
|
||||
const inits = [_core.types.variableDeclarator(i, _core.types.numericLiteral(0))];
|
||||
if (array) {
|
||||
inits.push(_core.types.variableDeclarator(array, right));
|
||||
} else {
|
||||
array = right;
|
||||
}
|
||||
const item = _core.types.memberExpression(_core.types.cloneNode(array), _core.types.cloneNode(i), true);
|
||||
let assignment;
|
||||
if (_core.types.isVariableDeclaration(left)) {
|
||||
assignment = left;
|
||||
assignment.declarations[0].init = item;
|
||||
} else {
|
||||
assignment = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, item));
|
||||
}
|
||||
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)));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
const buildForOfArray = (0, _core.template)`
|
||||
for (var KEY = 0, NAME = ARR; KEY < NAME.length; KEY++) BODY;
|
||||
`;
|
||||
const buildForOfNoIteratorClosing = _core.template.statements`
|
||||
for (var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT, ARRAY_LIKE_IS_ITERABLE), STEP_KEY;
|
||||
!(STEP_KEY = ITERATOR_HELPER()).done;) BODY;
|
||||
`;
|
||||
const buildForOf = _core.template.statements`
|
||||
var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT, ARRAY_LIKE_IS_ITERABLE), STEP_KEY;
|
||||
try {
|
||||
for (ITERATOR_HELPER.s(); !(STEP_KEY = ITERATOR_HELPER.n()).done;) BODY;
|
||||
} catch (err) {
|
||||
ITERATOR_HELPER.e(err);
|
||||
} finally {
|
||||
ITERATOR_HELPER.f();
|
||||
}
|
||||
`;
|
||||
const builder = skipIteratorClosing ? {
|
||||
build: buildForOfNoIteratorClosing,
|
||||
helper: "createForOfIteratorHelperLoose",
|
||||
getContainer: nodes => nodes
|
||||
} : {
|
||||
build: buildForOf,
|
||||
helper: "createForOfIteratorHelper",
|
||||
getContainer: nodes => nodes[1].block.body
|
||||
};
|
||||
function _ForOfStatementArray(path) {
|
||||
const {
|
||||
node,
|
||||
scope
|
||||
} = path;
|
||||
const right = scope.generateUidIdentifierBasedOnNode(node.right, "arr");
|
||||
const iterationKey = scope.generateUidIdentifier("i");
|
||||
const loop = buildForOfArray({
|
||||
BODY: node.body,
|
||||
KEY: iterationKey,
|
||||
NAME: right,
|
||||
ARR: node.right
|
||||
});
|
||||
_core.types.inherits(loop, node);
|
||||
const iterationValue = _core.types.memberExpression(_core.types.cloneNode(right), _core.types.cloneNode(iterationKey), true);
|
||||
let declar;
|
||||
const left = node.left;
|
||||
if (_core.types.isVariableDeclaration(left)) {
|
||||
left.declarations[0].init = iterationValue;
|
||||
declar = left;
|
||||
} else {
|
||||
declar = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, iterationValue));
|
||||
}
|
||||
loop.body = buildLoopBody(path, declar, loop.body);
|
||||
return loop;
|
||||
}
|
||||
return {
|
||||
name: "transform-for-of",
|
||||
visitor: {
|
||||
ForOfStatement(path, state) {
|
||||
const right = path.get("right");
|
||||
if (right.isArrayExpression() || right.isGenericType("Array") || _core.types.isArrayTypeAnnotation(right.getTypeAnnotation())) {
|
||||
path.replaceWith(_ForOfStatementArray(path));
|
||||
return;
|
||||
}
|
||||
if (!state.availableHelper(builder.helper)) {
|
||||
(0, _noHelperImplementation.default)(skipIteratorClosing, path, state);
|
||||
return;
|
||||
}
|
||||
const {
|
||||
node,
|
||||
parent,
|
||||
scope
|
||||
} = path;
|
||||
const left = node.left;
|
||||
let declar;
|
||||
const stepKey = scope.generateUid("step");
|
||||
const stepValue = _core.types.memberExpression(_core.types.identifier(stepKey), _core.types.identifier("value"));
|
||||
if (_core.types.isVariableDeclaration(left)) {
|
||||
declar = _core.types.variableDeclaration(left.kind, [_core.types.variableDeclarator(left.declarations[0].id, stepValue)]);
|
||||
} else {
|
||||
declar = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, stepValue));
|
||||
}
|
||||
const nodes = builder.build({
|
||||
CREATE_ITERATOR_HELPER: state.addHelper(builder.helper),
|
||||
ITERATOR_HELPER: scope.generateUidIdentifier("iterator"),
|
||||
ARRAY_LIKE_IS_ITERABLE: arrayLikeIsIterable ? _core.types.booleanLiteral(true) : null,
|
||||
STEP_KEY: _core.types.identifier(stepKey),
|
||||
OBJECT: node.right,
|
||||
BODY: buildLoopBody(path, declar)
|
||||
});
|
||||
const container = builder.getContainer(nodes);
|
||||
_core.types.inherits(container[0], node);
|
||||
_core.types.inherits(container[0].body, node.body);
|
||||
if (_core.types.isLabeledStatement(parent)) {
|
||||
container[0] = _core.types.labeledStatement(parent.label, container[0]);
|
||||
path.parentPath.replaceWithMultiple(nodes);
|
||||
path.skip();
|
||||
} else {
|
||||
path.replaceWithMultiple(nodes);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"names":["_helperPluginUtils","require","_core","_noHelperImplementation","_helperSkipTransparentExpressionWrappers","buildLoopBody","path","declar","newBody","block","bodyPath","get","body","node","t","isBlockStatement","Object","keys","getBindingIdentifiers","some","id","scope","hasOwnBinding","blockStatement","toBlock","unshift","_default","exports","default","declare","api","options","_options$assumeArray","_options$allowArrayLi","_api$assumption","assertVersion","assumeArray","allowArrayLike","loose","Error","test","version","iterableIsArray","assumption","arrayLikeIsIterable","skipIteratorClosing","name","visitor","ForOfStatement","left","await","isAwait","right","skipTransparentExprWrapperNodes","i","generateUidIdentifier","array","maybeGenerateMemoised","isIdentifier","inits","variableDeclarator","numericLiteral","push","item","memberExpression","cloneNode","assignment","isVariableDeclaration","declarations","init","expressionStatement","assignmentExpression","replaceWith","forStatement","variableDeclaration","binaryExpression","identifier","updateExpression","buildForOfArray","template","buildForOfNoIteratorClosing","statements","buildForOf","builder","build","helper","getContainer","nodes","_ForOfStatementArray","generateUidIdentifierBasedOnNode","iterationKey","loop","BODY","KEY","NAME","ARR","inherits","iterationValue","state","isArrayExpression","isGenericType","isArrayTypeAnnotation","getTypeAnnotation","availableHelper","transformWithoutHelper","parent","stepKey","generateUid","stepValue","kind","CREATE_ITERATOR_HELPER","addHelper","ITERATOR_HELPER","ARRAY_LIKE_IS_ITERABLE","booleanLiteral","STEP_KEY","OBJECT","container","isLabeledStatement","labeledStatement","label","parentPath","replaceWithMultiple","skip"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport { template, types as t, type NodePath } from \"@babel/core\";\n\nimport transformWithoutHelper from \"./no-helper-implementation.ts\" with { if: \"!process.env.BABEL_8_BREAKING\" };\nimport { skipTransparentExprWrapperNodes } from \"@babel/helper-skip-transparent-expression-wrappers\";\n\nexport interface Options {\n allowArrayLike?: boolean;\n assumeArray?: boolean;\n loose?: boolean;\n}\n\nfunction buildLoopBody(\n path: NodePath<t.ForXStatement>,\n declar: t.Statement,\n newBody?: t.Statement | t.Expression,\n) {\n let block;\n const bodyPath = path.get(\"body\");\n const body = newBody ?? bodyPath.node;\n if (\n t.isBlockStatement(body) &&\n Object.keys(path.getBindingIdentifiers()).some(id =>\n bodyPath.scope.hasOwnBinding(id),\n )\n ) {\n block = t.blockStatement([declar, body]);\n } else {\n block = t.toBlock(body);\n block.body.unshift(declar);\n }\n return block;\n}\n\nexport default declare((api, options: Options) => {\n api.assertVersion(REQUIRED_VERSION(7));\n\n {\n const { assumeArray, allowArrayLike, loose } = options;\n\n if (loose === true && assumeArray === true) {\n throw new Error(\n `The loose and assumeArray options cannot be used together in @babel/plugin-transform-for-of`,\n );\n }\n\n if (assumeArray === true && allowArrayLike === true) {\n throw new Error(\n `The assumeArray and allowArrayLike options cannot be used together in @babel/plugin-transform-for-of`,\n );\n }\n\n if (!process.env.BABEL_8_BREAKING) {\n // TODO: Remove in Babel 8\n if (allowArrayLike && /^7\\.\\d\\./.test(api.version)) {\n throw new Error(\n `The allowArrayLike is only supported when using @babel/core@^7.10.0`,\n );\n }\n }\n }\n\n const iterableIsArray =\n options.assumeArray ??\n // Loose mode is not compatible with 'assumeArray', so we shouldn't read\n // 'iterableIsArray' if 'loose' is true.\n (!options.loose && api.assumption(\"iterableIsArray\"));\n\n const arrayLikeIsIterable =\n options.allowArrayLike ?? api.assumption(\"arrayLikeIsIterable\");\n\n const skipIteratorClosing =\n api.assumption(\"skipForOfIteratorClosing\") ?? options.loose;\n\n if (iterableIsArray && arrayLikeIsIterable) {\n throw new Error(\n `The \"iterableIsArray\" and \"arrayLikeIsIterable\" assumptions are not compatible.`,\n );\n }\n\n if (iterableIsArray) {\n return {\n name: \"transform-for-of\",\n\n visitor: {\n ForOfStatement(path) {\n const { scope } = path;\n const { left, await: isAwait } = path.node;\n if (isAwait) {\n return;\n }\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n const right = skipTransparentExprWrapperNodes(\n path.node.right,\n ) as t.Expression;\n const i = scope.generateUidIdentifier(\"i\");\n let array: t.Identifier | t.ThisExpression =\n scope.maybeGenerateMemoised(right, true);\n if (\n !array &&\n Line truncated
|
||||
Generated
Vendored
+153
@@ -0,0 +1,153 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = transformWithoutHelper;
|
||||
var _core = require("@babel/core");
|
||||
function transformWithoutHelper(loose, path, state) {
|
||||
const pushComputedProps = loose ? pushComputedPropsLoose : pushComputedPropsSpec;
|
||||
const {
|
||||
node
|
||||
} = path;
|
||||
const build = pushComputedProps(path, state);
|
||||
const declar = build.declar;
|
||||
const loop = build.loop;
|
||||
const block = loop.body;
|
||||
path.ensureBlock();
|
||||
if (declar) {
|
||||
block.body.push(declar);
|
||||
}
|
||||
block.body.push(...node.body.body);
|
||||
_core.types.inherits(loop, node);
|
||||
_core.types.inherits(loop.body, node.body);
|
||||
if (build.replaceParent) {
|
||||
path.parentPath.replaceWithMultiple(build.node);
|
||||
path.remove();
|
||||
} else {
|
||||
path.replaceWithMultiple(build.node);
|
||||
}
|
||||
}
|
||||
const buildForOfLoose = _core.template.statement(`
|
||||
for (var LOOP_OBJECT = OBJECT,
|
||||
IS_ARRAY = Array.isArray(LOOP_OBJECT),
|
||||
INDEX = 0,
|
||||
LOOP_OBJECT = IS_ARRAY ? LOOP_OBJECT : LOOP_OBJECT[Symbol.iterator]();;) {
|
||||
INTERMEDIATE;
|
||||
if (IS_ARRAY) {
|
||||
if (INDEX >= LOOP_OBJECT.length) break;
|
||||
ID = LOOP_OBJECT[INDEX++];
|
||||
} else {
|
||||
INDEX = LOOP_OBJECT.next();
|
||||
if (INDEX.done) break;
|
||||
ID = INDEX.value;
|
||||
}
|
||||
}
|
||||
`);
|
||||
const buildForOf = _core.template.statements(`
|
||||
var ITERATOR_COMPLETION = true;
|
||||
var ITERATOR_HAD_ERROR_KEY = false;
|
||||
var ITERATOR_ERROR_KEY = undefined;
|
||||
try {
|
||||
for (
|
||||
var ITERATOR_KEY = OBJECT[Symbol.iterator](), STEP_KEY;
|
||||
!(ITERATOR_COMPLETION = (STEP_KEY = ITERATOR_KEY.next()).done);
|
||||
ITERATOR_COMPLETION = true
|
||||
) {}
|
||||
} catch (err) {
|
||||
ITERATOR_HAD_ERROR_KEY = true;
|
||||
ITERATOR_ERROR_KEY = err;
|
||||
} finally {
|
||||
try {
|
||||
if (!ITERATOR_COMPLETION && ITERATOR_KEY.return != null) {
|
||||
ITERATOR_KEY.return();
|
||||
}
|
||||
} finally {
|
||||
if (ITERATOR_HAD_ERROR_KEY) {
|
||||
throw ITERATOR_ERROR_KEY;
|
||||
}
|
||||
}
|
||||
}
|
||||
`);
|
||||
function pushComputedPropsLoose(path, state) {
|
||||
const {
|
||||
node,
|
||||
scope,
|
||||
parent
|
||||
} = path;
|
||||
const {
|
||||
left
|
||||
} = node;
|
||||
let declar, id, intermediate;
|
||||
if (_core.types.isIdentifier(left) || _core.types.isPattern(left) || _core.types.isMemberExpression(left)) {
|
||||
id = left;
|
||||
intermediate = null;
|
||||
} else if (_core.types.isVariableDeclaration(left)) {
|
||||
id = scope.generateUidIdentifier("ref");
|
||||
declar = _core.types.variableDeclaration(left.kind, [_core.types.variableDeclarator(left.declarations[0].id, _core.types.identifier(id.name))]);
|
||||
intermediate = _core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(id.name))]);
|
||||
} else {
|
||||
throw state.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`);
|
||||
}
|
||||
const iteratorKey = scope.generateUidIdentifier("iterator");
|
||||
const isArrayKey = scope.generateUidIdentifier("isArray");
|
||||
const loop = buildForOfLoose({
|
||||
LOOP_OBJECT: iteratorKey,
|
||||
IS_ARRAY: isArrayKey,
|
||||
OBJECT: node.right,
|
||||
INDEX: scope.generateUidIdentifier("i"),
|
||||
ID: id,
|
||||
INTERMEDIATE: intermediate
|
||||
});
|
||||
const isLabeledParent = _core.types.isLabeledStatement(parent);
|
||||
let labeled;
|
||||
if (isLabeledParent) {
|
||||
labeled = _core.types.labeledStatement(parent.label, loop);
|
||||
}
|
||||
return {
|
||||
replaceParent: isLabeledParent,
|
||||
declar: declar,
|
||||
node: labeled || loop,
|
||||
loop: loop
|
||||
};
|
||||
}
|
||||
function pushComputedPropsSpec(path, state) {
|
||||
const {
|
||||
node,
|
||||
scope,
|
||||
parent
|
||||
} = path;
|
||||
const left = node.left;
|
||||
let declar;
|
||||
const stepKey = scope.generateUid("step");
|
||||
const stepValue = _core.types.memberExpression(_core.types.identifier(stepKey), _core.types.identifier("value"));
|
||||
if (_core.types.isIdentifier(left) || _core.types.isPattern(left) || _core.types.isMemberExpression(left)) {
|
||||
declar = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, stepValue));
|
||||
} else if (_core.types.isVariableDeclaration(left)) {
|
||||
declar = _core.types.variableDeclaration(left.kind, [_core.types.variableDeclarator(left.declarations[0].id, stepValue)]);
|
||||
} else {
|
||||
throw state.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`);
|
||||
}
|
||||
const template = buildForOf({
|
||||
ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
|
||||
ITERATOR_COMPLETION: scope.generateUidIdentifier("iteratorNormalCompletion"),
|
||||
ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"),
|
||||
ITERATOR_KEY: scope.generateUidIdentifier("iterator"),
|
||||
STEP_KEY: _core.types.identifier(stepKey),
|
||||
OBJECT: node.right
|
||||
});
|
||||
const isLabeledParent = _core.types.isLabeledStatement(parent);
|
||||
const tryBody = template[3].block.body;
|
||||
const loop = tryBody[0];
|
||||
if (isLabeledParent) {
|
||||
tryBody[0] = _core.types.labeledStatement(parent.label, loop);
|
||||
}
|
||||
return {
|
||||
replaceParent: isLabeledParent,
|
||||
declar: declar,
|
||||
loop: loop,
|
||||
node: template
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=no-helper-implementation.js.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"names":["_core","require","transformWithoutHelper","loose","path","state","pushComputedProps","pushComputedPropsLoose","pushComputedPropsSpec","node","build","declar","loop","block","body","ensureBlock","push","t","inherits","replaceParent","parentPath","replaceWithMultiple","remove","buildForOfLoose","template","statement","buildForOf","statements","scope","parent","left","id","intermediate","isIdentifier","isPattern","isMemberExpression","isVariableDeclaration","generateUidIdentifier","variableDeclaration","kind","variableDeclarator","declarations","identifier","name","buildCodeFrameError","type","iteratorKey","isArrayKey","LOOP_OBJECT","IS_ARRAY","OBJECT","right","INDEX","ID","INTERMEDIATE","isLabeledParent","isLabeledStatement","labeled","labeledStatement","label","stepKey","generateUid","stepValue","memberExpression","expressionStatement","assignmentExpression","ITERATOR_HAD_ERROR_KEY","ITERATOR_COMPLETION","ITERATOR_ERROR_KEY","ITERATOR_KEY","STEP_KEY","tryBody"],"sources":["../src/no-helper-implementation.ts"],"sourcesContent":["if (process.env.BABEL_8_BREAKING && process.env.IS_PUBLISH) {\n throw new Error(\n \"Internal Babel error: This file should only be loaded in Babel 7\",\n );\n}\n\nimport { template, types as t } from \"@babel/core\";\nimport type { PluginPass, NodePath } from \"@babel/core\";\n\n// This is the legacy implementation, which inlines all the code.\n// It must be kept for compatibility reasons.\n// TODO(Babel 8): Remove this file.\n\nexport default function transformWithoutHelper(\n loose: boolean | void,\n path: NodePath<t.ForOfStatement>,\n state: PluginPass,\n) {\n const pushComputedProps = loose\n ? pushComputedPropsLoose\n : pushComputedPropsSpec;\n\n const { node } = path;\n const build = pushComputedProps(path, state);\n const declar = build.declar;\n const loop = build.loop;\n const block = loop.body as t.BlockStatement;\n\n // ensure that it's a block so we can take all its statements\n path.ensureBlock();\n\n // add the value declaration to the new loop body\n if (declar) {\n block.body.push(declar);\n }\n\n // push the rest of the original loop body onto our new body\n block.body.push(...(node.body as t.BlockStatement).body);\n\n t.inherits(loop, node);\n t.inherits(loop.body, node.body);\n\n if (build.replaceParent) {\n path.parentPath.replaceWithMultiple(build.node);\n path.remove();\n } else {\n path.replaceWithMultiple(build.node);\n }\n}\n\nconst buildForOfLoose = template.statement(`\n for (var LOOP_OBJECT = OBJECT,\n IS_ARRAY = Array.isArray(LOOP_OBJECT),\n INDEX = 0,\n LOOP_OBJECT = IS_ARRAY ? LOOP_OBJECT : LOOP_OBJECT[Symbol.iterator]();;) {\n INTERMEDIATE;\n if (IS_ARRAY) {\n if (INDEX >= LOOP_OBJECT.length) break;\n ID = LOOP_OBJECT[INDEX++];\n } else {\n INDEX = LOOP_OBJECT.next();\n if (INDEX.done) break;\n ID = INDEX.value;\n }\n }\n`);\n\nconst buildForOf = template.statements(`\n var ITERATOR_COMPLETION = true;\n var ITERATOR_HAD_ERROR_KEY = false;\n var ITERATOR_ERROR_KEY = undefined;\n try {\n for (\n var ITERATOR_KEY = OBJECT[Symbol.iterator](), STEP_KEY;\n !(ITERATOR_COMPLETION = (STEP_KEY = ITERATOR_KEY.next()).done);\n ITERATOR_COMPLETION = true\n ) {}\n } catch (err) {\n ITERATOR_HAD_ERROR_KEY = true;\n ITERATOR_ERROR_KEY = err;\n } finally {\n try {\n if (!ITERATOR_COMPLETION && ITERATOR_KEY.return != null) {\n ITERATOR_KEY.return();\n }\n } finally {\n if (ITERATOR_HAD_ERROR_KEY) {\n throw ITERATOR_ERROR_KEY;\n }\n }\n }\n`);\n\nfunction pushComputedPropsLoose(\n path: NodePath<t.ForOfStatement>,\n state: PluginPass,\n) {\n const { node, scope, parent } = path;\n const { left } = node;\n let declar, id, intermediate;\n\n if (t.isIdentifier(left) || t.isPattern(left) || t.isMemberExpression(left)) {\n // for (i of test), for ({ i } of test)\n id = left;\n intermediate = null;\n } else if (t.isVariableDeclaration(left)) {\n // for (let i of test)\n id = scope.generateUidIdentifier(\"ref\");\n declar = t.variableDeclaration(left.kind, [\n t.variableDeclarator(left.declarations[0].id, t.identifier(id.name)),\n ]);\n intermediate = t.variableDeclaration(\"var\", [\n t.variableDeclarator(t.identifier(id.name)),\n ]);\n } else {\n throw state.buildCodeFrameError(\n left,\n `Unknown node type ${left.type} in ForStatement`,\n );\n }\n\n const iteratorKey = scope.generateUidIdentifier(\"iterator\");\n const isArrayKey = scope.generateUidIdentifier(\"isArray\");\n\n const loop = buildForOfLoose({\n LOOP_OBJECT: iteratorKey,\n IS_ARRAY: isArrayKey,\n OBJECT: node.right,\n INDEX: scope.generateUidIdentifier(\"i\"),\n ID: id,\n INTERMEDIATE: intermediate,\n }) as t.ForStatement;\n\n //\n const isLabeledParent = t.isLabeledStatement(parent);\n let labeled;\n\n if (isLabeledPLine truncated
|
||||
Reference in new issue
Block a user