Skip to content

Commit

Permalink
fix(@angular-devkit/build-optimizer): wrap classes which contain empt…
Browse files Browse the repository at this point in the history
…y statements (#16538)

NGCC currently produces empty statments which breaks class wrapping.

Closes #16509
  • Loading branch information
alan-agius4 authored and mgechev committed Jan 4, 2020
1 parent 6f1207f commit a209667
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,15 @@ function findStatements(

for (let index = statementIndex + 1; index < statements.length; ++index) {
const statement = statements[index];

if (!ts.isExpressionStatement(statement)) {
// The below is a workaround for NGCC as TS will never emit an EmptyStatement.
// See: https://github.com/angular/angular-cli/issues/16509#issuecomment-570198398
if (ts.isEmptyStatement(statement)) {
count++;
continue;
}

break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ describe('wrap enums and classes transformer', () => {
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});

it('should wrap classes which contain EmptyStatement', () => {
const input = tags.stripIndent`
let JigsawTrustedHtml = JigsawTrustedHtml_1 = class JigsawTrustedHtml {
constructor(_sanitizer, zone) {
}
static _getContext(magicNumber) {
return JigsawTrustedHtml_1._contexts[magicNumber];
}
};
JigsawTrustedHtml.ɵfac = function JigsawTrustedHtml_Factory(t) { };
// NGCC outputs an empty statement sometimes like the below:
// https://github.com/angular/angular-cli/issues/16509#issuecomment-570198398
JigsawTrustedHtml.ɵdir = ɵngcc0.ɵɵdefineDirective(); ;
JigsawTrustedHtml.ctorParameters = () => [
{ type: DomSanitizer },
{ type: NgZone }
];
`;

const output = tags.stripIndent`
let JigsawTrustedHtml = /*@__PURE__*/ (() => {
${input}
return JigsawTrustedHtml;
})();
`;

expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});

it('should not wrap enum like which are inside of methods', () => {
const input = tags.stripIndent`
class LayoutDirective {
Expand Down

0 comments on commit a209667

Please sign in to comment.