From 9e3c6665e68c4641b0e5e7bb1f6e3ee3f214828b Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Thu, 31 Oct 2024 07:34:01 -0700 Subject: [PATCH] Skip plugin for non-Flow JS code (#1556) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: This is a mitigation for https://github.com/facebook/hermes/issues/1549. Updates `babel-plugin-syntax-hermes-parser` to abort when the file contents do not include `flow`. **Context: React Native** Originally changed in https://github.com/facebook/react-native/pull/46696, as our internal Flow support had diverged from `babel/plugin-syntax-flow` (https://github.com/facebook/react-native/issues/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 --- .../babel-plugin-syntax-hermes-parser-test.js | 10 +++++++--- .../js/babel-plugin-syntax-hermes-parser/src/index.js | 5 +++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/tools/hermes-parser/js/babel-plugin-syntax-hermes-parser/__tests__/babel-plugin-syntax-hermes-parser-test.js b/tools/hermes-parser/js/babel-plugin-syntax-hermes-parser/__tests__/babel-plugin-syntax-hermes-parser-test.js index a03108c2b20..7e2367b1d88 100644 --- a/tools/hermes-parser/js/babel-plugin-syntax-hermes-parser/__tests__/babel-plugin-syntax-hermes-parser-test.js +++ b/tools/hermes-parser/js/babel-plugin-syntax-hermes-parser/__tests__/babel-plugin-syntax-hermes-parser-test.js @@ -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', () => { @@ -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;" `); @@ -54,7 +56,9 @@ describe('babel-plugin-syntax-hermes-parser', () => { }, }); expect(output.code).toMatchInlineSnapshot(` - ""use strict"; + "// @flow + + "use strict"; function Foo() {}" `); diff --git a/tools/hermes-parser/js/babel-plugin-syntax-hermes-parser/src/index.js b/tools/hermes-parser/js/babel-plugin-syntax-hermes-parser/src/index.js index a04237fb031..ac4730cfd13 100644 --- a/tools/hermes-parser/js/babel-plugin-syntax-hermes-parser/src/index.js +++ b/tools/hermes-parser/js/babel-plugin-syntax-hermes-parser/src/index.js @@ -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; }