mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
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: https://github.com/react/react-native/pull/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
This commit is contained in:
1 parent
63d118af72
commit
0d64c9a13a
2 files changed
+28
-1
No files matched your search
@@ -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 */ <NativeProps>(
|
||||
'View',
|
||||
) as HostComponent<NativeProps>;
|
||||
`;
|
||||
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() {
|
||||
|
||||
+1
-1
@@ -146,7 +146,7 @@ const getPreset = (src, options, babel) => {
|
||||
|
||||
if (
|
||||
!options.disableStaticViewConfigsCodegen &&
|
||||
(src === null || /\bcodegenNativeComponent</.test(src))
|
||||
(isNull || src.indexOf('codegenNativeComponent') !== -1)
|
||||
) {
|
||||
extraPlugins.push([require('@react-native/babel-plugin-codegen')]);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user