Skip to content

Commit

Permalink
fix: pass optionLoc when validating plugin object (#10402)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung authored and nicolo-ribaudo committed Sep 11, 2019
1 parent 98b1484 commit af04f40
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/babel-core/src/config/validation/options.js
Expand Up @@ -272,7 +272,8 @@ export type OptionsSource =
| "configfile"
| "babelrcfile"
| "extendsfile"
| "preset";
| "preset"
| "plugin";

type RootPath = $ReadOnly<{
type: "root",
Expand Down
11 changes: 10 additions & 1 deletion packages/babel-core/src/config/validation/plugins.js
Expand Up @@ -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`);
});

Expand Down
15 changes: 15 additions & 0 deletions packages/babel-core/test/config-loading.js
Expand Up @@ -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(
/\.inherits must be a function, or undefined/,
);
});
});

describe("caller metadata", () => {
Expand Down

0 comments on commit af04f40

Please sign in to comment.