Skip to content

Commit f5ca058

Browse files
tanhauhauexistentialism
authored andcommitted
always throw when add missing helpers (#10208)
1 parent a9660cf commit f5ca058

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

packages/babel-core/src/transformation/file/file.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ export default class File {
203203
if (res) return res;
204204
}
205205

206+
// make sure that the helper exists
207+
helpers.ensure(name);
208+
206209
const uid = (this.declarations[name] = this.scope.generateUidIdentifier(
207210
name,
208211
));

packages/babel-core/test/api.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,4 +788,28 @@ describe("api", function() {
788788
);
789789
});
790790
});
791+
792+
describe("missing helpers", function() {
793+
it("should always throw", function() {
794+
expect(() =>
795+
babel.transformSync(``, {
796+
configFile: false,
797+
plugins: [
798+
function() {
799+
return {
800+
visitor: {
801+
Program(path) {
802+
try {
803+
path.pushContainer("body", this.addHelper("fooBar"));
804+
} catch {}
805+
path.pushContainer("body", this.addHelper("fooBar"));
806+
},
807+
},
808+
};
809+
},
810+
],
811+
}),
812+
).toThrow();
813+
});
814+
});
791815
});

packages/babel-helpers/src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ export function getDependencies(name: string): $ReadOnlyArray<string> {
280280
return Array.from(loadHelper(name).dependencies.values());
281281
}
282282

283+
export function ensure(name: string) {
284+
loadHelper(name);
285+
}
286+
283287
export const list = Object.keys(helpers)
284288
.map(name => name.replace(/^_/, ""))
285289
.filter(name => name !== "__esModule");

0 commit comments

Comments
 (0)