Skip to content

Commit d5525d9

Browse files
committed
figure out all the types we need at runtime
1 parent 5213e85 commit d5525d9

File tree

2 files changed

+81
-10
lines changed

2 files changed

+81
-10
lines changed

packages/ts-autocomplete/src/index.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
import path from 'path';
23
import { promises as fs } from 'fs';
34
import Autocompleter from './index';
@@ -33,13 +34,16 @@ describe('Autocompleter', function () {
3334
autoCompleter = new Autocompleter();
3435
});
3536

36-
it('returns the global scope for a global variable that does not exist', function () {
37+
it.only('returns the global scope for a global variable that does not exist', function () {
3738
autoCompleter.updateCode({
3839
'/code.d.ts': CODE_TS,
3940
});
4041

4142
const completions = autoCompleter.autocomplete('doesNotExist');
4243
expect(completions.length).to.be.gt(100);
44+
console.log(
45+
JSON.stringify(autoCompleter.listEncounteredPaths(), null, 2),
46+
);
4347
});
4448

4549
it('returns completions for global variables', function () {

packages/ts-autocomplete/src/index.ts

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
import _ from 'lodash';
23
import * as ts from 'typescript';
34
import createDebug from 'debug';
@@ -10,10 +11,34 @@ type TypeFilename = string;
1011

1112
type UpdateDefinitionFunction = (newDef: Record<TypeFilename, string>) => void;
1213

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(/\/node_modules\//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+
1337
function getVirtualLanguageService(): {
1438
languageService: ts.LanguageService;
1539
updateCode: UpdateDefinitionFunction;
1640
listFiles: () => string[];
41+
listEncounteredPaths: () => EncounteredPaths;
1742
} {
1843
const codeHolder: Record<TypeFilename, string> = Object.create(null);
1944
const versions: Record<TypeFilename, number> = Object.create(null);
@@ -34,10 +59,22 @@ function getVirtualLanguageService(): {
3459
}
3560
};
3661

37-
const listFiles = () => {
62+
const listFiles = (): string[] => {
3863
return Object.keys(codeHolder);
3964
};
4065

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+
4178
const servicesHost: ts.LanguageServiceHost = {
4279
getScriptFileNames: () => {
4380
return Object.keys(codeHolder);
@@ -50,24 +87,51 @@ function getVirtualLanguageService(): {
5087
return ts.ScriptSnapshot.fromString(codeHolder[fileName]);
5188
}
5289

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;
5496
},
5597
getCurrentDirectory: () => process.cwd(),
5698
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+
},
58105
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;
60114
},
61115
readFile: (fileName) => {
62116
if (fileName in codeHolder) {
63117
return codeHolder[fileName];
64118
}
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;
66134
},
67-
readDirectory: (...args) => ts.sys.readDirectory(...args),
68-
directoryExists: (...args) => ts.sys.directoryExists(...args),
69-
getDirectories: (...args) => ts.sys.getDirectories(...args),
70-
71135
log: (...args) => debugLog(args),
72136
trace: (...args) => debugTrace(args),
73137
error: (...args) => debugError(args),
@@ -80,6 +144,7 @@ function getVirtualLanguageService(): {
80144
),
81145
updateCode,
82146
listFiles,
147+
listEncounteredPaths,
83148
};
84149
}
85150

@@ -178,13 +243,15 @@ export default class Autocompleter {
178243
private readonly languageService: ts.LanguageService;
179244
readonly updateCode: UpdateDefinitionFunction;
180245
readonly listFiles: () => string[];
246+
readonly listEncounteredPaths: () => EncounteredPaths;
181247

182248
constructor({ filter }: AutocompleterOptions = {}) {
183249
this.filter = filter ?? (() => true);
184250
({
185251
languageService: this.languageService,
186252
updateCode: this.updateCode,
187253
listFiles: this.listFiles,
254+
listEncounteredPaths: this.listEncounteredPaths,
188255
} = getVirtualLanguageService());
189256
}
190257

0 commit comments

Comments
 (0)