Skip to content

Commit

Permalink
throw on allowDeclareFields
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Feb 3, 2021
1 parent 7f056b5 commit ff5d3a6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
13 changes: 7 additions & 6 deletions packages/babel-preset-flow/src/normalize-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import { OptionValidator } from "@babel/helper-validator-option";
const v = new OptionValidator("@babel/preset-flow");

export default function normalizeOptions(options = {}) {
let { all, allowDeclareFields } = options;
let { all } = options;
const { allowDeclareFields } = options;

if (process.env.BABEL_8_BREAKING) {
v.invariant(
!("allowDeclareFields" in options),
`Since Babel 8, \`declare property: A\` is always supported, and the "allowDeclareFields" option is no longer available. Please remove it from your config.`,
);
const TopLevelOptions = {
all: "all",
allowDeclareFields: "allowDeclareFields",
};
v.validateTopLevelOptions(options, TopLevelOptions);
all = v.validateBooleanOption(TopLevelOptions.all, options.all);
allowDeclareFields = v.validateBooleanOption(
TopLevelOptions.allowDeclareFields,
options.allowDeclareFields,
);
return { all };
}

return {
Expand Down
27 changes: 15 additions & 12 deletions packages/babel-preset-flow/test/normalize-options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@ import normalizeOptions from "../src/normalize-options";
describe("normalize options", () => {
(process.env.BABEL_8_BREAKING ? describe : describe.skip)("Babel 8", () => {
it("should throw on unknown options", () => {
expect(() => normalizeOptions({ allowDeclareField: true }))
expect(() => normalizeOptions({ al: true }))
.toThrowErrorMatchingInlineSnapshot(`
"@babel/preset-flow: 'allowDeclareField' is not a valid top-level option.
- Did you mean 'allowDeclareFields'?"
"@babel/preset-flow: 'al' is not a valid top-level option.
- Did you mean 'all'?"
`);
});
it.each(["all", "allowDeclareFields"])(
"should throw when `%p` is not a boolean",
optionName => {
expect(() => normalizeOptions({ [optionName]: 0 })).toThrow(
`@babel/preset-flow: '${optionName}' option must be a boolean.`,
);
},
);
it("should throw on Babel 7 `allowDeclareFields` option", () => {
expect(() =>
normalizeOptions({ allowDeclareFields: true }),
).toThrowErrorMatchingInlineSnapshot(
`"@babel/preset-flow: Since Babel 8, \`declare property: A\` is always supported, and the \\"allowDeclareFields\\" option is no longer available. Please remove it from your config."`,
);
});
it.each(["all"])("should throw when `%p` is not a boolean", optionName => {
expect(() => normalizeOptions({ [optionName]: 0 })).toThrow(
`@babel/preset-flow: '${optionName}' option must be a boolean.`,
);
});
it("should not throw when options is not defined", () => {
expect(() => normalizeOptions()).not.toThrowError();
});
it("default values", () => {
expect(normalizeOptions({})).toMatchInlineSnapshot(`
Object {
"all": undefined,
"allowDeclareFields": undefined,
}
`);
});
Expand Down

0 comments on commit ff5d3a6

Please sign in to comment.