Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@ngtools/webpack): fix error #4078

Merged
merged 1 commit into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/@ngtools/webpack/src/loader.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as ts from 'typescript';
import {removeModuleIdOnlyForTesting} from './loader';
import {WebpackCompilerHost} from './compiler_host';
import {TypeScriptFileRefactor} from './refactor';

describe('@ngtools/webpack', () => {
describe('loader', () => {
describe('removeModuleId', () => {
it('should work', () => {
const host = new WebpackCompilerHost({}, '');
host.writeFile('/file.ts', `
export const obj = { moduleId: 123 };
export const obj2 = { moduleId: 123, otherValue: 1 };
export const obj2 = { otherValue: 1, moduleId: 123 };
`, false);

const program = ts.createProgram(['/file.ts'], {}, host);

const refactor = new TypeScriptFileRefactor('/file.ts', host, program);
removeModuleIdOnlyForTesting(refactor);

expect(refactor.sourceText).toMatch(/obj = \{\s+};/);
expect(refactor.sourceText).toMatch(/obj2 = \{\s*otherValue: 1\s*};/);
});
});
});
});
4 changes: 2 additions & 2 deletions packages/@ngtools/webpack/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ function _removeModuleId(refactor: TypeScriptFileRefactor) {
refactor.findAstNodes(sourceFile, ts.SyntaxKind.ObjectLiteralExpression, true)
// Get all their property assignments.
.filter((node: ts.ObjectLiteralExpression) =>
node.properties.some(prop => prop.name.getText() == 'moduleId'))
node.properties.some(prop => _getContentOfKeyLiteral(sourceFile, prop.name) == 'moduleId'))
.forEach((node: ts.ObjectLiteralExpression) => {
const moduleIdProp = node.properties.filter((prop: ts.ObjectLiteralElement, idx: number) => {
return prop.name.getText() == 'moduleId';
return _getContentOfKeyLiteral(sourceFile, prop.name) == 'moduleId';
})[0];
// get the trailing comma
const moduleIdCommaProp = moduleIdProp.parent.getChildAt(1).getChildren()[1];
Expand Down
2 changes: 1 addition & 1 deletion packages/@ngtools/webpack/src/refactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export class TypeScriptFileRefactor {
}

removeNodes(...nodes: ts.Node[]) {
nodes.forEach(node => this.removeNode(node));
nodes.forEach(node => node && this.removeNode(node));
}

replaceNode(node: ts.Node, replacement: string) {
Expand Down
33 changes: 0 additions & 33 deletions tests/acceptance/ngtools-webpack-loader.spec.ts

This file was deleted.