Skip to content

Commit 3880c87

Browse files
committed
test(@angular/build): add failing specs for static-block Angular metadata elision
Add specs verifying that oxc-transform elides ɵsetClassMetadata, ɵsetClassMetadataAsync, and ngDevMode-guarded internal setClassMetadata calls inside ES2022 class static blocks while allowing pure static class wrapping.
1 parent 090bc99 commit 3880c87

2 files changed

Lines changed: 184 additions & 0 deletions

File tree

‎packages/angular/build/src/tools/oxc/adjust-static-class-members_oxc_spec.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,4 +1100,81 @@ describe('adjust-static-class-members oxc-transform implementation', () => {
11001100
`,
11011101
}),
11021102
);
1103+
1104+
it(
1105+
'wraps class with Angular static fields and ngDevMode-guarded ɵsetClassMetadata static block',
1106+
testCase({
1107+
input: `
1108+
export class SomeComponent {
1109+
static ɵfac = function SomeComponent_Factory(t) { return new (t || SomeComponent)(); };
1110+
static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({
1111+
type: SomeComponent,
1112+
selectors: [['some-cmp']],
1113+
decls: 0,
1114+
vars: 0,
1115+
template: function(rf, ctx) {},
1116+
});
1117+
static {
1118+
(typeof ngDevMode === "undefined" || ngDevMode) &&
1119+
i0.ɵsetClassMetadata(SomeComponent, [{ type: Component, args: [{ selector: 'some-cmp', template: '' }] }], null, null);
1120+
}
1121+
}
1122+
`,
1123+
expected: `
1124+
export let SomeComponent = /*#__PURE__*/ (() => {
1125+
class SomeComponent {
1126+
static ɵfac = function SomeComponent_Factory(t) { return new (t || SomeComponent)(); };
1127+
static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({
1128+
type: SomeComponent,
1129+
selectors: [['some-cmp']],
1130+
decls: 0,
1131+
vars: 0,
1132+
template: function(rf, ctx) {},
1133+
});
1134+
}
1135+
return SomeComponent;
1136+
})();
1137+
`,
1138+
}),
1139+
);
1140+
1141+
it(
1142+
'wraps class with Angular static fields and internal setClassMetadata static block',
1143+
testCase({
1144+
input: `
1145+
class PlatformRef {
1146+
static ɵfac = function PlatformRef_Factory(t) { return new (t || PlatformRef)(); };
1147+
static ɵprov = /*@__PURE__*/ ɵɵdefineInjectable({ token: PlatformRef, factory: PlatformRef.ɵfac, providedIn: 'platform' });
1148+
static {
1149+
(typeof ngDevMode === "undefined" || ngDevMode) &&
1150+
setClassMetadata(PlatformRef, [{ type: Injectable, args: [{ providedIn: 'platform' }] }], null, null);
1151+
}
1152+
}
1153+
`,
1154+
expected: `
1155+
let PlatformRef = /*#__PURE__*/ (() => {
1156+
class PlatformRef {
1157+
static ɵfac = function PlatformRef_Factory(t) { return new (t || PlatformRef)(); };
1158+
static ɵprov = /*@__PURE__*/ ɵɵdefineInjectable({ token: PlatformRef, factory: PlatformRef.ɵfac, providedIn: 'platform' });
1159+
}
1160+
return PlatformRef;
1161+
})();
1162+
`,
1163+
}),
1164+
);
1165+
1166+
it(
1167+
'does not wrap class with unguarded non-ɵ setClassMetadata call in static block',
1168+
testCase({
1169+
input: `
1170+
class CustomClass {
1171+
static ɵprov = 42;
1172+
static {
1173+
setClassMetadata(CustomClass);
1174+
}
1175+
}
1176+
`,
1177+
expected: NO_CHANGE,
1178+
}),
1179+
);
11031180
});

‎packages/angular/build/src/tools/oxc/elide-angular-metadata_oxc_spec.ts‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,111 @@ describe('elide-angular-metadata oxc-transform implementation', () => {
210210
`,
211211
}),
212212
);
213+
214+
it(
215+
'elides ɵsetClassMetadata inside a class static block (ngtsc)',
216+
testCase({
217+
input: `
218+
import { Component } from '@angular/core';
219+
export class SomeClass {
220+
static {
221+
(typeof ngDevMode === "undefined" || ngDevMode) &&
222+
i0.ɵsetClassMetadata(SomeClass, [{ type: Component, args: [{ selector: 'app-lazy', template: '' }] }], null, null);
223+
}
224+
}
225+
`,
226+
expected: `
227+
import { Component } from '@angular/core';
228+
export class SomeClass {}
229+
`,
230+
}),
231+
);
232+
233+
it(
234+
'elides parenthesized ɵsetClassMetadata inside a class static block (ngp)',
235+
testCase({
236+
input: `
237+
import { Component } from '@angular/core';
238+
export class SomeClass {
239+
static {
240+
(((typeof ngDevMode === "undefined") || ngDevMode) &&
241+
i0.ɵsetClassMetadata(SomeClass, [{ type: Component, args: [{ selector: 'app-lazy', template: '' }] }], null, null));
242+
}
243+
}
244+
`,
245+
expected: `
246+
import { Component } from '@angular/core';
247+
export class SomeClass {}
248+
`,
249+
}),
250+
);
251+
252+
it(
253+
'elides ɵsetClassMetadataAsync inside a class static block',
254+
testCase({
255+
input: `
256+
import { Component } from '@angular/core';
257+
export class SomeClass {
258+
static {
259+
(typeof ngDevMode === "undefined" || ngDevMode) &&
260+
i0.ɵsetClassMetadataAsync(
261+
SomeClass,
262+
() => [import("./cmp-a").then((m) => m.CmpA)],
263+
(CmpA) => {
264+
i0.ɵsetClassMetadata(
265+
SomeClass,
266+
[{ type: Component, args: [{ selector: 'test-cmp', imports: [CmpA], template: '' }] }],
267+
null,
268+
null,
269+
);
270+
},
271+
);
272+
}
273+
}
274+
`,
275+
expected: `
276+
import { Component } from '@angular/core';
277+
export class SomeClass {}
278+
`,
279+
}),
280+
);
281+
282+
it(
283+
'elides ngDevMode-guarded internal setClassMetadata inside a class static block (@angular/core FESM)',
284+
testCase({
285+
input: `
286+
export class PlatformRef {
287+
static {
288+
(typeof ngDevMode === "undefined" || ngDevMode) &&
289+
setClassMetadata(PlatformRef, [{ type: Injectable, args: [{ providedIn: 'platform' }] }], null, null);
290+
}
291+
}
292+
`,
293+
expected: `
294+
export class PlatformRef {}
295+
`,
296+
}),
297+
);
298+
299+
it(
300+
'elides ɵsetClassMetadata statement inside a multi-statement static block while preserving side effects',
301+
testCase({
302+
input: `
303+
export class SomeClass {
304+
static {
305+
console.log('keep');
306+
(typeof ngDevMode === "undefined" || ngDevMode) &&
307+
i0.ɵsetClassMetadata(SomeClass, [], null, null);
308+
}
309+
}
310+
`,
311+
expected: `
312+
export class SomeClass {
313+
static {
314+
console.log('keep');
315+
}
316+
}
317+
`,
318+
}),
319+
);
213320
});

0 commit comments

Comments
 (0)