@@ -9,19 +9,11 @@ const debugError = createDebug('ts-autocomplete:error');
99
1010type TypeFilename = string ;
1111
12- type UpdateDefinitionFunction = ( newDef : Record < TypeFilename , string > ) => void ;
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- */
12+ type UpdateDefinitionFunction = (
13+ newDef : Record < TypeFilename , string | boolean > ,
14+ ) => void ;
15+
2316function relativeNodePath ( fileName : string ) : string {
24- //console.log(fileName);
2517 const parts = fileName . split ( / \/ n o d e _ m o d u l e s \/ / g) ;
2618 if ( parts . length === 1 && fileName . endsWith ( 'package.json' ) ) {
2719 // special case: when it looks up this package itself it isn't going to find
@@ -35,9 +27,6 @@ type EncounteredPaths = {
3527 getScriptSnapshot : string [ ] ;
3628 fileExists : string [ ] ;
3729 readFile : string [ ] ;
38- //readDirectory: string[],
39- //directoryExists: string[],
40- //getDirectories: string[]
4130} ;
4231
4332function getVirtualLanguageService ( ) : {
@@ -49,7 +38,8 @@ function getVirtualLanguageService(): {
4938 // as an optimization, the contents of a file can be string or true. This is
5039 // because some files are only checked for existence during module resolution,
5140 // but never loaded. In that case the contents is true, not a string.
52- const codeHolder : Record < TypeFilename , string | true > = Object . create ( null ) ;
41+ const codeHolder : Record < TypeFilename , string | boolean > =
42+ Object . create ( null ) ;
5343 const versions : Record < TypeFilename , number > = Object . create ( null ) ;
5444 const options : ts . CompilerOptions = {
5545 target : ts . ScriptTarget . ES2022 ,
@@ -60,7 +50,7 @@ function getVirtualLanguageService(): {
6050 allowImportingTsExtensions : true ,
6151 } ;
6252
63- const updateCode = ( newDef : Record < TypeFilename , string > ) : void => {
53+ const updateCode = ( newDef : Record < TypeFilename , string | boolean > ) : void => {
6454 for ( const [ key , value ] of Object . entries ( newDef ) ) {
6555 codeHolder [ key ] = value ;
6656 versions [ key ] = ( versions [ key ] ?? 0 ) + 1 ;
@@ -75,9 +65,6 @@ function getVirtualLanguageService(): {
7565 getScriptSnapshot : [ ] ,
7666 fileExists : [ ] ,
7767 readFile : [ ] ,
78- //readDirectory: [], // unused
79- //directoryExists: [], // unused
80- //getDirectories: [] // unused
8168 } ;
8269 const listEncounteredPaths = ( ) : EncounteredPaths => {
8370 encounteredPaths . getScriptSnapshot . sort ( ) ;
@@ -100,61 +87,66 @@ function getVirtualLanguageService(): {
10087 return ts . ScriptSnapshot . fromString ( codeHolder [ fileName ] . toString ( ) ) ;
10188 }
10289
103- // TODO: this should not be encountered outside of CI
104- const result = ts . ScriptSnapshot . fromString (
105- // NOTE: some files do not exist. A good example is "typescript/lib/es2023.ts"
106- ts . sys . readFile ( fileName ) || '' ,
107- ) ;
90+ if ( process . env . CI ) {
91+ const result = ts . ScriptSnapshot . fromString (
92+ // NOTE: some files do not exist. A good example is "typescript/lib/es2023.ts"
93+ ts . sys . readFile ( fileName ) || '' ,
94+ ) ;
10895
109- encounteredPaths . getScriptSnapshot . push ( relativeNodePath ( fileName ) ) ;
110- return result ;
96+ encounteredPaths . getScriptSnapshot . push ( relativeNodePath ( fileName ) ) ;
97+ return result ;
98+ }
11199 } ,
112100 getCurrentDirectory : ( ) => process . cwd ( ) ,
113101 getCompilationSettings : ( ) => options ,
114102 getDefaultLibFileName : ( options ) => {
115- const defaultLibFileName = ts . getDefaultLibFilePath ( options ) ;
116- //console.log({ defaultLibFileName })
117- //return '/lib.es2022.full.d.ts'
118- return defaultLibFileName ;
103+ return ts . getDefaultLibFilePath ( options ) ;
119104 } ,
120105 fileExists : ( fileName ) => {
121106 fileName = relativeNodePath ( fileName ) ;
122107 if ( fileName in codeHolder ) {
123108 return true ;
124109 }
125110
126- // TODO: this should not be encountered outside of CI when tests will fail
127- const result = ts . sys . fileExists ( fileName ) ;
128- if ( result ) {
129- encounteredPaths . fileExists . push ( relativeNodePath ( fileName ) ) ;
111+ if ( process . env . CI ) {
112+ const result = ts . sys . fileExists ( fileName ) ;
113+ if ( result ) {
114+ encounteredPaths . fileExists . push ( relativeNodePath ( fileName ) ) ;
115+ }
116+ return result ;
130117 }
131- return result ;
118+
119+ return false ;
132120 } ,
133121 readFile : ( fileName ) => {
134122 fileName = relativeNodePath ( fileName ) ;
135123 if ( fileName in codeHolder ) {
136124 return codeHolder [ fileName ] . toString ( ) ;
137125 }
138126
139- // TODO: this should not be encountered outside of CI when tests will fail
140- const result = ts . sys . readFile ( fileName ) ;
141- encounteredPaths . readFile . push ( relativeNodePath ( fileName ) ) ;
142- return result ;
127+ if ( process . env . CI ) {
128+ const result = ts . sys . readFile ( fileName ) ;
129+ encounteredPaths . readFile . push ( relativeNodePath ( fileName ) ) ;
130+ return result ;
131+ }
143132 } ,
144133 readDirectory : ( ...args ) => {
145- // TODO: this should not be encountered outside of CI when tests will fail
146- const result = ts . sys . readDirectory ( ...args ) ;
147- return result ;
134+ if ( process . env . CI ) {
135+ return ts . sys . readDirectory ( ...args ) ;
136+ }
137+ return [ ] ;
148138 } ,
149139 directoryExists : ( ...args ) => {
150- // TODO: this should not be encountered outside of CI when tests will fail
151- const result = ts . sys . directoryExists ( ...args ) ;
152- return result ;
140+ if ( process . env . CI ) {
141+ return ts . sys . directoryExists ( ...args ) ;
142+ }
143+ return false ;
153144 } ,
154145 getDirectories : ( ...args ) => {
155- // TODO: this should not be encountered outside of CI when tests will fail
156- const result = ts . sys . getDirectories ( ...args ) ;
157- return result ;
146+ if ( process . env . CI ) {
147+ return ts . sys . getDirectories ( ...args ) ;
148+ }
149+ return [ ] ;
158150 } ,
159151 log : ( ...args ) => debugLog ( args ) ,
160152 trace : ( ...args ) => debugTrace ( args ) ,
0 commit comments