From 16d83002defc74dcbf0d73d8b71e9646acf04f16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 25 Mar 2021 19:33:02 -0400 Subject: [PATCH] [babel 8] type checking preset-flow options (#12751) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolò Ribaudo --- packages/babel-preset-flow/package.json | 1 + packages/babel-preset-flow/src/index.js | 4 +- .../src/normalize-options.js | 25 +++++++++ .../test/normalize-options.spec.js | 55 +++++++++++++++++++ yarn.lock | 3 +- 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 packages/babel-preset-flow/src/normalize-options.js create mode 100644 packages/babel-preset-flow/test/normalize-options.spec.js diff --git a/packages/babel-preset-flow/package.json b/packages/babel-preset-flow/package.json index 3b05f6e86c..fb61dea3b5 100644 --- a/packages/babel-preset-flow/package.json +++ b/packages/babel-preset-flow/package.json @@ -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": { diff --git a/packages/babel-preset-flow/src/index.js b/packages/babel-preset-flow/src/index.js index da02e7e6c0..523b2b063d 100644 --- a/packages/babel-preset-flow/src/index.js +++ b/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 }]], diff --git a/packages/babel-preset-flow/src/normalize-options.js b/packages/babel-preset-flow/src/normalize-options.js new file mode 100644 index 0000000000..9d79c4395b --- /dev/null +++ b/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, + }; + } +} diff --git a/packages/babel-preset-flow/test/normalize-options.spec.js b/packages/babel-preset-flow/test/normalize-options.spec.js new file mode 100644 index 0000000000..055a745970 --- /dev/null +++ b/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, + } + `); + }); + }); +}); diff --git a/yarn.lock b/yarn.lock index 7101fd75f3..27913ffafd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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