From 0d64c9a13a9a87593a497131ae87abc92810a2bb Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Fri, 4 Sep 2026 06:09:06 -0700 Subject: [PATCH] Run codegen for formatted native component calls (#58257) Summary: The source-optimized preset enables native component codegen only when `codegenNativeComponent` is immediately followed by `<`. Valid Flow syntax permits comments or whitespace before the type arguments, so `codegenNativeComponent /* comment */ (...)` bypasses codegen and emits an ordinary runtime call instead of the generated `__INTERNAL_VIEW_CONFIG` produced by the full preset path. Use the stable `codegenNativeComponent` identifier token as the cheap source prefilter while retaining the existing null/undefined full-scan path. Formatting no longer changes build output; false-positive tokens only enable the codegen visitor, which remains a no-op without a matching call. ## Changelog: [GENERAL] [FIXED] - Run native component codegen when Flow type arguments are separated by trivia. Pull Request resolved: https://github.com/react/react-native/pull/58257 Test Plan: - Added a valid Flow native-component spec containing a comment before ``. - Exact optimized baseline emits the ordinary call without `__INTERNAL_VIEW_CONFIG`; the fixed transform generates the static view config. - Full preset Jest passes: 4/4 suites, 111/111 tests, 16 snapshots. - Fresh Flow check reports 0 errors. - Targeted no-ignore ESLint, Prettier, and `git diff --check` pass. No public API or breaking behavior change; optimized and full preset paths now agree for valid formatting. Reviewed By: GijsWeterings Differential Revision: D118266389 Pulled By: javache fbshipit-source-id: 0b4279eed9f831e1e8f4669bd522645023dfdeac --- .../src/__tests__/transform-snapshot-test.js | 27 +++++++++++++++++++ .../src/configs/main.js | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js b/packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js index aacde536959..fb50cec1c5f 100644 --- a/packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js +++ b/packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js @@ -313,6 +313,33 @@ describe('react-native-babel-preset transform snapshots', () => { expect(result).toContain('displayName:"Component"'); }); + it('runs codegen when the type arguments are separated by trivia', () => { + const code = ` + // @flow strict-local + import type {ViewProps} from 'react-native'; + import type {HostComponent} from 'react-native'; + import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent'; + + type NativeProps = Readonly<{ + ...ViewProps, + }>; + + export default codegenNativeComponent /* comment */ ( + 'View', + ) as HostComponent; + `; + const config = preset.getPreset(code, {}); + const result = babel.transformSync(code, { + ...config, + babelrc: false, + configFile: false, + filename: MOCK_FILENAME, + sourceMaps: false, + }); + + expect(result?.code).toContain('__INTERNAL_VIEW_CONFIG'); + }); + it('handles async generators', () => { const code = ` async function* gen() { diff --git a/packages/react-native-babel-preset/src/configs/main.js b/packages/react-native-babel-preset/src/configs/main.js index 51451d80433..45fc88e21c9 100644 --- a/packages/react-native-babel-preset/src/configs/main.js +++ b/packages/react-native-babel-preset/src/configs/main.js @@ -146,7 +146,7 @@ const getPreset = (src, options, babel) => { if ( !options.disableStaticViewConfigsCodegen && - (src === null || /\bcodegenNativeComponent