Skip to content

Commit

Permalink
fix(@ngtools/webpack): fix error with object spread (angular#4642)
Browse files Browse the repository at this point in the history
The loader throws an exception when an object spread is used. This fixes it.

Fixes angular#4600
  • Loading branch information
hansl authored and Zhicheng Wang committed Mar 16, 2017
1 parent a899b2a commit 82db5e2
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/@ngtools/webpack/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const NormalModule = require('webpack/lib/NormalModule');


function _getContentOfKeyLiteral(source: ts.SourceFile, node: ts.Node): string {
if (node.kind == ts.SyntaxKind.Identifier) {
if (!node) {
return null;
} else if (node.kind == ts.SyntaxKind.Identifier) {
return (node as ts.Identifier).text;
} else if (node.kind == ts.SyntaxKind.StringLiteral) {
return (node as ts.StringLiteral).text;
Expand Down Expand Up @@ -241,11 +243,16 @@ function _removeModuleId(refactor: TypeScriptFileRefactor) {

refactor.findAstNodes(sourceFile, ts.SyntaxKind.ObjectLiteralExpression, true)
// Get all their property assignments.
.filter((node: ts.ObjectLiteralExpression) =>
node.properties.some(prop => _getContentOfKeyLiteral(sourceFile, prop.name) == 'moduleId'))
.filter((node: ts.ObjectLiteralExpression) => {
return node.properties.some(prop => {
return prop.kind == ts.SyntaxKind.PropertyAssignment
&& _getContentOfKeyLiteral(sourceFile, prop.name) == 'moduleId';
});
})
.forEach((node: ts.ObjectLiteralExpression) => {
const moduleIdProp = node.properties.filter((prop: ts.ObjectLiteralElement, idx: number) => {
return _getContentOfKeyLiteral(sourceFile, prop.name) == 'moduleId';
return prop.kind == ts.SyntaxKind.PropertyAssignment
&& _getContentOfKeyLiteral(sourceFile, prop.name) == 'moduleId';
})[0];
// get the trailing comma
const moduleIdCommaProp = moduleIdProp.parent.getChildAt(1).getChildren()[1];
Expand Down

0 comments on commit 82db5e2

Please sign in to comment.