From a3b71c2c61f62cdb99261ec5fef06b879a5205a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 6 Sep 2019 12:27:44 -0400 Subject: [PATCH] fix: pass optionLoc when validating plugin object --- .../babel-core/src/config/validation/options.js | 3 ++- .../babel-core/src/config/validation/plugins.js | 11 ++++++++++- packages/babel-core/test/config-loading.js | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/babel-core/src/config/validation/options.js b/packages/babel-core/src/config/validation/options.js index 3ee072ae344f..02c51649a4cb 100644 --- a/packages/babel-core/src/config/validation/options.js +++ b/packages/babel-core/src/config/validation/options.js @@ -272,7 +272,8 @@ export type OptionsSource = | "configfile" | "babelrcfile" | "extendsfile" - | "preset"; + | "preset" + | "plugin"; type RootPath = $ReadOnly<{ type: "root", diff --git a/packages/babel-core/src/config/validation/plugins.js b/packages/babel-core/src/config/validation/plugins.js index 7800de965e31..ea1fc514306d 100644 --- a/packages/babel-core/src/config/validation/plugins.js +++ b/packages/babel-core/src/config/validation/plugins.js @@ -84,10 +84,19 @@ export type PluginObject = { }; export function validatePluginObject(obj: {}): PluginObject { + const rootPath: RootPath = { + type: "root", + source: "plugin", + }; Object.keys(obj).forEach(key => { const validator = VALIDATORS[key]; + const optLoc = { + type: "option", + name: key, + parent: rootPath, + }; - if (validator) validator(key, obj[key]); + if (validator) validator(optLoc, obj[key]); else throw new Error(`.${key} is not a valid Plugin property`); }); diff --git a/packages/babel-core/test/config-loading.js b/packages/babel-core/test/config-loading.js index b31eac8ed2d9..69e786930f8e 100644 --- a/packages/babel-core/test/config-loading.js +++ b/packages/babel-core/test/config-loading.js @@ -322,6 +322,21 @@ describe("@babel/core config loading", () => { } } }); + + it("should thrown when plugin is not valid", () => { + const fooPlugin = { + inherits: "inhertis-should-not-be-string", + }; + const opts = { + cwd: path.dirname(FILEPATH), + filename: FILEPATH, + plugins: [fooPlugin], + }; + + expect(() => loadConfig(opts)).toThrow( + /\.inhertis must be a function, or undefined/, + ); + }); }); describe("caller metadata", () => {