Skip to content

Commit

Permalink
fix(@angular-devkit/build-optimizer): prefix renamed classes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
filipesilva authored and mgechev committed Jan 18, 2019
1 parent 44a415b commit 07ceb05
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Expand Up @@ -11,7 +11,7 @@ import * as ts from 'typescript';
* @deprecated From 0.9.0
*/
export function testPrefixClasses(content: string) {
const exportVarSetter = /(?:export )?(?:var|const)\s+(\S+)\s*=\s*/;
const exportVarSetter = /(?:export )?(?:var|const)\s+(?:\S+)\s*=\s*/;
const multiLineComment = /\s*(?:\/\*[\s\S]*?\*\/)?\s*/;
const newLine = /\s*\r?\n\s*/;

Expand All @@ -22,7 +22,7 @@ export function testPrefixClasses(content: string) {
/\(/, multiLineComment,
/\s*function \(\) {/, newLine,
multiLineComment,
/function \1\([^\)]*\) \{/, newLine,
/function (?:\S+)\([^\)]*\) \{/, newLine,
],
[
/^/,
Expand Down Expand Up @@ -142,9 +142,6 @@ function isDownleveledClass(node: ts.Node): boolean {
return false;
}

// The variable name should be the class name.
const className = variableDeclaration.name.text;

const firstStatement = functionStatements[0];

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

return ts.isFunctionDeclaration(secondStatement)
&& secondStatement.name !== undefined
&& className.endsWith(secondStatement.name.text)
&& returnStatement.expression.text === secondStatement.name.text;
}
Expand Up @@ -173,6 +173,26 @@ describe('prefix-classes', () => {
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});

it('prefix TS 2.5 - 2.6 renamed downlevel class', () => {
const input = tags.stripIndent`
var ComponentFactoryResolver$1 = /** @class */ (function () {
function ComponentFactoryResolver$$1() {
}
return ComponentFactoryResolver$$1;
}());
`;
const output = tags.stripIndent`
var ComponentFactoryResolver$1 = /** @class */ /*@__PURE__*/ (function () {
function ComponentFactoryResolver$$1() {
}
return ComponentFactoryResolver$$1;
}());
`;

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

it('prefix TS 2.5 - 2.6 downlevel class with static variable', () => {
const input = tags.stripIndent`
var StaticTestCase = /** @class */ (function () {
Expand Down

0 comments on commit 07ceb05

Please sign in to comment.