update
This commit is contained in:
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
> Compile ES2015 for...of to ES5
|
||||
|
||||
See our website [@babel/plugin-transform-for-of](https://babeljs.io/docs/en/next/babel-plugin-transform-for-of.html) for more information.
|
||||
See our website [@babel/plugin-transform-for-of](https://babeljs.io/docs/en/babel-plugin-transform-for-of) for more information.
|
||||
|
||||
## Install
|
||||
|
||||
|
||||
+61
-52
@@ -9,31 +9,55 @@ var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
||||
|
||||
var _core = require("@babel/core");
|
||||
|
||||
var _noHelperImplementation = _interopRequireDefault(require("./no-helper-implementation"));
|
||||
var _noHelperImplementation = require("./no-helper-implementation");
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
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 = (0, _helperPluginUtils.declare)((api, options) => {
|
||||
var _options$assumeArray, _options$allowArrayLi, _api$assumption;
|
||||
|
||||
api.assertVersion(7);
|
||||
const {
|
||||
loose,
|
||||
assumeArray,
|
||||
allowArrayLike
|
||||
} = options;
|
||||
{
|
||||
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 (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 skipteratorClosing = (_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 (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`);
|
||||
}
|
||||
|
||||
if (assumeArray) {
|
||||
if (iterableIsArray) {
|
||||
return {
|
||||
name: "transform-for-of",
|
||||
visitor: {
|
||||
@@ -72,34 +96,22 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
||||
assignment = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, item));
|
||||
}
|
||||
|
||||
let blockBody;
|
||||
const body = path.get("body");
|
||||
|
||||
if (body.isBlockStatement() && Object.keys(path.getBindingIdentifiers()).some(id => body.scope.hasOwnBinding(id))) {
|
||||
blockBody = _core.types.blockStatement([assignment, body.node]);
|
||||
} else {
|
||||
blockBody = _core.types.toBlock(body.node);
|
||||
blockBody.body.unshift(assignment);
|
||||
}
|
||||
|
||||
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)), blockBody));
|
||||
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)(`
|
||||
const buildForOfArray = (0, _core.template)`
|
||||
for (var KEY = 0, NAME = ARR; KEY < NAME.length; KEY++) BODY;
|
||||
`);
|
||||
|
||||
const buildForOfLoose = _core.template.statements(`
|
||||
for (var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT, ALLOW_ARRAY_LIKE), STEP_KEY;
|
||||
`;
|
||||
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, ALLOW_ARRAY_LIKE), STEP_KEY;
|
||||
`;
|
||||
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) {
|
||||
@@ -107,10 +119,9 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
||||
} finally {
|
||||
ITERATOR_HELPER.f();
|
||||
}
|
||||
`);
|
||||
|
||||
const builder = loose ? {
|
||||
build: buildForOfLoose,
|
||||
`;
|
||||
const builder = skipteratorClosing ? {
|
||||
build: buildForOfNoIteratorClosing,
|
||||
helper: "createForOfIteratorHelperLoose",
|
||||
getContainer: nodes => nodes
|
||||
} : {
|
||||
@@ -135,19 +146,19 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
||||
|
||||
_core.types.inherits(loop, node);
|
||||
|
||||
_core.types.ensureBlock(loop);
|
||||
|
||||
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;
|
||||
loop.body.body.unshift(left);
|
||||
declar = left;
|
||||
} else {
|
||||
loop.body.body.unshift(_core.types.expressionStatement(_core.types.assignmentExpression("=", left, iterationValue)));
|
||||
declar = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, iterationValue));
|
||||
}
|
||||
|
||||
loop.body = buildLoopBody(path, declar, loop.body);
|
||||
return loop;
|
||||
}
|
||||
|
||||
@@ -163,7 +174,7 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
||||
}
|
||||
|
||||
if (!state.availableHelper(builder.helper)) {
|
||||
(0, _noHelperImplementation.default)(loose, path, state);
|
||||
(0, _noHelperImplementation.default)(skipteratorClosing, path, state);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -184,15 +195,13 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
||||
declar = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, stepValue));
|
||||
}
|
||||
|
||||
path.ensureBlock();
|
||||
node.body.body.unshift(declar);
|
||||
const nodes = builder.build({
|
||||
CREATE_ITERATOR_HELPER: state.addHelper(builder.helper),
|
||||
ITERATOR_HELPER: scope.generateUidIdentifier("iterator"),
|
||||
ALLOW_ARRAY_LIKE: allowArrayLike ? _core.types.booleanLiteral(true) : null,
|
||||
ARRAY_LIKE_IS_ITERABLE: arrayLikeIsIterable ? _core.types.booleanLiteral(true) : null,
|
||||
STEP_KEY: _core.types.identifier(stepKey),
|
||||
OBJECT: node.right,
|
||||
BODY: node.body
|
||||
BODY: buildLoopBody(path, declar)
|
||||
});
|
||||
const container = builder.getContainer(nodes);
|
||||
|
||||
@@ -203,7 +212,7 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
||||
if (_core.types.isLabeledStatement(parent)) {
|
||||
container[0] = _core.types.labeledStatement(parent.label, container[0]);
|
||||
path.parentPath.replaceWithMultiple(nodes);
|
||||
path.remove();
|
||||
path.skip();
|
||||
} else {
|
||||
path.replaceWithMultiple(nodes);
|
||||
}
|
||||
|
||||
+8
-7
@@ -22,7 +22,7 @@ function transformWithoutHelper(loose, path, state) {
|
||||
block.body.push(declar);
|
||||
}
|
||||
|
||||
block.body = block.body.concat(node.body.body);
|
||||
block.body.push(...node.body.body);
|
||||
|
||||
_core.types.inherits(loop, node);
|
||||
|
||||
@@ -36,7 +36,7 @@ function transformWithoutHelper(loose, path, state) {
|
||||
}
|
||||
}
|
||||
|
||||
const buildForOfLoose = (0, _core.template)(`
|
||||
const buildForOfLoose = _core.template.statement(`
|
||||
for (var LOOP_OBJECT = OBJECT,
|
||||
IS_ARRAY = Array.isArray(LOOP_OBJECT),
|
||||
INDEX = 0,
|
||||
@@ -52,7 +52,8 @@ const buildForOfLoose = (0, _core.template)(`
|
||||
}
|
||||
}
|
||||
`);
|
||||
const buildForOf = (0, _core.template)(`
|
||||
|
||||
const buildForOf = _core.template.statements(`
|
||||
var ITERATOR_COMPLETION = true;
|
||||
var ITERATOR_HAD_ERROR_KEY = false;
|
||||
var ITERATOR_ERROR_KEY = undefined;
|
||||
@@ -78,7 +79,7 @@ const buildForOf = (0, _core.template)(`
|
||||
}
|
||||
`);
|
||||
|
||||
function pushComputedPropsLoose(path, file) {
|
||||
function pushComputedPropsLoose(path, state) {
|
||||
const {
|
||||
node,
|
||||
scope,
|
||||
@@ -97,7 +98,7 @@ function pushComputedPropsLoose(path, file) {
|
||||
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 file.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`);
|
||||
throw state.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`);
|
||||
}
|
||||
|
||||
const iteratorKey = scope.generateUidIdentifier("iterator");
|
||||
@@ -127,7 +128,7 @@ function pushComputedPropsLoose(path, file) {
|
||||
};
|
||||
}
|
||||
|
||||
function pushComputedPropsSpec(path, file) {
|
||||
function pushComputedPropsSpec(path, state) {
|
||||
const {
|
||||
node,
|
||||
scope,
|
||||
@@ -144,7 +145,7 @@ function pushComputedPropsSpec(path, file) {
|
||||
} else if (_core.types.isVariableDeclaration(left)) {
|
||||
declar = _core.types.variableDeclaration(left.kind, [_core.types.variableDeclarator(left.declarations[0].id, stepValue)]);
|
||||
} else {
|
||||
throw file.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`);
|
||||
throw state.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`);
|
||||
}
|
||||
|
||||
const template = buildForOf({
|
||||
|
||||
+25
-18
@@ -1,51 +1,57 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"@babel/plugin-transform-for-of@7.10.4",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"@babel/plugin-transform-for-of@7.18.8",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "@babel/plugin-transform-for-of@7.10.4",
|
||||
"_id": "@babel/plugin-transform-for-of@7.10.4",
|
||||
"_from": "@babel/plugin-transform-for-of@7.18.8",
|
||||
"_id": "@babel/plugin-transform-for-of@7.18.8",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-ItdQfAzu9AlEqmusA/65TqJ79eRcgGmpPPFvBnGILXZH975G0LNjP1yjHvGgfuCxqrPPueXOPe+FsvxmxKiHHQ==",
|
||||
"_integrity": "sha512-yEfTRnjuskWYo0k1mHUqrVWaZwrdq8AYbfrpqULOJOaucGSp4mNMVps+YtA8byoevxS/urwU75vyhQIxcCgiBQ==",
|
||||
"_location": "/@babel/plugin-transform-for-of",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "@babel/plugin-transform-for-of@7.10.4",
|
||||
"raw": "@babel/plugin-transform-for-of@7.18.8",
|
||||
"name": "@babel/plugin-transform-for-of",
|
||||
"escapedName": "@babel%2fplugin-transform-for-of",
|
||||
"scope": "@babel",
|
||||
"rawSpec": "7.10.4",
|
||||
"rawSpec": "7.18.8",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "7.10.4"
|
||||
"fetchSpec": "7.18.8"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/@babel/preset-env"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/@babel/plugin-transform-for-of/-/plugin-transform-for-of-7.10.4.tgz",
|
||||
"_spec": "7.10.4",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/@babel/plugin-transform-for-of/-/plugin-transform-for-of-7.18.8.tgz",
|
||||
"_spec": "7.18.8",
|
||||
"_where": "/home/node/nuxt",
|
||||
"author": {
|
||||
"name": "The Babel Team",
|
||||
"url": "https://babel.dev/team"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/babel/babel/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/helper-plugin-utils": "^7.10.4"
|
||||
"@babel/helper-plugin-utils": "^7.18.6"
|
||||
},
|
||||
"description": "Compile ES2015 for...of to ES5",
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.10.4",
|
||||
"@babel/helper-plugin-test-runner": "^7.10.4"
|
||||
"@babel/core": "^7.18.6",
|
||||
"@babel/helper-plugin-test-runner": "^7.18.6"
|
||||
},
|
||||
"gitHead": "7fd40d86a0d03ff0e9c3ea16b29689945433d4df",
|
||||
"homepage": "https://github.com/babel/babel#readme",
|
||||
"engines": {
|
||||
"node": ">=6.9.0"
|
||||
},
|
||||
"homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-for-of",
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"main": "./lib/index.js",
|
||||
"name": "@babel/plugin-transform-for-of",
|
||||
"peerDependencies": {
|
||||
"@babel/core": "^7.0.0-0"
|
||||
@@ -58,5 +64,6 @@
|
||||
"url": "git+https://github.com/babel/babel.git",
|
||||
"directory": "packages/babel-plugin-transform-for-of"
|
||||
},
|
||||
"version": "7.10.4"
|
||||
"type": "commonjs",
|
||||
"version": "7.18.8"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user