Skip to content

Commit ab80209

Browse files
alan-agius4vikerman
authored andcommitted
fix(@angular-devkit/build-optimizer): don't wrap enum like nodes which are inside of methods.
With this change we stop recursive lookup when the current node is not a BlockLike. This change should also, improve the BO overall speed as it's reduces a lot of recursive lookups. Fixes #15145
1 parent b959883 commit ab80209

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function visitBlockStatements(
5656
return node;
5757
}
5858
} else {
59-
return ts.visitEachChild(node, visitor, context);
59+
return node;
6060
}
6161
};
6262

@@ -112,7 +112,7 @@ function visitBlockStatements(
112112
oIndex++;
113113
}
114114

115-
const enumStatements = findStatements(name, statements, oIndex + 1);
115+
const enumStatements = findStatements(name, statements, oIndex, 1);
116116
if (!enumStatements) {
117117
continue;
118118
}
@@ -316,6 +316,7 @@ function findStatements(
316316
name: string,
317317
statements: ts.NodeArray<ts.Statement>,
318318
statementIndex: number,
319+
offset = 0,
319320
): ts.Statement[] | undefined {
320321
let count = 1;
321322

@@ -369,7 +370,7 @@ function findStatements(
369370
}
370371

371372
if (count > 1) {
372-
return statements.slice(statementIndex, statementIndex + count);
373+
return statements.slice(statementIndex + offset, statementIndex + count);
373374
}
374375

375376
return undefined;

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

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

91+
it('should not wrap enum like which are inside of methods', () => {
92+
const input = tags.stripIndent`
93+
class LayoutDirective {
94+
constructor(elRef) { }
95+
96+
applyStyleToElement(element, style, value) {
97+
let styles = {};
98+
if (typeof style === 'string') {
99+
styles[style] = value;
100+
style = styles;
101+
}
102+
styles = this.layoutConfig.disableVendorPrefixes ? style : applyCssPrefixes(style);
103+
this._applyMultiValueStyleToElement(styles, element);
104+
}
105+
}
106+
LayoutDirective.ctorParameters = () => [
107+
{ type: ElementRef }
108+
];
109+
`;
110+
111+
const output = tags.stripIndent`
112+
let LayoutDirective = /*@__PURE__*/ (() => {
113+
${input}
114+
115+
return LayoutDirective;
116+
})();
117+
`;
118+
119+
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
120+
});
121+
91122
it('should ClassDeclarations that are referenced with in CallExpressions', () => {
92123
const input = tags.stripIndent`
93124
class ApplicationModule {
@@ -439,6 +470,22 @@ describe('wrap enums and classes transformer', () => {
439470
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
440471
});
441472

473+
it('should not wrap enum like object literal declarations', () => {
474+
const input = tags.stripIndent`
475+
const RendererStyleFlags3 = {
476+
Important: 1,
477+
DashCase: 2,
478+
};
479+
if (typeof RendererStyleFlags3 === 'object') {
480+
RendererStyleFlags3[RendererStyleFlags3.Important] = 'DashCase';
481+
}
482+
RendererStyleFlags3[RendererStyleFlags3.Important] = 'Important';
483+
`;
484+
const output = input;
485+
486+
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
487+
});
488+
442489
it('wraps ES2015 tsickle enums in IIFE', () => {
443490
const input = tags.stripIndent`
444491
const ChangeDetectionStrategy = {

0 commit comments

Comments
 (0)