Skip to content

Commit 5279b1b

Browse files
authored
Transform class static blocks in the Babel preset (#59854)
1 parent d4d2814 commit 5279b1b

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎packages/react-native-babel-preset/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@babel/plugin-transform-async-to-generator": "^7.24.7",
3737
"@babel/plugin-transform-block-scoping": "^7.25.0",
3838
"@babel/plugin-transform-class-properties": "^7.25.4",
39+
"@babel/plugin-transform-class-static-block": "^7.26.0",
3940
"@babel/plugin-transform-classes": "^7.25.4",
4041
"@babel/plugin-transform-destructuring": "^7.24.8",
4142
"@babel/plugin-transform-flow-strip-types": "^7.25.2",

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,84 @@ describe('react-native-babel-preset transform snapshots', () => {
570570
expect(result).toContain('_classPrivateFieldLooseKey');
571571
});
572572

573+
it('transforms class static blocks without unstable_preserveClassPrivate', () => {
574+
const code = `
575+
class Config {
576+
static value = 0;
577+
static {
578+
Config.value = 2;
579+
}
580+
}
581+
`;
582+
const result = transformCode(code, {
583+
dev: false,
584+
unstable_transformProfile: 'hermes-stable',
585+
});
586+
expect(result).not.toContain('static {');
587+
expect(result).toContain('Config.value = 2');
588+
});
589+
590+
it('transforms class static blocks when the profile lowers classes', () => {
591+
const code = `
592+
class Config {
593+
static value = 0;
594+
static {
595+
Config.value = 2;
596+
}
597+
}
598+
`;
599+
const result = transformCode(code, {
600+
dev: false,
601+
unstable_transformProfile: 'hermes-legacy',
602+
customTransformOptions: {
603+
unstable_preserveClassPrivate: true,
604+
},
605+
});
606+
expect(result).not.toContain('static {');
607+
expect(result).toContain('Config.value = 2');
608+
});
609+
610+
it('transforms class static blocks that use private fields and super', () => {
611+
const code = `
612+
class Base {
613+
static base = 1;
614+
}
615+
class Config extends Base {
616+
static #secret = 1;
617+
static value = 0;
618+
static {
619+
Config.value = Config.#secret + super.base;
620+
}
621+
}
622+
`;
623+
const result = transformCode(code, {
624+
dev: false,
625+
unstable_transformProfile: 'hermes-stable',
626+
});
627+
expect(result).not.toContain('static {');
628+
expect(result).not.toContain('#secret');
629+
});
630+
631+
it('preserves class static blocks with unstable_preserveClassPrivate', () => {
632+
const code = `
633+
class Config {
634+
static #secret = 1;
635+
static {
636+
Config.value = Config.#secret;
637+
}
638+
}
639+
`;
640+
const result = transformCode(code, {
641+
dev: false,
642+
unstable_transformProfile: 'hermes-stable',
643+
customTransformOptions: {
644+
unstable_preserveClassPrivate: true,
645+
},
646+
});
647+
expect(result).toContain('static {');
648+
expect(result).toContain('#secret');
649+
});
650+
573651
it('preserves async/await with unstable_preserveAsync', () => {
574652
const code = `
575653
async function fetchData() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ const getPreset = (src, options, babel) => {
282282
...(preserveClassPrivate
283283
? []
284284
: [
285+
[require('@babel/plugin-transform-class-static-block')],
285286
[require('@babel/plugin-transform-private-methods'), {loose}],
286287
[
287288
require('@babel/plugin-transform-private-property-in-object'),

0 commit comments

Comments
 (0)