Skip to content

Commit

Permalink
Make errors for bugs in helpers more helpful (and not "this.hub is un…
Browse files Browse the repository at this point in the history
…defined")
  • Loading branch information
nicolo-ribaudo committed May 15, 2024
1 parent 557da44 commit dac7164
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions packages/babel-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface HelperMetadata {
* Given a file AST for a given helper, get a bunch of metadata about it so that Babel can quickly render
* the helper is whatever context it is needed in.
*/
function getHelperMetadata(file: File): HelperMetadata {
function getHelperMetadata(file: File, helperName: string): HelperMetadata {
const globals = new Set<string>();
const localBindingNames = new Set<string>();
// Maps imported identifier -> helper name
Expand All @@ -55,14 +55,14 @@ function getHelperMetadata(file: File): HelperMetadata {
ImportDeclaration(child) {
const name = child.node.source.value;
if (!helpers[name]) {
throw child.buildCodeFrameError(`Unknown helper ${name}`);
throw new Error(`Unknown helper ${name} (in ${helperName})`);
}
if (
child.get("specifiers").length !== 1 ||
!child.get("specifiers.0").isImportDefaultSpecifier()
) {
throw child.buildCodeFrameError(
"Helpers can only import a default value",
throw new Error(
`Helpers can only import a default value (in ${helperName})`,
);
}
const bindingIdentifier = child.node.specifiers[0].local;
Expand All @@ -73,19 +73,19 @@ function getHelperMetadata(file: File): HelperMetadata {
const decl = child.get("declaration");

if (!decl.isFunctionDeclaration() || !decl.node.id) {
throw decl.buildCodeFrameError(
"Helpers can only export named function declarations",
throw new Error(
`Helpers can only export named function declarations (in ${helperName})`,
);
}

exportName = decl.node.id.name;
exportPath = makePath(child);
},
ExportAllDeclaration(child) {
throw child.buildCodeFrameError("Helpers can only export default");
ExportAllDeclaration() {
throw new Error(`Helpers can only export default (in ${helperName})`);
},
ExportNamedDeclaration(child) {
throw child.buildCodeFrameError("Helpers can only export default");
ExportNamedDeclaration() {
throw new Error(`Helpers can only export default (in ${helperName})`);
},
Statement(child) {
if (child.isImportDeclaration() || child.isExportDeclaration()) return;
Expand Down Expand Up @@ -122,8 +122,8 @@ function getHelperMetadata(file: File): HelperMetadata {
if (!(exportName in left.getBindingIdentifiers())) return;

if (!left.isIdentifier()) {
throw left.buildCodeFrameError(
"Only simple assignments to exports are allowed in helpers",
throw new Error(
`Only simple assignments to exports are allowed in helpers (in ${helperName})`,
);
}

Expand Down Expand Up @@ -300,7 +300,7 @@ function loadHelper(name: string) {
minVersion: helper.minVersion,
build(getDependency, id, localBindings) {
const file = fn();
metadata ||= getHelperMetadata(file);
metadata ||= getHelperMetadata(file, name);
permuteHelperAST(file, metadata, id, localBindings, getDependency);

return {
Expand All @@ -309,7 +309,7 @@ function loadHelper(name: string) {
};
},
getDependencies() {
metadata ||= getHelperMetadata(fn());
metadata ||= getHelperMetadata(fn(), name);
return Array.from(metadata.dependencies.values());
},
};
Expand Down

0 comments on commit dac7164

Please sign in to comment.