@@ -85,17 +85,17 @@ export class Host implements ts.LanguageServiceHost {
85
85
}
86
86
87
87
getScriptFileNames ( ) {
88
- return Object . keys ( this . state . files ) ;
88
+ return this . state . allFileNames ( ) ;
89
89
}
90
90
91
91
getScriptVersion ( fileName : string ) {
92
- if ( this . state . files [ fileName ] ) {
93
- return this . state . files [ fileName ] . version . toString ( ) ;
92
+ if ( this . state . getFile ( fileName ) ) {
93
+ return this . state . getFile ( fileName ) . version . toString ( ) ;
94
94
}
95
95
}
96
96
97
97
getScriptSnapshot ( fileName ) {
98
- let file = this . state . files [ fileName ] ;
98
+ let file = this . state . getFile ( fileName ) ;
99
99
if ( file ) {
100
100
return this . state . ts . ScriptSnapshot . fromString ( file . text ) ;
101
101
}
@@ -169,7 +169,7 @@ export class State {
169
169
fs : typeof fs ;
170
170
compilerInfo : ICompilerInfo ;
171
171
host : Host ;
172
- files : { [ fileName : string ] : IFile } = { } ;
172
+ private files : { [ fileName : string ] : IFile } = { } ;
173
173
services : ts . LanguageService ;
174
174
options : ICompilerOptions ;
175
175
program : ts . Program ;
@@ -227,7 +227,21 @@ export class State {
227
227
this . program = this . services . getProgram ( ) ;
228
228
}
229
229
230
+ allFileNames ( ) {
231
+ return Object . keys ( this . files ) ;
232
+ }
233
+
234
+ /**
235
+ * Returns all the files in this state.
236
+ * Don't add new files using this value (eg `allFiles()[newFilePath] = ...`), just use it as a
237
+ * read only reference (as otherwise the paths won't be normalized correctly)
238
+ */
239
+ allFiles ( ) {
240
+ return this . files ;
241
+ }
242
+
230
243
emit ( fileName : string ) : IEmitOutput {
244
+ fileName = this . normalizePath ( fileName ) ;
231
245
232
246
if ( ! this . program ) {
233
247
this . program = this . services . getProgram ( ) ;
@@ -270,6 +284,7 @@ export class State {
270
284
}
271
285
272
286
updateFile ( fileName : string , text : string , checked : boolean = false ) : boolean {
287
+ fileName = this . normalizePath ( fileName ) ;
273
288
let prevFile = this . files [ fileName ] ;
274
289
let version = 0 ;
275
290
let changed = true ;
@@ -291,37 +306,49 @@ export class State {
291
306
}
292
307
293
308
addFile ( fileName : string , text : string ) : void {
309
+ fileName = this . normalizePath ( fileName ) ;
294
310
this . files [ fileName ] = {
295
311
text : text ,
296
312
version : 0
297
313
}
298
314
}
299
315
316
+ getFile ( fileName : string ) {
317
+ fileName = this . normalizePath ( fileName ) ;
318
+ return this . files [ fileName ] ;
319
+ }
320
+
300
321
hasFile ( fileName : string ) : boolean {
322
+ fileName = this . normalizePath ( fileName ) ;
301
323
return this . files . hasOwnProperty ( fileName ) ;
302
324
}
303
325
304
326
readFile ( fileName : string ) : Promise < string > {
327
+ fileName = this . normalizePath ( fileName ) ;
305
328
let readFile = Promise . promisify ( this . fs . readFile . bind ( this . fs ) ) ;
306
329
return readFile ( fileName ) . then ( function ( buf ) {
307
330
return buf . toString ( 'utf8' ) ;
308
331
} ) ;
309
332
}
310
333
311
334
readFileSync ( fileName : string ) : string {
335
+ fileName = this . normalizePath ( fileName ) ;
312
336
// Use global fs here, because local doesn't contain `readFileSync`
313
337
return fs . readFileSync ( fileName , { encoding : 'utf-8' } ) ;
314
338
}
315
339
316
340
readFileAndAdd ( fileName : string ) : Promise < any > {
341
+ fileName = this . normalizePath ( fileName ) ;
317
342
return this . readFile ( fileName ) . then ( ( text ) => this . addFile ( fileName , text ) ) ;
318
343
}
319
344
320
345
readFileAndUpdate ( fileName : string , checked : boolean = false ) : Promise < boolean > {
346
+ fileName = this . normalizePath ( fileName ) ;
321
347
return this . readFile ( fileName ) . then ( ( text ) => this . updateFile ( fileName , text , checked ) ) ;
322
348
}
323
349
324
350
readFileAndUpdateSync ( fileName : string , checked : boolean = false ) : boolean {
351
+ fileName = this . normalizePath ( fileName ) ;
325
352
let text = this . readFileSync ( fileName ) ;
326
353
return this . updateFile ( fileName , text , checked ) ;
327
354
}
0 commit comments