fix: pass optionLoc when validating plugin object (#10402)

This commit is contained in:
Huáng Jùnliàng 2019-09-11 06:23:20 -04:00 committed by Nicolò Ribaudo
parent 98b14843ba
commit af04f40ee0
3 changed files with 27 additions and 2 deletions

View File

@ -272,7 +272,8 @@ export type OptionsSource =
| "configfile"
| "babelrcfile"
| "extendsfile"
| "preset";
| "preset"
| "plugin";
type RootPath = $ReadOnly<{
type: "root",

View File

@ -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`);
});

View File

@ -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", () => {