@babel/traverse
Advanced tools
Comparing version 7.24.8 to 7.25.0
@@ -17,2 +17,3 @@ "use strict"; | ||
exports.requeue = requeue; | ||
exports.requeueComputedKeyAndDecorators = requeueComputedKeyAndDecorators; | ||
exports.resync = resync; | ||
@@ -30,2 +31,3 @@ exports.setContext = setContext; | ||
var _removal = require("./removal.js"); | ||
var t = require("@babel/types"); | ||
function call(key) { | ||
@@ -214,2 +216,16 @@ const opts = this.opts; | ||
} | ||
function requeueComputedKeyAndDecorators() { | ||
const { | ||
context, | ||
node | ||
} = this; | ||
if (!t.isPrivate(node) && node.computed) { | ||
context.maybeQueue(this.get("key")); | ||
} | ||
if (node.decorators) { | ||
for (const decorator of this.get("decorators")) { | ||
context.maybeQueue(decorator); | ||
} | ||
} | ||
} | ||
function _getQueueContexts() { | ||
@@ -216,0 +232,0 @@ let path = this; |
@@ -8,7 +8,8 @@ "use strict"; | ||
exports.ensureBlock = ensureBlock; | ||
exports.ensureFunctionName = ensureFunctionName; | ||
exports.splitExportDeclaration = splitExportDeclaration; | ||
exports.toComputedKey = toComputedKey; | ||
exports.unwrapFunctionEnvironment = unwrapFunctionEnvironment; | ||
var _t = require("@babel/types"); | ||
var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor"); | ||
var _helperFunctionName = require("@babel/helper-function-name"); | ||
var _template = require("@babel/template"); | ||
var _visitors = require("../visitors.js"); | ||
@@ -40,3 +41,14 @@ const { | ||
toExpression, | ||
unaryExpression | ||
unaryExpression, | ||
toBindingIdentifierName, | ||
isFunction, | ||
isAssignmentPattern, | ||
isRestElement, | ||
getFunctionName, | ||
cloneNode, | ||
variableDeclaration, | ||
variableDeclarator, | ||
exportNamedDeclaration, | ||
exportSpecifier, | ||
inherits | ||
} = _t; | ||
@@ -115,6 +127,11 @@ function toComputedKey() { | ||
} | ||
let self = this; | ||
if (!noNewArrows) { | ||
var _self$ensureFunctionN; | ||
self = (_self$ensureFunctionN = self.ensureFunctionName(false)) != null ? _self$ensureFunctionN : self; | ||
} | ||
const { | ||
thisBinding, | ||
fnPath: fn | ||
} = hoistFunctionEnvironment(this, noNewArrows, allowInsertArrow, allowInsertArrowWithRest); | ||
} = hoistFunctionEnvironment(self, noNewArrows, allowInsertArrow, allowInsertArrowWithRest); | ||
fn.ensureBlock(); | ||
@@ -131,3 +148,3 @@ setType(fn, "FunctionExpression"); | ||
fn.get("body").unshiftContainer("body", expressionStatement(callExpression(this.hub.addHelper("newArrowCheck"), [thisExpression(), checkBinding ? identifier(checkBinding.name) : identifier(thisBinding)]))); | ||
fn.replaceWith(callExpression(memberExpression((0, _helperFunctionName.default)(this, true) || fn.node, identifier("bind")), [checkBinding ? identifier(checkBinding.name) : thisExpression()])); | ||
fn.replaceWith(callExpression(memberExpression(fn.node, identifier("bind")), [checkBinding ? identifier(checkBinding.name) : thisExpression()])); | ||
return fn.get("callee.object"); | ||
@@ -137,3 +154,3 @@ } | ||
} | ||
const getSuperCallsVisitor = (0, _visitors.merge)([{ | ||
const getSuperCallsVisitor = (0, _visitors.environmentVisitor)({ | ||
CallExpression(child, { | ||
@@ -145,3 +162,3 @@ allSuperCalls | ||
} | ||
}, _helperEnvironmentVisitor.default]); | ||
}); | ||
function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow = true, allowInsertArrowWithRest = true) { | ||
@@ -333,3 +350,3 @@ let arrowParent; | ||
} | ||
const assignSuperThisVisitor = (0, _visitors.merge)([{ | ||
const assignSuperThisVisitor = (0, _visitors.environmentVisitor)({ | ||
CallExpression(child, { | ||
@@ -344,3 +361,3 @@ supers, | ||
} | ||
}, _helperEnvironmentVisitor.default]); | ||
}); | ||
function getThisBinding(thisEnvFn, inConstructor) { | ||
@@ -395,3 +412,3 @@ return getBinding(thisEnvFn, "this", thisBinding => { | ||
} | ||
const getScopeInformationVisitor = (0, _visitors.merge)([{ | ||
const getScopeInformationVisitor = (0, _visitors.environmentVisitor)({ | ||
ThisExpression(child, { | ||
@@ -454,3 +471,3 @@ thisPaths | ||
} | ||
}, _helperEnvironmentVisitor.default]); | ||
}); | ||
function getScopeInformation(fnPath) { | ||
@@ -477,3 +494,126 @@ const thisPaths = []; | ||
} | ||
function splitExportDeclaration() { | ||
if (!this.isExportDeclaration() || this.isExportAllDeclaration()) { | ||
throw new Error("Only default and named export declarations can be split."); | ||
} | ||
if (this.isExportNamedDeclaration() && this.get("specifiers").length > 0) { | ||
throw new Error("It doesn't make sense to split exported specifiers."); | ||
} | ||
const declaration = this.get("declaration"); | ||
if (this.isExportDefaultDeclaration()) { | ||
const standaloneDeclaration = declaration.isFunctionDeclaration() || declaration.isClassDeclaration(); | ||
const exportExpr = declaration.isFunctionExpression() || declaration.isClassExpression(); | ||
const scope = declaration.isScope() ? declaration.scope.parent : declaration.scope; | ||
let id = declaration.node.id; | ||
let needBindingRegistration = false; | ||
if (!id) { | ||
needBindingRegistration = true; | ||
id = scope.generateUidIdentifier("default"); | ||
if (standaloneDeclaration || exportExpr) { | ||
declaration.node.id = cloneNode(id); | ||
} | ||
} else if (exportExpr && scope.hasBinding(id.name)) { | ||
needBindingRegistration = true; | ||
id = scope.generateUidIdentifier(id.name); | ||
} | ||
const updatedDeclaration = standaloneDeclaration ? declaration.node : variableDeclaration("var", [variableDeclarator(cloneNode(id), declaration.node)]); | ||
const updatedExportDeclaration = exportNamedDeclaration(null, [exportSpecifier(cloneNode(id), identifier("default"))]); | ||
this.insertAfter(updatedExportDeclaration); | ||
this.replaceWith(updatedDeclaration); | ||
if (needBindingRegistration) { | ||
scope.registerDeclaration(this); | ||
} | ||
return this; | ||
} else if (this.get("specifiers").length > 0) { | ||
throw new Error("It doesn't make sense to split exported specifiers."); | ||
} | ||
const bindingIdentifiers = declaration.getOuterBindingIdentifiers(); | ||
const specifiers = Object.keys(bindingIdentifiers).map(name => { | ||
return exportSpecifier(identifier(name), identifier(name)); | ||
}); | ||
const aliasDeclar = exportNamedDeclaration(null, specifiers); | ||
this.insertAfter(aliasDeclar); | ||
this.replaceWith(declaration.node); | ||
return this; | ||
} | ||
const refersOuterBindingVisitor = { | ||
"ReferencedIdentifier|BindingIdentifier"(path, state) { | ||
if (path.node.name !== state.name) return; | ||
state.needsRename = true; | ||
path.stop(); | ||
}, | ||
Scope(path, state) { | ||
if (path.scope.hasOwnBinding(state.name)) { | ||
path.skip(); | ||
} | ||
} | ||
}; | ||
function ensureFunctionName(supportUnicodeId) { | ||
if (this.node.id) return this; | ||
const res = getFunctionName(this.node, this.parent); | ||
if (res == null) return this; | ||
let { | ||
name | ||
} = res; | ||
if (!supportUnicodeId && /[\uD800-\uDFFF]/.test(name)) { | ||
return null; | ||
} | ||
if (name.startsWith("get ") || name.startsWith("set ")) { | ||
return null; | ||
} | ||
name = toBindingIdentifierName(name.replace(/[/ ]/g, "_")); | ||
const id = identifier(name); | ||
inherits(id, res.originalNode); | ||
const state = { | ||
needsRename: false, | ||
name | ||
}; | ||
const { | ||
scope | ||
} = this; | ||
const binding = scope.getOwnBinding(name); | ||
if (binding) { | ||
if (binding.kind === "param") { | ||
state.needsRename = true; | ||
} else {} | ||
} else if (scope.parent.hasBinding(name) || scope.hasGlobal(name)) { | ||
this.traverse(refersOuterBindingVisitor, state); | ||
} | ||
if (!state.needsRename) { | ||
this.node.id = id; | ||
scope.getProgramParent().references[id.name] = true; | ||
return this; | ||
} | ||
if (scope.hasBinding(id.name) && !scope.hasGlobal(id.name)) { | ||
scope.rename(id.name); | ||
this.node.id = id; | ||
scope.getProgramParent().references[id.name] = true; | ||
return this; | ||
} | ||
if (!isFunction(this.node)) return null; | ||
const key = scope.generateUidIdentifier(id.name); | ||
const params = []; | ||
for (let i = 0, len = getFunctionArity(this.node); i < len; i++) { | ||
params.push(scope.generateUidIdentifier("x")); | ||
} | ||
const call = _template.default.expression.ast` | ||
(function (${key}) { | ||
function ${id}(${params}) { | ||
return ${cloneNode(key)}.apply(this, arguments); | ||
} | ||
${cloneNode(id)}.toString = function () { | ||
return ${cloneNode(key)}.toString(); | ||
} | ||
return ${cloneNode(id)}; | ||
})(${toExpression(this.node)}) | ||
`; | ||
return this.replaceWith(call)[0].get("arguments.0"); | ||
} | ||
function getFunctionArity(node) { | ||
const count = node.params.findIndex(param => isAssignmentPattern(param) || isRestElement(param)); | ||
return count === -1 ? node.params.length : count; | ||
} | ||
//# sourceMappingURL=conversion.js.map |
@@ -11,2 +11,3 @@ "use strict"; | ||
exports.getAllPrevSiblings = getAllPrevSiblings; | ||
exports.getAssignmentIdentifiers = getAssignmentIdentifiers; | ||
exports.getBindingIdentifierPaths = getBindingIdentifierPaths; | ||
@@ -24,2 +25,3 @@ exports.getBindingIdentifiers = getBindingIdentifiers; | ||
const { | ||
getAssignmentIdentifiers: _getAssignmentIdentifiers, | ||
getBindingIdentifiers: _getBindingIdentifiers, | ||
@@ -279,2 +281,5 @@ getOuterBindingIdentifiers: _getOuterBindingIdentifiers, | ||
} | ||
function getAssignmentIdentifiers() { | ||
return _getAssignmentIdentifiers(this.node); | ||
} | ||
function getBindingIdentifiers(duplicates) { | ||
@@ -281,0 +286,0 @@ return _getBindingIdentifiers(this.node, duplicates); |
@@ -181,2 +181,4 @@ "use strict"; | ||
arrowFunctionToExpression: NodePath_conversion.arrowFunctionToExpression, | ||
splitExportDeclaration: NodePath_conversion.splitExportDeclaration, | ||
ensureFunctionName: NodePath_conversion.ensureFunctionName, | ||
matchesPattern: NodePath_introspection.matchesPattern, | ||
@@ -215,2 +217,3 @@ has: NodePath_introspection.has, | ||
requeue: NodePath_context.requeue, | ||
requeueComputedKeyAndDecorators: NodePath_context.requeueComputedKeyAndDecorators, | ||
remove: NodePath_removal.remove, | ||
@@ -231,2 +234,3 @@ insertBefore: NodePath_modification.insertBefore, | ||
get: NodePath_family.get, | ||
getAssignmentIdentifiers: NodePath_family.getAssignmentIdentifiers, | ||
getBindingIdentifiers: NodePath_family.getBindingIdentifiers, | ||
@@ -233,0 +237,0 @@ getOuterBindingIdentifiers: NodePath_family.getOuterBindingIdentifiers, |
@@ -19,3 +19,2 @@ "use strict"; | ||
var _t = require("@babel/types"); | ||
var _helperHoistVariables = require("@babel/helper-hoist-variables"); | ||
const { | ||
@@ -164,8 +163,6 @@ FUNCTION_TYPES, | ||
const callee = this.get("callee"); | ||
(0, _helperHoistVariables.default)(callee.get("body"), id => { | ||
this.scope.push({ | ||
id | ||
}); | ||
}, "var"); | ||
const completionRecords = this.get("callee").getCompletionRecords(); | ||
callee.get("body").scope.hoistVariables(id => this.scope.push({ | ||
id | ||
})); | ||
const completionRecords = callee.getCompletionRecords(); | ||
for (const path of completionRecords) { | ||
@@ -172,0 +169,0 @@ if (!path.isExpressionStatement()) continue; |
@@ -17,2 +17,3 @@ "use strict"; | ||
NOT_LOCAL_BINDING, | ||
assignmentExpression, | ||
callExpression, | ||
@@ -49,2 +50,3 @@ cloneNode, | ||
isVariableDeclaration, | ||
expressionStatement, | ||
matchesPattern, | ||
@@ -63,3 +65,4 @@ memberExpression, | ||
isExportDeclaration, | ||
buildUndefinedNode | ||
buildUndefinedNode, | ||
sequenceExpression | ||
} = _t; | ||
@@ -521,3 +524,3 @@ function gatherNodeParts(node, parts) { | ||
registerConstantViolation(path) { | ||
const ids = path.getBindingIdentifiers(); | ||
const ids = path.getAssignmentIdentifiers(); | ||
for (const name of Object.keys(ids)) { | ||
@@ -708,3 +711,3 @@ var _this$getBinding; | ||
for (const path of state.assignments) { | ||
const ids = path.getBindingIdentifiers(); | ||
const ids = path.getAssignmentIdentifiers(); | ||
for (const name of Object.keys(ids)) { | ||
@@ -910,2 +913,51 @@ if (path.scope.getBinding(name)) continue; | ||
} | ||
hoistVariables(emit = id => this.push({ | ||
id | ||
})) { | ||
this.crawl(); | ||
const seen = new Set(); | ||
for (const name of Object.keys(this.bindings)) { | ||
const binding = this.bindings[name]; | ||
if (!binding) continue; | ||
const { | ||
path | ||
} = binding; | ||
if (!path.isVariableDeclarator()) continue; | ||
const { | ||
parent, | ||
parentPath | ||
} = path; | ||
if (parent.kind !== "var" || seen.has(parent)) continue; | ||
seen.add(path.parent); | ||
let firstId; | ||
const init = []; | ||
for (const decl of parent.declarations) { | ||
var _firstId; | ||
(_firstId = firstId) != null ? _firstId : firstId = decl.id; | ||
if (decl.init) { | ||
init.push(assignmentExpression("=", decl.id, decl.init)); | ||
} | ||
const ids = Object.keys(getBindingIdentifiers(decl, false, true, true)); | ||
for (const name of ids) { | ||
emit(identifier(name), decl.init != null); | ||
} | ||
} | ||
if (parentPath.parentPath.isFor({ | ||
left: parent | ||
})) { | ||
parentPath.replaceWith(firstId); | ||
} else if (init.length === 0) { | ||
parentPath.remove(); | ||
} else { | ||
const expr = init.length === 1 ? init[0] : sequenceExpression(init); | ||
if (parentPath.parentPath.isForStatement({ | ||
init: parent | ||
})) { | ||
parentPath.replaceWith(expr); | ||
} else { | ||
parentPath.replaceWith(expressionStatement(expr)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
@@ -912,0 +964,0 @@ exports.default = Scope; |
@@ -7,5 +7,3 @@ "use strict"; | ||
exports.default = void 0; | ||
var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration"); | ||
var t = require("@babel/types"); | ||
var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor"); | ||
var _traverseNode = require("../../traverse-node.js"); | ||
@@ -25,3 +23,3 @@ var _visitors = require("../../visitors.js"); | ||
if (path.isMethod()) { | ||
(0, _helperEnvironmentVisitor.requeueComputedKeyAndDecorators)(path); | ||
path.requeueComputedKeyAndDecorators(); | ||
} | ||
@@ -47,3 +45,3 @@ } | ||
if (path.isVariableDeclaration()) return; | ||
const ids = path.getOuterBindingIdentifiers(); | ||
const ids = path.isAssignmentExpression() ? path.getAssignmentIdentifiers() : path.getOuterBindingIdentifiers(); | ||
for (const name in ids) { | ||
@@ -76,3 +74,3 @@ if (name === state.oldName) ids[name].name = state.newName; | ||
} | ||
(0, _helperSplitExportDeclaration.default)(maybeExportDeclar); | ||
maybeExportDeclar.splitExportDeclaration(); | ||
} | ||
@@ -79,0 +77,0 @@ maybeConvertFromClassFunctionDeclaration(path) { |
@@ -6,2 +6,3 @@ "use strict"; | ||
}); | ||
exports.environmentVisitor = environmentVisitor; | ||
exports.explode = explode$1; | ||
@@ -221,3 +222,20 @@ exports.isExplodedVisitor = isExplodedVisitor; | ||
} | ||
const _environmentVisitor = { | ||
FunctionParent(path) { | ||
if (path.isArrowFunctionExpression()) return; | ||
path.skip(); | ||
if (path.isMethod()) { | ||
path.requeueComputedKeyAndDecorators(); | ||
} | ||
}, | ||
Property(path) { | ||
if (path.isObjectProperty()) return; | ||
path.skip(); | ||
path.requeueComputedKeyAndDecorators(); | ||
} | ||
}; | ||
function environmentVisitor(visitor) { | ||
return merge([_environmentVisitor, visitor]); | ||
} | ||
//# sourceMappingURL=visitors.js.map |
{ | ||
"name": "@babel/traverse", | ||
"version": "7.24.8", | ||
"version": "7.25.0", | ||
"description": "The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes", | ||
@@ -20,9 +20,6 @@ "author": "The Babel Team (https://babel.dev/team)", | ||
"@babel/code-frame": "^7.24.7", | ||
"@babel/generator": "^7.24.8", | ||
"@babel/helper-environment-visitor": "^7.24.7", | ||
"@babel/helper-function-name": "^7.24.7", | ||
"@babel/helper-hoist-variables": "^7.24.7", | ||
"@babel/helper-split-export-declaration": "^7.24.7", | ||
"@babel/parser": "^7.24.8", | ||
"@babel/types": "^7.24.8", | ||
"@babel/generator": "^7.25.0", | ||
"@babel/parser": "^7.25.0", | ||
"@babel/template": "^7.25.0", | ||
"@babel/types": "^7.25.0", | ||
"debug": "^4.3.1", | ||
@@ -32,3 +29,3 @@ "globals": "^11.1.0" | ||
"devDependencies": { | ||
"@babel/core": "^7.24.8", | ||
"@babel/core": "^7.24.9", | ||
"@babel/helper-plugin-test-runner": "^7.24.7" | ||
@@ -35,0 +32,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
663940
7
5555
+ Added@babel/template@^7.25.0
- Removed@babel/helper-function-name@^7.24.7
- 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-split-export-declaration@7.24.7(transitive)
Updated@babel/generator@^7.25.0
Updated@babel/parser@^7.25.0
Updated@babel/types@^7.25.0