Skip to content

Commit

Permalink
Skip plugin for non-Flow JS code (#1556)
Browse files Browse the repository at this point in the history
Summary:

This is a mitigation for #1549.

Updates `babel-plugin-syntax-hermes-parser` to abort when the file contents do not include `flow`.

**Context: React Native**

Originally changed in facebook/react-native#46696, as our internal Flow support had diverged from `babel/plugin-syntax-flow` (facebook/react-native#46601).

We effectively have three flavours of JavaScript in support:
- Flow@latest for the `react-native` package, shipped as source — uses `hermes-parser`.
- TypeScript for product code (community template, Expo) — uses `babel/plugin-syntax-typescript`.
- Plain JavaScript/JSX in product code, *which may be extended with additional user Babel plugins and needs lenient parsing* — uses base `babel/parser` (**this change**).

I'd love to simplify this 😅.

Reviewed By: cipolleschi

Differential Revision: D65272155
  • Loading branch information
huntie authored and facebook-github-bot committed Oct 31, 2024
1 parent acbcf34 commit 9e3c666
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {transformSync} from '@babel/core';
import hermesParserPlugin from '../src';
import * as HermesParser from 'hermes-parser';

const MODULE_PREAMBLE = '"use strict";\n\n';
const MODULE_PREAMBLE = '// @flow\n\n"use strict";\n\n';

describe('babel-plugin-syntax-hermes-parser', () => {
test('test basic parsing', () => {
Expand All @@ -37,7 +37,9 @@ describe('babel-plugin-syntax-hermes-parser', () => {
filename: 'foo.ts',
});
expect(output.code).toMatchInlineSnapshot(`
""use strict";
"// @flow
"use strict";
const a = 1;"
`);
Expand All @@ -54,7 +56,9 @@ describe('babel-plugin-syntax-hermes-parser', () => {
},
});
expect(output.code).toMatchInlineSnapshot(`
""use strict";
"// @flow
"use strict";
function Foo() {}"
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ export default function BabelPluginSyntaxHermesParser(
parserOverride(code: string) {
const filename = curFilename;
if (
filename != null &&
(filename.endsWith('.ts') || filename.endsWith('.tsx'))
!/@flow/.test(code) ||
(filename != null &&
(filename.endsWith('.ts') || filename.endsWith('.tsx')))
) {
return;
}
Expand Down

0 comments on commit 9e3c666

Please sign in to comment.