Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1100,4 +1100,81 @@ describe('adjust-static-class-members oxc-transform implementation', () => {
`,
}),
);

it(
'wraps class with Angular static fields and ngDevMode-guarded ɵsetClassMetadata static block',
testCase({
input: `
export class SomeComponent {
static ɵfac = function SomeComponent_Factory(t) { return new (t || SomeComponent)(); };
static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({
type: SomeComponent,
selectors: [['some-cmp']],
decls: 0,
vars: 0,
template: function(rf, ctx) {},
});
static {
(typeof ngDevMode === "undefined" || ngDevMode) &&
i0.ɵsetClassMetadata(SomeComponent, [{ type: Component, args: [{ selector: 'some-cmp', template: '' }] }], null, null);
}
}
`,
expected: `
export let SomeComponent = /*#__PURE__*/ (() => {
class SomeComponent {
static ɵfac = function SomeComponent_Factory(t) { return new (t || SomeComponent)(); };
static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({
type: SomeComponent,
selectors: [['some-cmp']],
decls: 0,
vars: 0,
template: function(rf, ctx) {},
});
}
return SomeComponent;
})();
`,
}),
);

it(
'wraps class with Angular static fields and internal setClassMetadata static block',
testCase({
input: `
class PlatformRef {
static ɵfac = function PlatformRef_Factory(t) { return new (t || PlatformRef)(); };
static ɵprov = /*@__PURE__*/ ɵɵdefineInjectable({ token: PlatformRef, factory: PlatformRef.ɵfac, providedIn: 'platform' });
static {
(typeof ngDevMode === "undefined" || ngDevMode) &&
setClassMetadata(PlatformRef, [{ type: Injectable, args: [{ providedIn: 'platform' }] }], null, null);
}
}
`,
expected: `
let PlatformRef = /*#__PURE__*/ (() => {
class PlatformRef {
static ɵfac = function PlatformRef_Factory(t) { return new (t || PlatformRef)(); };
static ɵprov = /*@__PURE__*/ ɵɵdefineInjectable({ token: PlatformRef, factory: PlatformRef.ɵfac, providedIn: 'platform' });
}
return PlatformRef;
})();
`,
}),
);

it(
'does not wrap class with unguarded non-ɵ setClassMetadata call in static block',
testCase({
input: `
class CustomClass {
static ɵprov = 42;
static {
setClassMetadata(CustomClass);
}
}
`,
expected: NO_CHANGE,
}),
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,111 @@ describe('elide-angular-metadata oxc-transform implementation', () => {
`,
}),
);

it(
'elides ɵsetClassMetadata inside a class static block (ngtsc)',
testCase({
input: `
import { Component } from '@angular/core';
export class SomeClass {
static {
(typeof ngDevMode === "undefined" || ngDevMode) &&
i0.ɵsetClassMetadata(SomeClass, [{ type: Component, args: [{ selector: 'app-lazy', template: '' }] }], null, null);
}
}
`,
expected: `
import { Component } from '@angular/core';
export class SomeClass {}
`,
}),
);

it(
'elides parenthesized ɵsetClassMetadata inside a class static block (ngp)',
testCase({
input: `
import { Component } from '@angular/core';
export class SomeClass {
static {
(((typeof ngDevMode === "undefined") || ngDevMode) &&
i0.ɵsetClassMetadata(SomeClass, [{ type: Component, args: [{ selector: 'app-lazy', template: '' }] }], null, null));
}
}
`,
expected: `
import { Component } from '@angular/core';
export class SomeClass {}
`,
}),
);

it(
'elides ɵsetClassMetadataAsync inside a class static block',
testCase({
input: `
import { Component } from '@angular/core';
export class SomeClass {
static {
(typeof ngDevMode === "undefined" || ngDevMode) &&
i0.ɵsetClassMetadataAsync(
SomeClass,
() => [import("./cmp-a").then((m) => m.CmpA)],
(CmpA) => {
i0.ɵsetClassMetadata(
SomeClass,
[{ type: Component, args: [{ selector: 'test-cmp', imports: [CmpA], template: '' }] }],
null,
null,
);
},
);
}
}
`,
expected: `
import { Component } from '@angular/core';
export class SomeClass {}
`,
}),
);

it(
'elides ngDevMode-guarded internal setClassMetadata inside a class static block (@angular/core FESM)',
testCase({
input: `
export class PlatformRef {
static {
(typeof ngDevMode === "undefined" || ngDevMode) &&
setClassMetadata(PlatformRef, [{ type: Injectable, args: [{ providedIn: 'platform' }] }], null, null);
}
}
`,
expected: `
export class PlatformRef {}
`,
}),
);

it(
'elides ɵsetClassMetadata statement inside a multi-statement static block while preserving side effects',
testCase({
input: `
export class SomeClass {
static {
console.log('keep');
(typeof ngDevMode === "undefined" || ngDevMode) &&
i0.ɵsetClassMetadata(SomeClass, [], null, null);
}
}
`,
expected: `
export class SomeClass {
static {
console.log('keep');
}
}
`,
}),
);
});
Loading