Skip to content

Commit fa65766

Browse files
robhoganmeta-codesync[bot]
authored andcommitted
Babel preset: Add unstable_preserveClassPrivate to experiment with disabling private class transforms for SH (#55880)
Summary: Pull Request resolved: #55880 Disable `babel/plugin-transform-private-methods` and `babel/plugin-transform-private-property-in-object` when `customTransformOptions.unstable_preserveClassPrivate` is truthy. This allows us to experiment with native private class field and method support in Static Hermes. Changelog: [Internal] Reviewed By: vzaidman, javache Differential Revision: D93010263 fbshipit-source-id: 25c48c8561b7c9b4f8d9984e47c5ca8b14007360
1 parent 1132229 commit fa65766

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,5 +352,41 @@ describe('react-native-babel-preset transform snapshots', () => {
352352
expect(result).toContain('import');
353353
expect(result).toContain('export');
354354
});
355+
356+
it('preserves private class fields with unstable_preserveClassPrivate', () => {
357+
const code = `
358+
class Counter {
359+
#count = 0;
360+
#privateMethod() { return this.#count; }
361+
increment() { this.#count++; }
362+
}
363+
`;
364+
const result = transformCode(code, {
365+
dev: false,
366+
unstable_transformProfile: 'hermes-stable',
367+
customTransformOptions: {
368+
unstable_preserveClassPrivate: true,
369+
},
370+
});
371+
expect(result).toContain('#count');
372+
expect(result).toContain('#privateMethod');
373+
});
374+
375+
it('transforms private class fields without unstable_preserveClassPrivate', () => {
376+
const code = `
377+
class Counter {
378+
#count = 0;
379+
#privateMethod() { return this.#count; }
380+
increment() { this.#count++; }
381+
}
382+
`;
383+
const result = transformCode(code, {
384+
dev: false,
385+
unstable_transformProfile: 'hermes-stable',
386+
});
387+
expect(result).not.toContain('#count');
388+
expect(result).not.toContain('#privateMethod');
389+
expect(result).toContain('_classPrivateFieldLooseKey');
390+
});
355391
});
356392
});

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const EXCLUDED_FIRST_PARTY_PATHS = [
2121
/[/\\]private[/\\]react-native-fantom[/\\]/,
2222
];
2323

24+
// customTransformOptions may be strings from URL params, or booleans passed
25+
// programatically. For strings, handle them as Metro does when parsing URLs.
26+
const TRUE_VALS = new Set([true, 'true', '1']);
27+
2428
function isTypeScriptSource(fileName) {
2529
return !!fileName && fileName.endsWith('.ts');
2630
}
@@ -73,6 +77,11 @@ const getPreset = (src, options, babel) => {
7377
// Preserve class syntax and related if we're using Hermes V1.
7478
const preserveClasses = isHermesV1;
7579

80+
// Preserve private class fields and methods if the experiment is enabled.
81+
const preserveClassPrivate = TRUE_VALS.has(
82+
options?.customTransformOptions?.unstable_preserveClassPrivate,
83+
);
84+
7685
const isNull = src == null;
7786
const hasClass = isNull || src.indexOf('class') !== -1;
7887

@@ -217,11 +226,15 @@ const getPreset = (src, options, babel) => {
217226
...(preserveClasses
218227
? []
219228
: [[require('@babel/plugin-transform-class-properties'), {loose}]]),
220-
[require('@babel/plugin-transform-private-methods'), {loose}],
221-
[
222-
require('@babel/plugin-transform-private-property-in-object'),
223-
{loose},
224-
],
229+
...(preserveClassPrivate
230+
? []
231+
: [
232+
[require('@babel/plugin-transform-private-methods'), {loose}],
233+
[
234+
require('@babel/plugin-transform-private-property-in-object'),
235+
{loose},
236+
],
237+
]),
225238
[require('@babel/plugin-syntax-dynamic-import')],
226239
[require('@babel/plugin-syntax-export-default-from')],
227240
...passthroughSyntaxPlugins,

0 commit comments

Comments
 (0)