Skip to content

Commit 6679863

Browse files
committed
Detect formatted React.createClass calls
1 parent 2fa055f commit 6679863

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)