[babel 8] type checking preset-flow options (#12751)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
parent
b0d83daceb
commit
16d83002de
@ -21,6 +21,7 @@
|
||||
],
|
||||
"dependencies": {
|
||||
"@babel/helper-plugin-utils": "workspace:^7.12.13",
|
||||
"@babel/helper-validator-option": "workspace:^7.12.11",
|
||||
"@babel/plugin-transform-flow-strip-types": "workspace:^7.12.13"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
import { declare } from "@babel/helper-plugin-utils";
|
||||
import transformFlowStripTypes from "@babel/plugin-transform-flow-strip-types";
|
||||
import normalizeOptions from "./normalize-options";
|
||||
|
||||
export default declare((api, { all, allowDeclareFields }) => {
|
||||
export default declare((api, opts) => {
|
||||
api.assertVersion(7);
|
||||
const { all, allowDeclareFields } = normalizeOptions(opts);
|
||||
|
||||
return {
|
||||
plugins: [[transformFlowStripTypes, { all, allowDeclareFields }]],
|
||||
|
||||
25
packages/babel-preset-flow/src/normalize-options.js
Normal file
25
packages/babel-preset-flow/src/normalize-options.js
Normal file
@ -0,0 +1,25 @@
|
||||
import { OptionValidator } from "@babel/helper-validator-option";
|
||||
const v = new OptionValidator("@babel/preset-flow");
|
||||
|
||||
export default function normalizeOptions(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",
|
||||
};
|
||||
v.validateTopLevelOptions(options, TopLevelOptions);
|
||||
all = v.validateBooleanOption(TopLevelOptions.all, options.all);
|
||||
return { all };
|
||||
} else {
|
||||
return {
|
||||
all,
|
||||
allowDeclareFields,
|
||||
};
|
||||
}
|
||||
}
|
||||
55
packages/babel-preset-flow/test/normalize-options.spec.js
Normal file
55
packages/babel-preset-flow/test/normalize-options.spec.js
Normal file
@ -0,0 +1,55 @@
|
||||
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({ al: true }))
|
||||
.toThrowErrorMatchingInlineSnapshot(`
|
||||
"@babel/preset-flow: 'al' is not a valid top-level option.
|
||||
- Did you mean 'all'?"
|
||||
`);
|
||||
});
|
||||
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,
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
||||
(process.env.BABEL_8_BREAKING ? describe.skip : describe)("Babel 7", () => {
|
||||
it("should not throw on unknown options", () => {
|
||||
expect(() =>
|
||||
normalizeOptions({ allDeclareField: true }),
|
||||
).not.toThrowError();
|
||||
});
|
||||
it.each(["all", "allowDeclareFields"])(
|
||||
"should not throw when `%p` is not a boolean",
|
||||
optionName => {
|
||||
expect(() => normalizeOptions({ [optionName]: 0 })).not.toThrowError();
|
||||
},
|
||||
);
|
||||
it("default values", () => {
|
||||
expect(normalizeOptions({})).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"all": undefined,
|
||||
"allowDeclareFields": undefined,
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -851,7 +851,7 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@babel/helper-validator-option@workspace:^7.12.17, @babel/helper-validator-option@workspace:packages/babel-helper-validator-option":
|
||||
"@babel/helper-validator-option@workspace:^7.12.11, @babel/helper-validator-option@workspace:^7.12.17, @babel/helper-validator-option@workspace:packages/babel-helper-validator-option":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@babel/helper-validator-option@workspace:packages/babel-helper-validator-option"
|
||||
languageName: unknown
|
||||
@ -3169,6 +3169,7 @@ __metadata:
|
||||
"@babel/core": "workspace:*"
|
||||
"@babel/helper-plugin-test-runner": "workspace:*"
|
||||
"@babel/helper-plugin-utils": "workspace:^7.12.13"
|
||||
"@babel/helper-validator-option": "workspace:^7.12.11"
|
||||
"@babel/plugin-transform-flow-strip-types": "workspace:^7.12.13"
|
||||
peerDependencies:
|
||||
"@babel/core": ^7.0.0-0
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user