Skip to content

Commit

Permalink
always throw when add missing helpers (#10208)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau authored and existentialism committed Jul 12, 2019
1 parent a9660cf commit f5ca058
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/babel-core/src/transformation/file/file.js
Expand Up @@ -203,6 +203,9 @@ export default class File {
if (res) return res;
}

// make sure that the helper exists
helpers.ensure(name);

const uid = (this.declarations[name] = this.scope.generateUidIdentifier(
name,
));
Expand Down
24 changes: 24 additions & 0 deletions packages/babel-core/test/api.js
Expand Up @@ -788,4 +788,28 @@ describe("api", function() {
);
});
});

describe("missing helpers", function() {
it("should always throw", function() {
expect(() =>
babel.transformSync(``, {
configFile: false,
plugins: [
function() {
return {
visitor: {
Program(path) {
try {
path.pushContainer("body", this.addHelper("fooBar"));
} catch {}
path.pushContainer("body", this.addHelper("fooBar"));
},
},
};
},
],
}),
).toThrow();
});
});
});
4 changes: 4 additions & 0 deletions packages/babel-helpers/src/index.js
Expand Up @@ -280,6 +280,10 @@ export function getDependencies(name: string): $ReadOnlyArray<string> {
return Array.from(loadHelper(name).dependencies.values());
}

export function ensure(name: string) {
loadHelper(name);
}

export const list = Object.keys(helpers)
.map(name => name.replace(/^_/, ""))
.filter(name => name !== "__esModule");
Expand Down

0 comments on commit f5ca058

Please sign in to comment.