Skip to content

Commit 0d64c9a

Browse files
OskarEichlermeta-codesync[bot]
authored andcommitted
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 */ <NativeProps>(...)` 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: #58257 Test Plan: - Added a valid Flow native-component spec containing a comment before `<NativeProps>`. - 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
1 parent 63d118a commit 0d64c9a

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

packages/react-native-babel-preset/src/__tests__/transform-snapshot-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,33 @@ describe('react-native-babel-preset transform snapshots', () => {
313313
expect(result).toContain('displayName:"Component"');
314314
});
315315

316+
it('runs codegen when the type arguments are separated by trivia', () => {
317+
const code = `
318+
// @flow strict-local
319+
import type {ViewProps} from 'react-native';
320+
import type {HostComponent} from 'react-native';
321+
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
322+
323+
type NativeProps = Readonly<{
324+
...ViewProps,
325+
}>;
326+
327+
export default codegenNativeComponent /* comment */ <NativeProps>(
328+
'View',
329+
) as HostComponent<NativeProps>;
330+
`;
331+
const config = preset.getPreset(code, {});
332+
const result = babel.transformSync(code, {
333+
...config,
334+
babelrc: false,
335+
configFile: false,
336+
filename: MOCK_FILENAME,
337+
sourceMaps: false,
338+
});
339+
340+
expect(result?.code).toContain('__INTERNAL_VIEW_CONFIG');
341+
});
342+
316343
it('handles async generators', () => {
317344
const code = `
318345
async function* gen() {

packages/react-native-babel-preset/src/configs/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ const getPreset = (src, options, babel) => {
146146

147147
if (
148148
!options.disableStaticViewConfigsCodegen &&
149-
(src === null || /\bcodegenNativeComponent</.test(src))
149+
(isNull || src.indexOf('codegenNativeComponent') !== -1)
150150
) {
151151
extraPlugins.push([require('@react-native/babel-plugin-codegen')]);
152152
}

0 commit comments

Comments
 (0)