Skip to content

Commit

Permalink
fix(@ngtools/webpack): better ctor parameters in AOT
Browse files Browse the repository at this point in the history
We missed a few cases that were generating errors. Sorry.

Fixes #4427
  • Loading branch information
hansl committed Feb 4, 2017
1 parent 9fcf10a commit 5357e25
Showing 1 changed file with 47 additions and 30 deletions.
77 changes: 47 additions & 30 deletions packages/@ngtools/webpack/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,35 +61,57 @@ function _angularImportsFromNode(node: ts.ImportDeclaration, sourceFile: ts.Sour
function _ctorParameterFromTypeReference(paramNode: ts.ParameterDeclaration,
angularImports: string[],
refactor: TypeScriptFileRefactor) {
if (paramNode.type.kind == ts.SyntaxKind.TypeReference) {
const type = paramNode.type as ts.TypeReferenceNode;
const decorators = refactor.findAstNodes(paramNode, ts.SyntaxKind.Decorator) as ts.Decorator[];
const decoratorStr = decorators
.map(decorator => {
const fnName =
(refactor.findFirstAstNode(decorator, ts.SyntaxKind.CallExpression) as ts.CallExpression)
.expression.getText(refactor.sourceFile);

if (angularImports.indexOf(fnName) === -1) {
return null;
let typeName = 'undefined';

if (paramNode.type) {
switch (paramNode.type.kind) {
case ts.SyntaxKind.TypeReference:
const type = paramNode.type as ts.TypeReferenceNode;
if (type.typeName) {
typeName = type.typeName.getText(refactor.sourceFile);
} else {
return fnName;
typeName = type.getText(refactor.sourceFile);
}
})
.filter(x => !!x)
.map(name => `{ type: ${name} }`)
.join(', ');

if (type.typeName.kind == ts.SyntaxKind.Identifier) {
const typeName = type.typeName as ts.Identifier;
if (decorators.length > 0) {
return `{ type: ${typeName.text}, decorators: [${decoratorStr}] }`;
}
return `{ type: ${typeName.text} }`;
break;
case ts.SyntaxKind.AnyKeyword:
typeName = 'undefined';
break;
default:
typeName = 'null';
}
}

return 'null';
const decorators = refactor.findAstNodes(paramNode, ts.SyntaxKind.Decorator) as ts.Decorator[];
const decoratorStr = decorators
.map(decorator => {
const call =
refactor.findFirstAstNode(decorator, ts.SyntaxKind.CallExpression) as ts.CallExpression;

if (!call) {
return null;
}

const fnName = call.expression.getText(refactor.sourceFile);
const args = call.arguments.map(x => x.getText(refactor.sourceFile)).join(', ');
if (angularImports.indexOf(fnName) === -1) {
return null;
} else {
return [fnName, args];
}
})
.filter(x => !!x)
.map(([name, args]: string[]) => {
if (args) {
return `{ type: ${name}, args: [${args}] }`;
}
return `{ type: ${name} }`;
})
.join(', ');

if (decorators.length > 0) {
return `{ type: ${typeName}, decorators: [${decoratorStr}] }`;
}
return `{ type: ${typeName} }`;
}


Expand All @@ -106,12 +128,7 @@ function _addCtorParameters(classNode: ts.ClassDeclaration,
}

const params = Array.from(ctor.parameters).map(paramNode => {
switch (paramNode.type.kind) {
case ts.SyntaxKind.TypeReference:
return _ctorParameterFromTypeReference(paramNode, angularImports, refactor);
default:
return 'null';
}
return _ctorParameterFromTypeReference(paramNode, angularImports, refactor);
});

const ctorParametersDecl = `static ctorParameters() { return [ ${params.join(', ')} ]; }`;
Expand Down

0 comments on commit 5357e25

Please sign in to comment.