Skip to content

fix(@angular-devkit/build-optimizer): don't wrap enum like nodes whic… #15165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2019
Merged
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 @@ -56,7 +56,7 @@ function visitBlockStatements(
return node;
}
} else {
return ts.visitEachChild(node, visitor, context);
return node;
}
};

Expand Down Expand Up @@ -112,7 +112,7 @@ function visitBlockStatements(
oIndex++;
}

const enumStatements = findStatements(name, statements, oIndex + 1);
const enumStatements = findStatements(name, statements, oIndex, 1);
if (!enumStatements) {
continue;
}
Expand Down Expand Up @@ -316,6 +316,7 @@ function findStatements(
name: string,
statements: ts.NodeArray<ts.Statement>,
statementIndex: number,
offset = 0,
): ts.Statement[] | undefined {
let count = 1;

Expand Down Expand Up @@ -369,7 +370,7 @@ function findStatements(
}

if (count > 1) {
return statements.slice(statementIndex, statementIndex + count);
return statements.slice(statementIndex + offset, statementIndex + count);
}

return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,37 @@ describe('wrap enums and classes transformer', () => {
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 {
constructor(elRef) { }

applyStyleToElement(element, style, value) {
let styles = {};
if (typeof style === 'string') {
styles[style] = value;
style = styles;
}
styles = this.layoutConfig.disableVendorPrefixes ? style : applyCssPrefixes(style);
this._applyMultiValueStyleToElement(styles, element);
}
}
LayoutDirective.ctorParameters = () => [
{ type: ElementRef }
];
`;

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

return LayoutDirective;
})();
`;

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

it('should ClassDeclarations that are referenced with in CallExpressions', () => {
const input = tags.stripIndent`
class ApplicationModule {
Expand Down Expand Up @@ -439,6 +470,22 @@ describe('wrap enums and classes transformer', () => {
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});

it('should not wrap enum like object literal declarations', () => {
const input = tags.stripIndent`
const RendererStyleFlags3 = {
Important: 1,
DashCase: 2,
};
if (typeof RendererStyleFlags3 === 'object') {
RendererStyleFlags3[RendererStyleFlags3.Important] = 'DashCase';
}
RendererStyleFlags3[RendererStyleFlags3.Important] = 'Important';
`;
const output = input;

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

it('wraps ES2015 tsickle enums in IIFE', () => {
const input = tags.stripIndent`
const ChangeDetectionStrategy = {
Expand Down