refactor: use option-validator in preset-typescript (#12347)

This commit is contained in:
Huáng Jùnliàng 2020-11-20 09:11:20 -05:00 committed by GitHub
parent ab5a90ce47
commit 2e3eec6646
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 64 deletions

View File

@ -29,12 +29,6 @@ export default declare((api, opts) => {
const development = !!opts.development;
const useBuiltIns = !!opts.useBuiltIns;
if (typeof development !== "boolean") {
throw new Error(
"@babel/preset-react 'development' option must be a boolean.",
);
}
const transformReactJSXPlugin =
runtime === "automatic" && development
? transformReactJSXDevelopment

View File

@ -18,6 +18,7 @@
],
"dependencies": {
"@babel/helper-plugin-utils": "workspace:^7.10.4",
"@babel/helper-validator-option": "workspace:^7.12.1",
"@babel/plugin-transform-typescript": "workspace:^7.12.1"
},
"peerDependencies": {

View File

@ -1,35 +1,34 @@
import { declare } from "@babel/helper-plugin-utils";
import transformTypeScript from "@babel/plugin-transform-typescript";
import { OptionValidator } from "@babel/helper-validator-option";
const v = new OptionValidator("@babel/preset-typescript");
export default declare(
(
api,
{
allExtensions = false,
export default declare((api, opts) => {
api.assertVersion(7);
const {
allowDeclareFields,
allowNamespaces,
jsxPragma,
jsxPragmaFrag = "React.Fragment",
isTSX = false,
onlyRemoveTypeImports,
},
) => {
api.assertVersion(7);
} = opts;
if (typeof jsxPragmaFrag !== "string") {
throw new Error(".jsxPragmaFrag must be a string, or undefined");
}
const jsxPragmaFrag = v.validateStringOption(
"jsxPragmaFrag",
opts.jsxPragmaFrag,
"React.Fragment",
);
if (typeof allExtensions !== "boolean") {
throw new Error(".allExtensions must be a boolean, or undefined");
}
const allExtensions = v.validateBooleanOption(
"allExtensions",
opts.allExtensions,
false,
);
if (typeof isTSX !== "boolean") {
throw new Error(".isTSX must be a boolean, or undefined");
}
const isTSX = v.validateBooleanOption("isTSX", opts.isTSX, false);
if (isTSX && !allExtensions) {
throw new Error("isTSX:true requires allExtensions:true");
if (isTSX) {
v.invariant(allExtensions, "isTSX:true requires allExtensions:true");
}
const pluginOptions = isTSX => ({
@ -63,5 +62,4 @@ export default declare(
},
],
};
},
);
});

View File

@ -3134,6 +3134,7 @@ __metadata:
"@babel/core": "workspace:*"
"@babel/helper-plugin-test-runner": "workspace:*"
"@babel/helper-plugin-utils": "workspace:^7.10.4"
"@babel/helper-validator-option": "workspace:^7.12.1"
"@babel/plugin-transform-typescript": "workspace:^7.12.1"
peerDependencies:
"@babel/core": ^7.0.0-0