From 45f7f3bdf7abdcdbae851692bafe0170a8d085f7 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sat, 13 Jul 2019 00:06:06 +0800 Subject: [PATCH] always throw when add missing helpers --- .../src/transformation/file/file.js | 3 +++ packages/babel-core/test/api.js | 24 +++++++++++++++++++ packages/babel-helpers/src/index.js | 4 ++++ 3 files changed, 31 insertions(+) diff --git a/packages/babel-core/src/transformation/file/file.js b/packages/babel-core/src/transformation/file/file.js index b6bdda846977..a0a9f65bcdf4 100644 --- a/packages/babel-core/src/transformation/file/file.js +++ b/packages/babel-core/src/transformation/file/file.js @@ -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, )); diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 0121d919f108..9f2f955f1d7e 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -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(); + }); + }); }); diff --git a/packages/babel-helpers/src/index.js b/packages/babel-helpers/src/index.js index 86465941a063..9dc87ce58916 100644 --- a/packages/babel-helpers/src/index.js +++ b/packages/babel-helpers/src/index.js @@ -280,6 +280,10 @@ export function getDependencies(name: string): $ReadOnlyArray { 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");