Skip to content

Commit 07ceb05

Browse files
filipesilvamgechev
authored andcommitted
fix(@angular-devkit/build-optimizer): prefix renamed classes
Module concatenation may rename classes with the same name. The renaming logic is specific to the bundler so we can't really foresee it. But the fact remains that the inner function declaration doesn't need to have the same name as the outer one.
1 parent 44a415b commit 07ceb05

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

packages/angular_devkit/build_optimizer/src/transforms/prefix-classes.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as ts from 'typescript';
1111
* @deprecated From 0.9.0
1212
*/
1313
export function testPrefixClasses(content: string) {
14-
const exportVarSetter = /(?:export )?(?:var|const)\s+(\S+)\s*=\s*/;
14+
const exportVarSetter = /(?:export )?(?:var|const)\s+(?:\S+)\s*=\s*/;
1515
const multiLineComment = /\s*(?:\/\*[\s\S]*?\*\/)?\s*/;
1616
const newLine = /\s*\r?\n\s*/;
1717

@@ -22,7 +22,7 @@ export function testPrefixClasses(content: string) {
2222
/\(/, multiLineComment,
2323
/\s*function \(\) {/, newLine,
2424
multiLineComment,
25-
/function \1\([^\)]*\) \{/, newLine,
25+
/function (?:\S+)\([^\)]*\) \{/, newLine,
2626
],
2727
[
2828
/^/,
@@ -142,9 +142,6 @@ function isDownleveledClass(node: ts.Node): boolean {
142142
return false;
143143
}
144144

145-
// The variable name should be the class name.
146-
const className = variableDeclaration.name.text;
147-
148145
const firstStatement = functionStatements[0];
149146

150147
// find return statement - may not be last statement
@@ -166,7 +163,6 @@ function isDownleveledClass(node: ts.Node): boolean {
166163
// potential non-extended class or wrapped es2015 class
167164
return (ts.isFunctionDeclaration(firstStatement) || ts.isClassDeclaration(firstStatement))
168165
&& firstStatement.name !== undefined
169-
&& firstStatement.name.text === className
170166
&& returnStatement.expression.text === firstStatement.name.text;
171167
} else if (functionExpression.parameters.length !== 1) {
172168
return false;
@@ -216,6 +212,5 @@ function isDownleveledClass(node: ts.Node): boolean {
216212

217213
return ts.isFunctionDeclaration(secondStatement)
218214
&& secondStatement.name !== undefined
219-
&& className.endsWith(secondStatement.name.text)
220215
&& returnStatement.expression.text === secondStatement.name.text;
221216
}

packages/angular_devkit/build_optimizer/src/transforms/prefix-classes_spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ describe('prefix-classes', () => {
173173
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
174174
});
175175

176+
it('prefix TS 2.5 - 2.6 renamed downlevel class', () => {
177+
const input = tags.stripIndent`
178+
var ComponentFactoryResolver$1 = /** @class */ (function () {
179+
function ComponentFactoryResolver$$1() {
180+
}
181+
return ComponentFactoryResolver$$1;
182+
}());
183+
`;
184+
const output = tags.stripIndent`
185+
var ComponentFactoryResolver$1 = /** @class */ /*@__PURE__*/ (function () {
186+
function ComponentFactoryResolver$$1() {
187+
}
188+
return ComponentFactoryResolver$$1;
189+
}());
190+
`;
191+
192+
expect(testPrefixClasses(input)).toBeTruthy();
193+
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
194+
});
195+
176196
it('prefix TS 2.5 - 2.6 downlevel class with static variable', () => {
177197
const input = tags.stripIndent`
178198
var StaticTestCase = /** @class */ (function () {

0 commit comments

Comments
 (0)