Skip to content

Commit 4cde922

Browse files
alan-agius4mgechev
authored andcommitted
fix(@angular-devkit/build-optimizer): wrap classes which contain empty statements (#16538)
NGCC currently produces empty statments which breaks class wrapping. Closes #16509
1 parent 835100c commit 4cde922

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

packages/angular_devkit/build_optimizer/src/transforms/wrap-enums.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,15 @@ function findStatements(
322322

323323
for (let index = statementIndex + 1; index < statements.length; ++index) {
324324
const statement = statements[index];
325+
325326
if (!ts.isExpressionStatement(statement)) {
327+
// The below is a workaround for NGCC as TS will never emit an EmptyStatement.
328+
// See: https://github.com/angular/angular-cli/issues/16509#issuecomment-570198398
329+
if (ts.isEmptyStatement(statement)) {
330+
count++;
331+
continue;
332+
}
333+
326334
break;
327335
}
328336

packages/angular_devkit/build_optimizer/src/transforms/wrap-enums_spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,36 @@ describe('wrap enums and classes transformer', () => {
8888
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
8989
});
9090

91+
it('should wrap classes which contain EmptyStatement', () => {
92+
const input = tags.stripIndent`
93+
let JigsawTrustedHtml = JigsawTrustedHtml_1 = class JigsawTrustedHtml {
94+
constructor(_sanitizer, zone) {
95+
}
96+
static _getContext(magicNumber) {
97+
return JigsawTrustedHtml_1._contexts[magicNumber];
98+
}
99+
};
100+
JigsawTrustedHtml.ɵfac = function JigsawTrustedHtml_Factory(t) { };
101+
// NGCC outputs an empty statement sometimes like the below:
102+
// https://github.com/angular/angular-cli/issues/16509#issuecomment-570198398
103+
JigsawTrustedHtml.ɵdir = ɵngcc0.ɵɵdefineDirective(); ;
104+
JigsawTrustedHtml.ctorParameters = () => [
105+
{ type: DomSanitizer },
106+
{ type: NgZone }
107+
];
108+
`;
109+
110+
const output = tags.stripIndent`
111+
let JigsawTrustedHtml = /*@__PURE__*/ (() => {
112+
${input}
113+
114+
return JigsawTrustedHtml;
115+
})();
116+
`;
117+
118+
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
119+
});
120+
91121
it('should not wrap enum like which are inside of methods', () => {
92122
const input = tags.stripIndent`
93123
class LayoutDirective {

0 commit comments

Comments
 (0)