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 😅.

Differential Revision: D65272155
  • Loading branch information
huntie authored and facebook-github-bot committed Nov 1, 2024
1 parent e3aec0f commit 62a3c4d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,31 @@ 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';
const NON_FLOW_MODULE_PREAMBLE = '"use strict";\n\n';

describe('babel-plugin-syntax-hermes-parser', () => {
test('test basic parsing', () => {
const parseSpy = jest.spyOn(HermesParser, 'parse');
const code = MODULE_PREAMBLE + 'const a = 1;';
const parseSpy = jest.spyOn(HermesParser, 'parse');

afterEach(() => {
parseSpy.mockReset();
});

test('should parse Flow files', () => {
const code = MODULE_PREAMBLE + 'const a: number = 1;';
const output = transformSync(code, {
plugins: [hermesParserPlugin],
});
expect(output.code).toBe(code);
expect(output.code).toMatchInlineSnapshot(`
""use strict";
const a = 1;"
`);
expect(parseSpy).toBeCalledTimes(1);
});

test('test skip TS', () => {
const parseSpy = jest.spyOn(HermesParser, 'parse');
const code = MODULE_PREAMBLE + 'const a: string = 1;';
test('should skip TypeScript files', () => {
const code = NON_FLOW_MODULE_PREAMBLE + 'const a: number = 1;';
const output = transformSync(code, {
plugins: [hermesParserPlugin],
filename: 'foo.ts',
Expand All @@ -44,8 +53,32 @@ describe('babel-plugin-syntax-hermes-parser', () => {
expect(parseSpy).toBeCalledTimes(0);
});

test('test component syntax parsing', () => {
const parseSpy = jest.spyOn(HermesParser, 'parse');
test('should skip JS files without @flow annotation', () => {
const code = NON_FLOW_MODULE_PREAMBLE + 'class Foo {}';
const output = transformSync(code, {
plugins: [hermesParserPlugin],
});
expect(output.code).toBe(code);
expect(parseSpy).toBeCalledTimes(0);
});

test("should parse all JS files when parserOpts.flow = 'all'", () => {
const code = NON_FLOW_MODULE_PREAMBLE + 'const a: number = 1;';
const output = transformSync(code, {
plugins: [hermesParserPlugin],
parserOpts: {
flow: 'all',
},
});
expect(output.code).toMatchInlineSnapshot(`
""use strict";
const a = 1;"
`);
expect(parseSpy).toBeCalledTimes(1);
});

test('should parse component syntax when enabled', () => {
const code = MODULE_PREAMBLE + 'component Foo() {}';
const output = transformSync(code, {
plugins: [hermesParserPlugin],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ export default function BabelPluginSyntaxHermesParser(
) {
return;
}

const opts: ParserOptions = {};
for (const [key, value] of Object.entries(curParserOpts)) {
if (HermesParser.ParserOptionsKeys.has(key)) {
// $FlowExpectedError[incompatible-type]
opts[key] = value;
}
}

if (opts.flow !== 'all' && !/@flow/.test(code)) {
return;
}

return HermesParser.parse(code, {...opts, babel: true});
},

Expand Down

0 comments on commit 62a3c4d

Please sign in to comment.