@babel/helpers
Advanced tools
Comparing version 7.17.8 to 7.17.9
136
lib/index.js
@@ -23,6 +23,4 @@ "use strict"; | ||
expressionStatement, | ||
file: t_file, | ||
identifier, | ||
variableDeclaration, | ||
variableDeclarator | ||
file, | ||
identifier | ||
} = _t; | ||
@@ -41,3 +39,3 @@ | ||
let fileClass = undefined; | ||
let FileClass = undefined; | ||
@@ -73,10 +71,7 @@ function getHelperMetadata(file) { | ||
if (decl.isFunctionDeclaration()) { | ||
if (!decl.node.id) { | ||
throw decl.buildCodeFrameError("Helpers should give names to their exported func declaration"); | ||
} | ||
exportName = decl.node.id.name; | ||
if (!decl.isFunctionDeclaration() || !decl.node.id) { | ||
throw decl.buildCodeFrameError("Helpers can only export named function declarations"); | ||
} | ||
exportName = decl.node.id.name; | ||
exportPath = makePath(child); | ||
@@ -138,3 +133,3 @@ }, | ||
(0, _traverse.default)(file.ast, referenceVisitor, file.scope); | ||
if (!exportPath) throw new Error("Helpers must default-export something."); | ||
if (!exportPath) throw new Error("Helpers must have a default export."); | ||
exportBindingAssignments.reverse(); | ||
@@ -186,46 +181,33 @@ return { | ||
const visitor = { | ||
Program(path) { | ||
const exp = path.get(exportPath); | ||
const imps = importPaths.map(p => path.get(p)); | ||
const impsBindingRefs = importBindingsReferences.map(p => path.get(p)); | ||
const decl = exp.get("declaration"); | ||
const { | ||
path | ||
} = file; | ||
const exp = path.get(exportPath); | ||
const imps = importPaths.map(p => path.get(p)); | ||
const impsBindingRefs = importBindingsReferences.map(p => path.get(p)); | ||
const decl = exp.get("declaration"); | ||
if (id.type === "Identifier") { | ||
if (decl.isFunctionDeclaration()) { | ||
exp.replaceWith(decl); | ||
} else { | ||
exp.replaceWith(variableDeclaration("var", [variableDeclarator(id, decl.node)])); | ||
} | ||
} else if (id.type === "MemberExpression") { | ||
if (decl.isFunctionDeclaration()) { | ||
exportBindingAssignments.forEach(assignPath => { | ||
const assign = path.get(assignPath); | ||
assign.replaceWith(assignmentExpression("=", id, assign.node)); | ||
}); | ||
exp.replaceWith(decl); | ||
path.pushContainer("body", expressionStatement(assignmentExpression("=", id, identifier(exportName)))); | ||
} else { | ||
exp.replaceWith(expressionStatement(assignmentExpression("=", id, decl.node))); | ||
} | ||
} else { | ||
throw new Error("Unexpected helper format."); | ||
} | ||
if (id.type === "Identifier") { | ||
exp.replaceWith(decl); | ||
} else if (id.type === "MemberExpression") { | ||
exportBindingAssignments.forEach(assignPath => { | ||
const assign = path.get(assignPath); | ||
assign.replaceWith(assignmentExpression("=", id, assign.node)); | ||
}); | ||
exp.replaceWith(decl); | ||
path.pushContainer("body", expressionStatement(assignmentExpression("=", id, identifier(exportName)))); | ||
} else { | ||
throw new Error("Unexpected helper format."); | ||
} | ||
Object.keys(toRename).forEach(name => { | ||
path.scope.rename(name, toRename[name]); | ||
}); | ||
Object.keys(toRename).forEach(name => { | ||
path.scope.rename(name, toRename[name]); | ||
}); | ||
for (const path of imps) path.remove(); | ||
for (const path of imps) path.remove(); | ||
for (const path of impsBindingRefs) { | ||
const node = cloneNode(dependenciesRefs[path.node.name]); | ||
path.replaceWith(node); | ||
} | ||
path.stop(); | ||
} | ||
}; | ||
(0, _traverse.default)(file.ast, visitor, file.scope); | ||
for (const path of impsBindingRefs) { | ||
const node = cloneNode(dependenciesRefs[path.node.name]); | ||
path.replaceWith(node); | ||
} | ||
} | ||
@@ -247,19 +229,30 @@ | ||
const fn = () => { | ||
const file = { | ||
ast: t_file(helper.ast()) | ||
}; | ||
if (fileClass) { | ||
return new fileClass({ | ||
filename: `babel-helper://${name}` | ||
}, file); | ||
{ | ||
if (!FileClass) { | ||
const fakeFile = { | ||
ast: file(helper.ast()), | ||
path: null | ||
}; | ||
(0, _traverse.default)(fakeFile.ast, { | ||
Program: path => (fakeFile.path = path).stop() | ||
}); | ||
return fakeFile; | ||
} | ||
} | ||
return file; | ||
return new FileClass({ | ||
filename: `babel-helper://${name}` | ||
}, { | ||
ast: file(helper.ast()), | ||
code: "[internal Babel helper code]", | ||
inputMap: null | ||
}); | ||
}; | ||
const metadata = getHelperMetadata(fn()); | ||
let metadata = null; | ||
helperData[name] = { | ||
minVersion: helper.minVersion, | ||
build(getDependency, id, localBindings) { | ||
const file = fn(); | ||
metadata || (metadata = getHelperMetadata(file)); | ||
permuteHelperAST(file, metadata, id, localBindings, getDependency); | ||
@@ -272,7 +265,7 @@ return { | ||
minVersion() { | ||
return helper.minVersion; | ||
}, | ||
getDependencies() { | ||
metadata || (metadata = getHelperMetadata(fn())); | ||
return Array.from(metadata.dependencies.values()); | ||
} | ||
dependencies: metadata.dependencies | ||
}; | ||
@@ -289,14 +282,11 @@ } | ||
function minVersion(name) { | ||
return loadHelper(name).minVersion(); | ||
return loadHelper(name).minVersion; | ||
} | ||
function getDependencies(name) { | ||
return Array.from(loadHelper(name).dependencies.values()); | ||
return loadHelper(name).getDependencies(); | ||
} | ||
function ensure(name, newFileClass) { | ||
if (!fileClass) { | ||
fileClass = newFileClass; | ||
} | ||
FileClass || (FileClass = newFileClass); | ||
loadHelper(name); | ||
@@ -303,0 +293,0 @@ } |
{ | ||
"name": "@babel/helpers", | ||
"version": "7.17.8", | ||
"version": "7.17.9", | ||
"description": "Collection of helper functions used by Babel transforms.", | ||
@@ -19,3 +19,3 @@ "author": "The Babel Team (https://babel.dev/team)", | ||
"@babel/template": "^7.16.7", | ||
"@babel/traverse": "^7.17.3", | ||
"@babel/traverse": "^7.17.9", | ||
"@babel/types": "^7.17.0" | ||
@@ -22,0 +22,0 @@ }, |
109710
2782
Updated@babel/traverse@^7.17.9