@babel/plugin-transform-for-of
Advanced tools
Comparing version 7.22.5 to 8.0.0-alpha.0
130
lib/index.js
@@ -1,18 +0,54 @@ | ||
"use strict"; | ||
import { declare } from '@babel/helper-plugin-utils'; | ||
import { template, types } from '@babel/core'; | ||
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"); | ||
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; | ||
} | ||
} | ||
`); | ||
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 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]); | ||
const body = newBody ?? bodyPath.node; | ||
if (types.isBlockStatement(body) && Object.keys(path.getBindingIdentifiers()).some(id => bodyPath.scope.hasOwnBinding(id))) { | ||
block = types.blockStatement([declar, body]); | ||
} else { | ||
block = _core.types.toBlock(body); | ||
block = types.toBlock(body); | ||
block.body.unshift(declar); | ||
@@ -22,4 +58,3 @@ } | ||
} | ||
var _default = (0, _helperPluginUtils.declare)((api, options) => { | ||
var _options$assumeArray, _options$allowArrayLi, _api$assumption; | ||
var index = declare((api, options) => { | ||
api.assertVersion(7); | ||
@@ -38,11 +73,6 @@ { | ||
} | ||
{ | ||
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; | ||
const iterableIsArray = options.assumeArray ?? (!options.loose && api.assumption("iterableIsArray")); | ||
const arrayLikeIsIterable = options.allowArrayLike ?? api.assumption("arrayLikeIsIterable"); | ||
const skipIteratorClosing = api.assumption("skipForOfIteratorClosing") ?? options.loose; | ||
if (iterableIsArray && arrayLikeIsIterable) { | ||
@@ -69,17 +99,17 @@ throw new Error(`The "iterableIsArray" and "arrayLikeIsIterable" assumptions are not compatible.`); | ||
let array = scope.maybeGenerateMemoised(right, true); | ||
const inits = [_core.types.variableDeclarator(i, _core.types.numericLiteral(0))]; | ||
const inits = [types.variableDeclarator(i, types.numericLiteral(0))]; | ||
if (array) { | ||
inits.push(_core.types.variableDeclarator(array, right)); | ||
inits.push(types.variableDeclarator(array, right)); | ||
} else { | ||
array = right; | ||
} | ||
const item = _core.types.memberExpression(_core.types.cloneNode(array), _core.types.cloneNode(i), true); | ||
const item = types.memberExpression(types.cloneNode(array), types.cloneNode(i), true); | ||
let assignment; | ||
if (_core.types.isVariableDeclaration(left)) { | ||
if (types.isVariableDeclaration(left)) { | ||
assignment = left; | ||
assignment.declarations[0].init = item; | ||
} else { | ||
assignment = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, item)); | ||
assignment = types.expressionStatement(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))); | ||
path.replaceWith(types.forStatement(types.variableDeclaration("let", inits), types.binaryExpression("<", types.cloneNode(i), types.memberExpression(types.cloneNode(array), types.identifier("length"))), types.updateExpression("++", types.cloneNode(i)), buildLoopBody(path, assignment))); | ||
} | ||
@@ -89,10 +119,10 @@ } | ||
} | ||
const buildForOfArray = (0, _core.template)` | ||
const buildForOfArray = template` | ||
for (var KEY = 0, NAME = ARR; KEY < NAME.length; KEY++) BODY; | ||
`; | ||
const buildForOfNoIteratorClosing = _core.template.statements` | ||
const buildForOfNoIteratorClosing = 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` | ||
const buildForOf = template.statements` | ||
var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT, ARRAY_LIKE_IS_ITERABLE), STEP_KEY; | ||
@@ -129,11 +159,11 @@ try { | ||
}); | ||
_core.types.inherits(loop, node); | ||
const iterationValue = _core.types.memberExpression(_core.types.cloneNode(right), _core.types.cloneNode(iterationKey), true); | ||
types.inherits(loop, node); | ||
const iterationValue = types.memberExpression(types.cloneNode(right), types.cloneNode(iterationKey), true); | ||
let declar; | ||
const left = node.left; | ||
if (_core.types.isVariableDeclaration(left)) { | ||
if (types.isVariableDeclaration(left)) { | ||
left.declarations[0].init = iterationValue; | ||
declar = left; | ||
} else { | ||
declar = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, iterationValue)); | ||
declar = types.expressionStatement(types.assignmentExpression("=", left, iterationValue)); | ||
} | ||
@@ -148,12 +178,6 @@ loop.body = buildLoopBody(path, declar, loop.body); | ||
const right = path.get("right"); | ||
if (right.isArrayExpression() || right.isGenericType("Array") || _core.types.isArrayTypeAnnotation(right.getTypeAnnotation())) { | ||
if (right.isArrayExpression() || right.isGenericType("Array")) { | ||
path.replaceWith(_ForOfStatementArray(path)); | ||
return; | ||
} | ||
{ | ||
if (!state.availableHelper(builder.helper)) { | ||
(0, _noHelperImplementation.default)(skipIteratorClosing, path, state); | ||
return; | ||
} | ||
} | ||
const { | ||
@@ -167,7 +191,7 @@ node, | ||
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)]); | ||
const stepValue = types.memberExpression(types.identifier(stepKey), types.identifier("value")); | ||
if (types.isVariableDeclaration(left)) { | ||
declar = types.variableDeclaration(left.kind, [types.variableDeclarator(left.declarations[0].id, stepValue)]); | ||
} else { | ||
declar = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, stepValue)); | ||
declar = types.expressionStatement(types.assignmentExpression("=", left, stepValue)); | ||
} | ||
@@ -177,4 +201,4 @@ const nodes = builder.build({ | ||
ITERATOR_HELPER: scope.generateUidIdentifier("iterator"), | ||
ARRAY_LIKE_IS_ITERABLE: arrayLikeIsIterable ? _core.types.booleanLiteral(true) : null, | ||
STEP_KEY: _core.types.identifier(stepKey), | ||
ARRAY_LIKE_IS_ITERABLE: arrayLikeIsIterable ? types.booleanLiteral(true) : null, | ||
STEP_KEY: types.identifier(stepKey), | ||
OBJECT: node.right, | ||
@@ -184,6 +208,6 @@ 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]); | ||
types.inherits(container[0], node); | ||
types.inherits(container[0].body, node.body); | ||
if (types.isLabeledStatement(parent)) { | ||
container[0] = types.labeledStatement(parent.label, container[0]); | ||
path.parentPath.replaceWithMultiple(nodes); | ||
@@ -198,4 +222,4 @@ path.skip(); | ||
}); | ||
exports.default = _default; | ||
export { index as default }; | ||
//# sourceMappingURL=index.js.map |
@@ -1,9 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = transformWithoutHelper; | ||
var _core = require("@babel/core"); | ||
function transformWithoutHelper(loose, path, state) { | ||
import { template, types as t } from "@babel/core"; | ||
export default function transformWithoutHelper(loose, path, state) { | ||
const pushComputedProps = loose ? pushComputedPropsLoose : pushComputedPropsSpec; | ||
@@ -22,4 +16,4 @@ const { | ||
block.body.push(...node.body.body); | ||
_core.types.inherits(loop, node); | ||
_core.types.inherits(loop.body, node.body); | ||
t.inherits(loop, node); | ||
t.inherits(loop.body, node.body); | ||
if (build.replaceParent) { | ||
@@ -32,3 +26,3 @@ path.parentPath.replaceWithMultiple(build.node); | ||
} | ||
const buildForOfLoose = _core.template.statement(` | ||
const buildForOfLoose = template.statement(` | ||
for (var LOOP_OBJECT = OBJECT, | ||
@@ -49,3 +43,3 @@ IS_ARRAY = Array.isArray(LOOP_OBJECT), | ||
`); | ||
const buildForOf = _core.template.statements(` | ||
const buildForOf = template.statements(` | ||
var ITERATOR_COMPLETION = true; | ||
@@ -85,9 +79,9 @@ var ITERATOR_HAD_ERROR_KEY = false; | ||
let declar, id, intermediate; | ||
if (_core.types.isIdentifier(left) || _core.types.isPattern(left) || _core.types.isMemberExpression(left)) { | ||
if (t.isIdentifier(left) || t.isPattern(left) || t.isMemberExpression(left)) { | ||
id = left; | ||
intermediate = null; | ||
} else if (_core.types.isVariableDeclaration(left)) { | ||
} else if (t.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))]); | ||
declar = t.variableDeclaration(left.kind, [t.variableDeclarator(left.declarations[0].id, t.identifier(id.name))]); | ||
intermediate = t.variableDeclaration("var", [t.variableDeclarator(t.identifier(id.name))]); | ||
} else { | ||
@@ -106,6 +100,6 @@ throw state.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`); | ||
}); | ||
const isLabeledParent = _core.types.isLabeledStatement(parent); | ||
const isLabeledParent = t.isLabeledStatement(parent); | ||
let labeled; | ||
if (isLabeledParent) { | ||
labeled = _core.types.labeledStatement(parent.label, loop); | ||
labeled = t.labeledStatement(parent.label, loop); | ||
} | ||
@@ -128,7 +122,7 @@ return { | ||
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)]); | ||
const stepValue = t.memberExpression(t.identifier(stepKey), t.identifier("value")); | ||
if (t.isIdentifier(left) || t.isPattern(left) || t.isMemberExpression(left)) { | ||
declar = t.expressionStatement(t.assignmentExpression("=", left, stepValue)); | ||
} else if (t.isVariableDeclaration(left)) { | ||
declar = t.variableDeclaration(left.kind, [t.variableDeclarator(left.declarations[0].id, stepValue)]); | ||
} else { | ||
@@ -142,10 +136,10 @@ throw state.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`); | ||
ITERATOR_KEY: scope.generateUidIdentifier("iterator"), | ||
STEP_KEY: _core.types.identifier(stepKey), | ||
STEP_KEY: t.identifier(stepKey), | ||
OBJECT: node.right | ||
}); | ||
const isLabeledParent = _core.types.isLabeledStatement(parent); | ||
const isLabeledParent = t.isLabeledStatement(parent); | ||
const tryBody = template[3].block.body; | ||
const loop = tryBody[0]; | ||
if (isLabeledParent) { | ||
tryBody[0] = _core.types.labeledStatement(parent.label, loop); | ||
tryBody[0] = t.labeledStatement(parent.label, loop); | ||
} | ||
@@ -152,0 +146,0 @@ return { |
{ | ||
"name": "@babel/plugin-transform-for-of", | ||
"version": "7.22.5", | ||
"version": "8.0.0-alpha.0", | ||
"description": "Compile ES2015 for...of to ES5", | ||
@@ -20,16 +20,20 @@ "repository": { | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "^7.22.5" | ||
"@babel/helper-plugin-utils": "^8.0.0-alpha.0" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "^7.0.0-0" | ||
"@babel/core": "^8.0.0-alpha.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.22.5", | ||
"@babel/helper-plugin-test-runner": "^7.22.5" | ||
"@babel/core": "^8.0.0-alpha.0", | ||
"@babel/helper-plugin-test-runner": "^8.0.0-alpha.0" | ||
}, | ||
"engines": { | ||
"node": ">=6.9.0" | ||
"node": "^16.20.0 || ^18.16.0 || >=20.0.0" | ||
}, | ||
"author": "The Babel Team (https://babel.dev/team)", | ||
"type": "commonjs" | ||
"exports": { | ||
".": "./lib/index.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"type": "module" | ||
} |
@@ -5,3 +5,3 @@ # @babel/plugin-transform-for-of | ||
See our website [@babel/plugin-transform-for-of](https://babeljs.io/docs/en/babel-plugin-transform-for-of) for more information. | ||
See our website [@babel/plugin-transform-for-of](https://babeljs.io/docs/babel-plugin-transform-for-of) for more information. | ||
@@ -8,0 +8,0 @@ ## Install |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
49042
359
Yes
1
+ Added@babel/code-frame@8.0.0-alpha.11(transitive)
+ Added@babel/compat-data@8.0.0-alpha.11(transitive)
+ Added@babel/core@8.0.0-alpha.11(transitive)
+ Added@babel/generator@8.0.0-alpha.11(transitive)
+ Added@babel/helper-compilation-targets@8.0.0-alpha.11(transitive)
+ Added@babel/helper-environment-visitor@8.0.0-alpha.11(transitive)
+ Added@babel/helper-function-name@8.0.0-alpha.11(transitive)
+ Added@babel/helper-hoist-variables@8.0.0-alpha.11(transitive)
+ Added@babel/helper-plugin-utils@8.0.0-alpha.11(transitive)
+ Added@babel/helper-split-export-declaration@8.0.0-alpha.11(transitive)
+ Added@babel/helper-string-parser@8.0.0-alpha.11(transitive)
+ Added@babel/helper-validator-identifier@8.0.0-alpha.11(transitive)
+ Added@babel/helper-validator-option@8.0.0-alpha.11(transitive)
+ Added@babel/helpers@8.0.0-alpha.11(transitive)
+ Added@babel/highlight@8.0.0-alpha.11(transitive)
+ Added@babel/parser@8.0.0-alpha.11(transitive)
+ Added@babel/template@8.0.0-alpha.11(transitive)
+ Added@babel/traverse@8.0.0-alpha.11(transitive)
+ Added@babel/types@8.0.0-alpha.11(transitive)
+ Addedglobals@13.24.0(transitive)
+ Addedjs-tokens@8.0.3(transitive)
+ Addedjsesc@3.0.2(transitive)
+ Addedlru-cache@7.18.3(transitive)
+ Addedsemver@7.6.2(transitive)
+ Addedto-fast-properties@3.0.1(transitive)
+ Addedtype-fest@0.20.2(transitive)
- Removed@babel/code-frame@7.24.7(transitive)
- Removed@babel/compat-data@7.24.7(transitive)
- Removed@babel/core@7.24.7(transitive)
- Removed@babel/generator@7.24.7(transitive)
- Removed@babel/helper-compilation-targets@7.24.7(transitive)
- Removed@babel/helper-environment-visitor@7.24.7(transitive)
- Removed@babel/helper-function-name@7.24.7(transitive)
- Removed@babel/helper-hoist-variables@7.24.7(transitive)
- Removed@babel/helper-module-imports@7.24.7(transitive)
- Removed@babel/helper-module-transforms@7.24.7(transitive)
- Removed@babel/helper-plugin-utils@7.24.7(transitive)
- Removed@babel/helper-simple-access@7.24.7(transitive)
- Removed@babel/helper-split-export-declaration@7.24.7(transitive)
- Removed@babel/helper-string-parser@7.24.7(transitive)
- Removed@babel/helper-validator-identifier@7.24.7(transitive)
- Removed@babel/helper-validator-option@7.24.7(transitive)
- Removed@babel/helpers@7.24.7(transitive)
- Removed@babel/highlight@7.24.7(transitive)
- Removed@babel/parser@7.24.7(transitive)
- Removed@babel/template@7.24.7(transitive)
- Removed@babel/traverse@7.24.7(transitive)
- Removed@babel/types@7.24.7(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedglobals@11.12.0(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedjs-tokens@4.0.0(transitive)
- Removedjsesc@2.5.2(transitive)
- Removedlru-cache@5.1.1(transitive)
- Removedsemver@6.3.1(transitive)
- Removedsupports-color@5.5.0(transitive)
- Removedto-fast-properties@2.0.0(transitive)
- Removedyallist@3.1.1(transitive)