File tree Expand file tree Collapse file tree
packages/@ngtools/webpack/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import * as ts from 'typescript' ;
2+ import { removeModuleIdOnlyForTesting } from './loader' ;
3+ import { WebpackCompilerHost } from './compiler_host' ;
4+ import { TypeScriptFileRefactor } from './refactor' ;
5+
6+ describe ( '@ngtools/webpack' , ( ) => {
7+ describe ( 'loader' , ( ) => {
8+ describe ( 'removeModuleId' , ( ) => {
9+ it ( 'should work' , ( ) => {
10+ const host = new WebpackCompilerHost ( { } , '' ) ;
11+ host . writeFile ( '/file.ts' , `
12+ export const obj = { moduleId: 123 };
13+ export const obj2 = { moduleId: 123, otherValue: 1 };
14+ export const obj2 = { otherValue: 1, moduleId: 123 };
15+ ` , false ) ;
16+
17+ const program = ts . createProgram ( [ '/file.ts' ] , { } , host ) ;
18+
19+ const refactor = new TypeScriptFileRefactor ( '/file.ts' , host , program ) ;
20+ removeModuleIdOnlyForTesting ( refactor ) ;
21+
22+ expect ( refactor . sourceText ) . toMatch ( / o b j = \{ \s + } ; / ) ;
23+ expect ( refactor . sourceText ) . toMatch ( / o b j 2 = \{ \s * o t h e r V a l u e : 1 \s * } ; / ) ;
24+ } ) ;
25+ } ) ;
26+ } ) ;
27+ } ) ;
Original file line number Diff line number Diff line change @@ -97,10 +97,10 @@ function _removeModuleId(refactor: TypeScriptFileRefactor) {
9797 refactor . findAstNodes ( sourceFile , ts . SyntaxKind . ObjectLiteralExpression , true )
9898 // Get all their property assignments.
9999 . filter ( ( node : ts . ObjectLiteralExpression ) =>
100- node . properties . some ( prop => prop . name . getText ( ) == 'moduleId' ) )
100+ node . properties . some ( prop => _getContentOfKeyLiteral ( sourceFile , prop . name ) == 'moduleId' ) )
101101 . forEach ( ( node : ts . ObjectLiteralExpression ) => {
102102 const moduleIdProp = node . properties . filter ( ( prop : ts . ObjectLiteralElement , idx : number ) => {
103- return prop . name . getText ( ) == 'moduleId' ;
103+ return _getContentOfKeyLiteral ( sourceFile , prop . name ) == 'moduleId' ;
104104 } ) [ 0 ] ;
105105 // get the trailing comma
106106 const moduleIdCommaProp = moduleIdProp . parent . getChildAt ( 1 ) . getChildren ( ) [ 1 ] ;
Original file line number Diff line number Diff line change @@ -170,7 +170,7 @@ export class TypeScriptFileRefactor {
170170 }
171171
172172 removeNodes ( ...nodes : ts . Node [ ] ) {
173- nodes . forEach ( node => this . removeNode ( node ) ) ;
173+ nodes . forEach ( node => node && this . removeNode ( node ) ) ;
174174 }
175175
176176 replaceNode ( node : ts . Node , replacement : string ) {
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments