File tree 4 files changed +30
-36
lines changed
packages/@ngtools/webpack/src
4 files changed +30
-36
lines changed 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
+ ` ) ;
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) {
97
97
refactor . findAstNodes ( sourceFile , ts . SyntaxKind . ObjectLiteralExpression , true )
98
98
// Get all their property assignments.
99
99
. filter ( ( node : ts . ObjectLiteralExpression ) =>
100
- node . properties . some ( prop => prop . name . getText ( ) == 'moduleId' ) )
100
+ node . properties . some ( prop => _getContentOfKeyLiteral ( sourceFile , prop . name ) == 'moduleId' ) )
101
101
. forEach ( ( node : ts . ObjectLiteralExpression ) => {
102
102
const moduleIdProp = node . properties . filter ( ( prop : ts . ObjectLiteralElement , idx : number ) => {
103
- return prop . name . getText ( ) == 'moduleId' ;
103
+ return _getContentOfKeyLiteral ( sourceFile , prop . name ) == 'moduleId' ;
104
104
} ) [ 0 ] ;
105
105
// get the trailing comma
106
106
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 {
170
170
}
171
171
172
172
removeNodes ( ...nodes : ts . Node [ ] ) {
173
- nodes . forEach ( node => this . removeNode ( node ) ) ;
173
+ nodes . forEach ( node => node && this . removeNode ( node ) ) ;
174
174
}
175
175
176
176
replaceNode ( node : ts . Node , replacement : string ) {
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments