1
+ /* eslint-disable no-console */
1
2
import _ from 'lodash' ;
2
3
import * as ts from 'typescript' ;
3
4
import createDebug from 'debug' ;
@@ -10,10 +11,34 @@ type TypeFilename = string;
10
11
11
12
type UpdateDefinitionFunction = ( newDef : Record < TypeFilename , string > ) => void ;
12
13
14
+ // TODO
15
+ /*
16
+ function createIOError(code: string, details = ""): NodeJS.ErrnoException {
17
+ const err: NodeJS.ErrnoException = new Error(`${code} ${details}`);
18
+ err.code = code;
19
+ if (Error.captureStackTrace) Error.captureStackTrace(err, createIOError);
20
+ return err;
21
+ }
22
+ */
23
+ function relativeNodePath ( fileName : string ) : string {
24
+ const parts = fileName . split ( / \/ n o d e _ m o d u l e s \/ / g) ;
25
+ return parts [ parts . length - 1 ] ;
26
+ }
27
+
28
+ type EncounteredPaths = {
29
+ getScriptSnapshot : string [ ] ;
30
+ fileExists : string [ ] ;
31
+ readFile : string [ ] ;
32
+ //readDirectory: string[],
33
+ //directoryExists: string[],
34
+ //getDirectories: string[]
35
+ } ;
36
+
13
37
function getVirtualLanguageService ( ) : {
14
38
languageService : ts . LanguageService ;
15
39
updateCode : UpdateDefinitionFunction ;
16
40
listFiles : ( ) => string [ ] ;
41
+ listEncounteredPaths : ( ) => EncounteredPaths ;
17
42
} {
18
43
const codeHolder : Record < TypeFilename , string > = Object . create ( null ) ;
19
44
const versions : Record < TypeFilename , number > = Object . create ( null ) ;
@@ -34,10 +59,22 @@ function getVirtualLanguageService(): {
34
59
}
35
60
} ;
36
61
37
- const listFiles = ( ) => {
62
+ const listFiles = ( ) : string [ ] => {
38
63
return Object . keys ( codeHolder ) ;
39
64
} ;
40
65
66
+ const encounteredPaths : EncounteredPaths = {
67
+ getScriptSnapshot : [ ] ,
68
+ fileExists : [ ] ,
69
+ readFile : [ ] ,
70
+ //readDirectory: [], // unused
71
+ //directoryExists: [], // unused
72
+ //getDirectories: [] // unused
73
+ } ;
74
+ const listEncounteredPaths = ( ) : EncounteredPaths => {
75
+ return encounteredPaths ;
76
+ } ;
77
+
41
78
const servicesHost : ts . LanguageServiceHost = {
42
79
getScriptFileNames : ( ) => {
43
80
return Object . keys ( codeHolder ) ;
@@ -50,24 +87,51 @@ function getVirtualLanguageService(): {
50
87
return ts . ScriptSnapshot . fromString ( codeHolder [ fileName ] ) ;
51
88
}
52
89
53
- return ts . ScriptSnapshot . fromString ( ts . sys . readFile ( fileName ) || '' ) ;
90
+ const result = ts . ScriptSnapshot . fromString (
91
+ ts . sys . readFile ( fileName ) || '' ,
92
+ ) ;
93
+
94
+ encounteredPaths . getScriptSnapshot . push ( relativeNodePath ( fileName ) ) ;
95
+ return result ;
54
96
} ,
55
97
getCurrentDirectory : ( ) => process . cwd ( ) ,
56
98
getCompilationSettings : ( ) => options ,
57
- getDefaultLibFileName : ( options ) => ts . getDefaultLibFilePath ( options ) ,
99
+ getDefaultLibFileName : ( options ) => {
100
+ const defaultLibFileName = ts . getDefaultLibFilePath ( options ) ;
101
+ //console.log({ defaultLibFileName })
102
+ //return '/lib.es2022.full.d.ts'
103
+ return defaultLibFileName ;
104
+ } ,
58
105
fileExists : ( fileName ) => {
59
- return fileName in codeHolder || ts . sys . fileExists ( fileName ) ;
106
+ if ( fileName in codeHolder ) {
107
+ return true ;
108
+ }
109
+ const result = ts . sys . fileExists ( fileName ) ;
110
+ if ( result ) {
111
+ encounteredPaths . fileExists . push ( relativeNodePath ( fileName ) ) ;
112
+ }
113
+ return result ;
60
114
} ,
61
115
readFile : ( fileName ) => {
62
116
if ( fileName in codeHolder ) {
63
117
return codeHolder [ fileName ] ;
64
118
}
65
- return ts . sys . readFile ( fileName ) ;
119
+ const result = ts . sys . readFile ( fileName ) ;
120
+ encounteredPaths . readFile . push ( relativeNodePath ( fileName ) ) ;
121
+ return result ;
122
+ } ,
123
+ readDirectory : ( ...args ) => {
124
+ const result = ts . sys . readDirectory ( ...args ) ;
125
+ return result ;
126
+ } ,
127
+ directoryExists : ( ...args ) => {
128
+ const result = ts . sys . directoryExists ( ...args ) ;
129
+ return result ;
130
+ } ,
131
+ getDirectories : ( ...args ) => {
132
+ const result = ts . sys . getDirectories ( ...args ) ;
133
+ return result ;
66
134
} ,
67
- readDirectory : ( ...args ) => ts . sys . readDirectory ( ...args ) ,
68
- directoryExists : ( ...args ) => ts . sys . directoryExists ( ...args ) ,
69
- getDirectories : ( ...args ) => ts . sys . getDirectories ( ...args ) ,
70
-
71
135
log : ( ...args ) => debugLog ( args ) ,
72
136
trace : ( ...args ) => debugTrace ( args ) ,
73
137
error : ( ...args ) => debugError ( args ) ,
@@ -80,6 +144,7 @@ function getVirtualLanguageService(): {
80
144
) ,
81
145
updateCode,
82
146
listFiles,
147
+ listEncounteredPaths,
83
148
} ;
84
149
}
85
150
@@ -178,13 +243,15 @@ export default class Autocompleter {
178
243
private readonly languageService : ts . LanguageService ;
179
244
readonly updateCode : UpdateDefinitionFunction ;
180
245
readonly listFiles : ( ) => string [ ] ;
246
+ readonly listEncounteredPaths : ( ) => EncounteredPaths ;
181
247
182
248
constructor ( { filter } : AutocompleterOptions = { } ) {
183
249
this . filter = filter ?? ( ( ) => true ) ;
184
250
( {
185
251
languageService : this . languageService ,
186
252
updateCode : this . updateCode ,
187
253
listFiles : this . listFiles ,
254
+ listEncounteredPaths : this . listEncounteredPaths ,
188
255
} = getVirtualLanguageService ( ) ) ;
189
256
}
190
257
0 commit comments