Skip to content

Commit 5357e25

Browse files
committed
fix(@ngtools/webpack): better ctor parameters in AOT
We missed a few cases that were generating errors. Sorry. Fixes angular#4427
1 parent 9fcf10a commit 5357e25

File tree

1 file changed

+47
-30
lines changed

1 file changed

+47
-30
lines changed

packages/@ngtools/webpack/src/loader.ts

+47-30
Original file line numberDiff line numberDiff line change
@@ -61,35 +61,57 @@ function _angularImportsFromNode(node: ts.ImportDeclaration, sourceFile: ts.Sour
6161
function _ctorParameterFromTypeReference(paramNode: ts.ParameterDeclaration,
6262
angularImports: string[],
6363
refactor: TypeScriptFileRefactor) {
64-
if (paramNode.type.kind == ts.SyntaxKind.TypeReference) {
65-
const type = paramNode.type as ts.TypeReferenceNode;
66-
const decorators = refactor.findAstNodes(paramNode, ts.SyntaxKind.Decorator) as ts.Decorator[];
67-
const decoratorStr = decorators
68-
.map(decorator => {
69-
const fnName =
70-
(refactor.findFirstAstNode(decorator, ts.SyntaxKind.CallExpression) as ts.CallExpression)
71-
.expression.getText(refactor.sourceFile);
72-
73-
if (angularImports.indexOf(fnName) === -1) {
74-
return null;
64+
let typeName = 'undefined';
65+
66+
if (paramNode.type) {
67+
switch (paramNode.type.kind) {
68+
case ts.SyntaxKind.TypeReference:
69+
const type = paramNode.type as ts.TypeReferenceNode;
70+
if (type.typeName) {
71+
typeName = type.typeName.getText(refactor.sourceFile);
7572
} else {
76-
return fnName;
73+
typeName = type.getText(refactor.sourceFile);
7774
}
78-
})
79-
.filter(x => !!x)
80-
.map(name => `{ type: ${name} }`)
81-
.join(', ');
82-
83-
if (type.typeName.kind == ts.SyntaxKind.Identifier) {
84-
const typeName = type.typeName as ts.Identifier;
85-
if (decorators.length > 0) {
86-
return `{ type: ${typeName.text}, decorators: [${decoratorStr}] }`;
87-
}
88-
return `{ type: ${typeName.text} }`;
75+
break;
76+
case ts.SyntaxKind.AnyKeyword:
77+
typeName = 'undefined';
78+
break;
79+
default:
80+
typeName = 'null';
8981
}
9082
}
9183

92-
return 'null';
84+
const decorators = refactor.findAstNodes(paramNode, ts.SyntaxKind.Decorator) as ts.Decorator[];
85+
const decoratorStr = decorators
86+
.map(decorator => {
87+
const call =
88+
refactor.findFirstAstNode(decorator, ts.SyntaxKind.CallExpression) as ts.CallExpression;
89+
90+
if (!call) {
91+
return null;
92+
}
93+
94+
const fnName = call.expression.getText(refactor.sourceFile);
95+
const args = call.arguments.map(x => x.getText(refactor.sourceFile)).join(', ');
96+
if (angularImports.indexOf(fnName) === -1) {
97+
return null;
98+
} else {
99+
return [fnName, args];
100+
}
101+
})
102+
.filter(x => !!x)
103+
.map(([name, args]: string[]) => {
104+
if (args) {
105+
return `{ type: ${name}, args: [${args}] }`;
106+
}
107+
return `{ type: ${name} }`;
108+
})
109+
.join(', ');
110+
111+
if (decorators.length > 0) {
112+
return `{ type: ${typeName}, decorators: [${decoratorStr}] }`;
113+
}
114+
return `{ type: ${typeName} }`;
93115
}
94116

95117

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

108130
const params = Array.from(ctor.parameters).map(paramNode => {
109-
switch (paramNode.type.kind) {
110-
case ts.SyntaxKind.TypeReference:
111-
return _ctorParameterFromTypeReference(paramNode, angularImports, refactor);
112-
default:
113-
return 'null';
114-
}
131+
return _ctorParameterFromTypeReference(paramNode, angularImports, refactor);
115132
});
116133

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

0 commit comments

Comments
 (0)