Skip to content

Commit 619c8ae

Browse files
OskarEichlermeta-codesync[bot]
authored andcommitted
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: #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
1 parent 1b40443 commit 619c8ae

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,21 @@ function transformCode(
171171
return result?.code ?? null;
172172
}
173173

174+
function transformCodeWithSourceOptimization(
175+
code: string,
176+
options: {[string]: unknown},
177+
): string | null {
178+
const config = preset.getPreset(code, options);
179+
const result = babel.transformSync(code, {
180+
...config,
181+
babelrc: false,
182+
configFile: false,
183+
filename: MOCK_FILENAME,
184+
sourceMaps: false,
185+
});
186+
return result?.code ?? null;
187+
}
188+
174189
function getSnapshotPath(configName: string): string {
175190
return path.join(OUTPUT_DIR, `${configName}.js`);
176191
}
@@ -267,6 +282,16 @@ describe('react-native-babel-preset transform snapshots', () => {
267282
);
268283

269284
describe('specific feature transformations', () => {
285+
it('adds display names when React.createClass contains trivia', () => {
286+
const code = `
287+
const Component = React /* comment */ . createClass({
288+
render() { return null; }
289+
});
290+
`;
291+
const result = transformCodeWithSourceOptimization(code, {dev: false});
292+
expect(result).toContain('displayName:"Component"');
293+
});
294+
270295
it('handles async generators', () => {
271296
const code = `
272297
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
@@ -190,7 +190,7 @@ const getPreset = (src, options, babel) => {
190190
}
191191
if (
192192
isNull ||
193-
src.indexOf('React.createClass') !== -1 ||
193+
src.indexOf('createClass') !== -1 ||
194194
src.indexOf('createReactClass') !== -1
195195
) {
196196
extraPlugins.push([require('@babel/plugin-transform-react-display-name')]);

0 commit comments

Comments
 (0)