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

Add experimental_useHermesParser option in preset-flow #16284

Merged
merged 4 commits into from Feb 27, 2024
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
3 changes: 2 additions & 1 deletion packages/babel-preset-flow/package.json
Expand Up @@ -29,7 +29,8 @@
},
"devDependencies": {
"@babel/core": "workspace:^",
"@babel/helper-plugin-test-runner": "workspace:^"
"@babel/helper-plugin-test-runner": "workspace:^",
"babel-plugin-syntax-hermes-parser": "^0.19.1"
},
"homepage": "https://babel.dev/docs/en/next/babel-preset-flow",
"bugs": "https://github.com/babel/babel/issues?utf8=%E2%9C%93&q=is%3Aissue+label%3A%22area%3A%20flow%22+is%3Aopen",
Expand Down
23 changes: 20 additions & 3 deletions packages/babel-preset-flow/src/index.ts
Expand Up @@ -12,22 +12,39 @@ export default declarePreset((api, opts) => {
all,
allowDeclareFields,
ignoreExtensions = false,
experimental_useHermesParser: useHermesParser = false,
} = normalizeOptions(opts);

const flowPlugin = [transformFlowStripTypes, { all, allowDeclareFields }];
const plugins: any[] = [
[transformFlowStripTypes, { all, allowDeclareFields }],
];

if (useHermesParser) {
if (Number.parseInt(process.versions.node, 10) < 12) {
throw new Error(
"The Hermes parser is only supported in Node 12 and later.",
);
}
if (IS_STANDALONE) {
throw new Error(
"The Hermes parser is not supported in the @babel/standalone.",
);
}
plugins.unshift("babel-plugin-syntax-hermes-parser");
}

// TODO: In Babel 7, ignoreExtensions is always true.
// Allow setting it to false in the next minor.
if (process.env.BABEL_8_BREAKING ? ignoreExtensions : true) {
return { plugins: [flowPlugin] };
return { plugins };
}

if (process.env.BABEL_8_BREAKING) {
return {
overrides: [
{
test: filename => filename == null || !/\.tsx?$/.test(filename),
plugins: [flowPlugin],
plugins,
},
],
};
Expand Down
14 changes: 12 additions & 2 deletions packages/babel-preset-flow/src/normalize-options.ts
Expand Up @@ -2,7 +2,7 @@ import { OptionValidator } from "@babel/helper-validator-option";
const v = new OptionValidator("@babel/preset-flow");

export default function normalizeOptions(options: any = {}) {
let { all, ignoreExtensions } = options;
let { all, ignoreExtensions, experimental_useHermesParser } = options;
const { allowDeclareFields } = options;

if (process.env.BABEL_8_BREAKING) {
Expand All @@ -13,19 +13,29 @@ export default function normalizeOptions(options: any = {}) {
const TopLevelOptions = {
all: "all",
ignoreExtensions: "ignoreExtensions",
experimental_useHermesParser: "experimental_useHermesParser",
};
v.validateTopLevelOptions(options, TopLevelOptions);
all = v.validateBooleanOption(TopLevelOptions.all, all);
ignoreExtensions = v.validateBooleanOption(
TopLevelOptions.ignoreExtensions,
ignoreExtensions,
);
return { all, ignoreExtensions };
experimental_useHermesParser = v.validateBooleanOption(
TopLevelOptions.experimental_useHermesParser,
experimental_useHermesParser,
);
return {
all,
ignoreExtensions,
experimental_useHermesParser,
};
} else {
return {
all,
allowDeclareFields,
ignoreExtensions,
experimental_useHermesParser,
};
}
}
@@ -0,0 +1,4 @@
// @flow

type TypeofAnimal = Dog extends Animal ? 'animal' : 'unknown';
function foo(a) {}
@@ -0,0 +1,4 @@
{
"presets": [["flow", { "experimental_useHermesParser": true }]],
"minNodeVersion": "12.0.0"
}
@@ -0,0 +1 @@
function foo(a) {}
Expand Up @@ -30,6 +30,7 @@ describe("normalize options", () => {
expect(normalizeOptions({})).toMatchInlineSnapshot(`
Object {
"all": undefined,
"experimental_useHermesParser": undefined,
"ignoreExtensions": undefined,
}
`);
Expand All @@ -50,6 +51,7 @@ describe("normalize options", () => {
Object {
"all": undefined,
"allowDeclareFields": undefined,
"experimental_useHermesParser": undefined,
"ignoreExtensions": undefined,
}
`);
Expand Down
26 changes: 26 additions & 0 deletions yarn.lock
Expand Up @@ -4007,6 +4007,7 @@ __metadata:
"@babel/helper-plugin-utils": "workspace:^"
"@babel/helper-validator-option": "workspace:^"
"@babel/plugin-transform-flow-strip-types": "workspace:^"
babel-plugin-syntax-hermes-parser: "npm:^0.19.1"
peerDependencies:
"@babel/core": ^7.0.0-0
languageName: unknown
Expand Down Expand Up @@ -6883,6 +6884,15 @@ __metadata:
languageName: node
linkType: hard

"babel-plugin-syntax-hermes-parser@npm:^0.19.1":
version: 0.19.1
resolution: "babel-plugin-syntax-hermes-parser@npm:0.19.1"
dependencies:
hermes-parser: "npm:0.19.1"
checksum: cb99e79b2d979cd3a69be3da23748f957d9fab294464f0720602271afb1f4ec40bed71d8884549edf00fa47523c60e9ff2407f4a2a0167eb9ac1dd094c0f8b57
languageName: node
linkType: hard

"babel-plugin-transform-charcodes@npm:^0.2.0":
version: 0.2.0
resolution: "babel-plugin-transform-charcodes@npm:0.2.0"
Expand Down Expand Up @@ -10638,6 +10648,22 @@ __metadata:
languageName: node
linkType: hard

"hermes-estree@npm:0.19.1":
version: 0.19.1
resolution: "hermes-estree@npm:0.19.1"
checksum: dadafea5cf8fcf7d2c2d3d43740898c73b03db4747d4cc83e3cdb06bfcfbf3ee97f4ee26f077aea455771703f5bd18a4cb40c1ce7af9e38ce541d6c03fc8847a
languageName: node
linkType: hard

"hermes-parser@npm:0.19.1":
version: 0.19.1
resolution: "hermes-parser@npm:0.19.1"
dependencies:
hermes-estree: "npm:0.19.1"
checksum: 4fd886ce3ab80c79b258fa60085f2915f587aef57bf59e17f6cfe3b0ad2e7b1a1cfff8371b736392f66cff0658a90ece279b608edcb5589f8c56957e799c56f2
languageName: node
linkType: hard

"hmac-drbg@npm:^1.0.1":
version: 1.0.1
resolution: "hmac-drbg@npm:1.0.1"
Expand Down