From 619c8aed24e7d4ba472d228d7d21ad2b7ab6a738 Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Wed, 2 Sep 2026 03:22:21 -0700 Subject: [PATCH] Detect formatted React.createClass calls (#58256) Summary: The source-optimized preset enables React's display-name transform only when the exact bytes `React.createClass` are present. Valid comments or whitespace around the member access, such as `React /* comment */ . createClass(...)`, bypass the transform and omit the inferred component `displayName`, unlike the full preset path. Use the stable `createClass` identifier token as the cheap source probe while retaining the separate `createReactClass` probe. Formatting no longer changes output; unrelated token matches only enable an otherwise no-op Babel visitor. ## Changelog: [GENERAL] [FIXED] - Detect trivia-separated React.createClass calls in optimized preset configuration. Pull Request resolved: https://github.com/react/react-native/pull/58256 Test Plan: - Added an optimized-source regression with comment/whitespace-separated member access. - Exact baseline omits `displayName`; the fixed transform emits `displayName: "Component"`. - 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: D118266439 Pulled By: javache fbshipit-source-id: 802fe1c8cadda18931384cd5a7f6e88a63e87bad --- .../src/__tests__/transform-snapshot-test.js | 25 +++++++++++++++++++ .../src/configs/main.js | 2 +- 2 files changed, 26 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 1e972039ee5..29ec5148cb0 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 @@ -171,6 +171,21 @@ function transformCode( return result?.code ?? null; } +function transformCodeWithSourceOptimization( + code: string, + options: {[string]: unknown}, +): string | null { + const config = preset.getPreset(code, options); + const result = babel.transformSync(code, { + ...config, + babelrc: false, + configFile: false, + filename: MOCK_FILENAME, + sourceMaps: false, + }); + return result?.code ?? null; +} + function getSnapshotPath(configName: string): string { return path.join(OUTPUT_DIR, `${configName}.js`); } @@ -267,6 +282,16 @@ describe('react-native-babel-preset transform snapshots', () => { ); describe('specific feature transformations', () => { + it('adds display names when React.createClass contains trivia', () => { + const code = ` + const Component = React /* comment */ . createClass({ + render() { return null; } + }); + `; + const result = transformCodeWithSourceOptimization(code, {dev: false}); + expect(result).toContain('displayName:"Component"'); + }); + 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 a48b126ca8b..970853f3c01 100644 --- a/packages/react-native-babel-preset/src/configs/main.js +++ b/packages/react-native-babel-preset/src/configs/main.js @@ -190,7 +190,7 @@ const getPreset = (src, options, babel) => { } if ( isNull || - src.indexOf('React.createClass') !== -1 || + src.indexOf('createClass') !== -1 || src.indexOf('createReactClass') !== -1 ) { extraPlugins.push([require('@babel/plugin-transform-react-display-name')]);