[babel 8] type checking preset-flow options (#12751)

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Huáng Jùnliàng 2021-03-25 19:33:02 -04:00 committed by GitHub
parent b0d83daceb
commit 16d83002de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 2 deletions

View File

@ -21,6 +21,7 @@
], ],
"dependencies": { "dependencies": {
"@babel/helper-plugin-utils": "workspace:^7.12.13", "@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" "@babel/plugin-transform-flow-strip-types": "workspace:^7.12.13"
}, },
"peerDependencies": { "peerDependencies": {

View File

@ -1,8 +1,10 @@
import { declare } from "@babel/helper-plugin-utils"; import { declare } from "@babel/helper-plugin-utils";
import transformFlowStripTypes from "@babel/plugin-transform-flow-strip-types"; 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); api.assertVersion(7);
const { all, allowDeclareFields } = normalizeOptions(opts);
return { return {
plugins: [[transformFlowStripTypes, { all, allowDeclareFields }]], plugins: [[transformFlowStripTypes, { all, allowDeclareFields }]],

View 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,
};
}
}

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

View File

@ -851,7 +851,7 @@ __metadata:
languageName: node languageName: node
linkType: hard 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 version: 0.0.0-use.local
resolution: "@babel/helper-validator-option@workspace:packages/babel-helper-validator-option" resolution: "@babel/helper-validator-option@workspace:packages/babel-helper-validator-option"
languageName: unknown languageName: unknown
@ -3169,6 +3169,7 @@ __metadata:
"@babel/core": "workspace:*" "@babel/core": "workspace:*"
"@babel/helper-plugin-test-runner": "workspace:*" "@babel/helper-plugin-test-runner": "workspace:*"
"@babel/helper-plugin-utils": "workspace:^7.12.13" "@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" "@babel/plugin-transform-flow-strip-types": "workspace:^7.12.13"
peerDependencies: peerDependencies:
"@babel/core": ^7.0.0-0 "@babel/core": ^7.0.0-0