Skip to content

Commit

Permalink
[babel 8] type checking preset-flow options (#12751)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
JLHwung and nicolo-ribaudo committed Mar 25, 2021
1 parent b0d83da commit 16d8300
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/babel-preset-flow/package.json
Expand Up @@ -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": {
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-preset-flow/src/index.js
@@ -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 }]],
Expand Down
25 changes: 25 additions & 0 deletions packages/babel-preset-flow/src/normalize-options.js
@@ -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 changes: 55 additions & 0 deletions packages/babel-preset-flow/test/normalize-options.spec.js
@@ -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,
}
`);
});
});
});
3 changes: 2 additions & 1 deletion yarn.lock
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 16d8300

Please sign in to comment.