Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 0 additions & 6 deletions packages/babel-preset-react/src/index.js
Expand Up @@ -29,12 +29,6 @@ export default declare((api, opts) => {
const development = !!opts.development;
const useBuiltIns = !!opts.useBuiltIns;

if (typeof development !== "boolean") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch is unreachable, we have cast development to boolean by double negation above.

throw new Error(
"@babel/preset-react 'development' option must be a boolean.",
);
}

const transformReactJSXPlugin =
runtime === "automatic" && development
? transformReactJSXDevelopment
Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-typescript/package.json
Expand Up @@ -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": {
Expand Down
114 changes: 56 additions & 58 deletions packages/babel-preset-typescript/src/index.js
@@ -1,67 +1,65 @@
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,
allowDeclareFields,
allowNamespaces,
jsxPragma,
jsxPragmaFrag = "React.Fragment",
isTSX = false,
onlyRemoveTypeImports,
},
) => {
api.assertVersion(7);
export default declare((api, opts) => {
api.assertVersion(7);

if (typeof jsxPragmaFrag !== "string") {
throw new Error(".jsxPragmaFrag must be a string, or undefined");
}
const {
allowDeclareFields,
allowNamespaces,
jsxPragma,
onlyRemoveTypeImports,
} = opts;

if (typeof allExtensions !== "boolean") {
throw new Error(".allExtensions must be a boolean, or undefined");
}
const jsxPragmaFrag = v.validateStringOption(
"jsxPragmaFrag",
opts.jsxPragmaFrag,
"React.Fragment",
);

if (typeof isTSX !== "boolean") {
throw new Error(".isTSX must be a boolean, or undefined");
}
const allExtensions = v.validateBooleanOption(
"allExtensions",
opts.allExtensions,
false,
);

if (isTSX && !allExtensions) {
throw new Error("isTSX:true requires allExtensions:true");
}
const isTSX = v.validateBooleanOption("isTSX", opts.isTSX, false);

const pluginOptions = isTSX => ({
allowDeclareFields,
allowNamespaces,
isTSX,
jsxPragma,
jsxPragmaFrag,
onlyRemoveTypeImports,
});
if (isTSX) {
v.invariant(allExtensions, "isTSX:true requires allExtensions:true");
}

return {
overrides: allExtensions
? [
{
plugins: [[transformTypeScript, pluginOptions(isTSX)]],
},
]
: [
{
// Only set 'test' if explicitly requested, since it requires that
// Babel is being called`
test: /\.ts$/,
plugins: [[transformTypeScript, pluginOptions(false)]],
},
{
// Only set 'test' if explicitly requested, since it requires that
// Babel is being called`
test: /\.tsx$/,
plugins: [[transformTypeScript, pluginOptions(true)]],
},
],
};
},
);
const pluginOptions = isTSX => ({
allowDeclareFields,
allowNamespaces,
isTSX,
jsxPragma,
jsxPragmaFrag,
onlyRemoveTypeImports,
});

return {
overrides: allExtensions
? [
{
plugins: [[transformTypeScript, pluginOptions(isTSX)]],
},
]
: [
{
// Only set 'test' if explicitly requested, since it requires that
// Babel is being called`
test: /\.ts$/,
plugins: [[transformTypeScript, pluginOptions(false)]],
},
{
// Only set 'test' if explicitly requested, since it requires that
// Babel is being called`
test: /\.tsx$/,
plugins: [[transformTypeScript, pluginOptions(true)]],
},
],
};
});
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -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
Expand Down