Skip to content

Commit 6202c51

Browse files
committed
Enable loading std library d.ts files
This implements enough of `ServerHost` that we can load the standard d.ts files using synchronous XMLHttpRequests. I also had to patch some code in `editorServices`. I don't know if these changes are correct and need someone on the TS team to review
1 parent f3178b6 commit 6202c51

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

src/server/editorServices.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,7 +2489,7 @@ namespace ts.server {
24892489
}
24902490

24912491
private getOrCreateScriptInfoNotOpenedByClientForNormalizedPath(fileName: NormalizedPath, currentDirectory: string, scriptKind: ScriptKind | undefined, hasMixedContent: boolean | undefined, hostToQueryFileExistsOn: DirectoryStructureHost | undefined) {
2492-
if (isRootedDiskPath(fileName) || isDynamicFileName(fileName)) {
2492+
if (isRootedDiskPath(fileName) || isDynamicFileName(fileName) || isUrl(fileName)) {
24932493
return this.getOrCreateScriptInfoWorker(fileName, currentDirectory, /*openedByClient*/ false, /*fileContent*/ undefined, scriptKind, hasMixedContent, hostToQueryFileExistsOn);
24942494
}
24952495

@@ -2519,7 +2519,7 @@ namespace ts.server {
25192519
let info = this.getScriptInfoForPath(path);
25202520
if (!info) {
25212521
const isDynamic = isDynamicFileName(fileName);
2522-
Debug.assert(isRootedDiskPath(fileName) || isDynamic || openedByClient, "", () => `${JSON.stringify({ fileName, currentDirectory, hostCurrentDirectory: this.currentDirectory, openKeys: arrayFrom(this.openFilesWithNonRootedDiskPath.keys()) })}\nScript info with non-dynamic relative file name can only be open script info or in context of host currentDirectory`);
2522+
Debug.assert(isRootedDiskPath(fileName) || isUrl(fileName) || isDynamic || openedByClient, "", () => `${JSON.stringify({ fileName, currentDirectory, hostCurrentDirectory: this.currentDirectory, openKeys: arrayFrom(this.openFilesWithNonRootedDiskPath.keys()) })}\nScript info with non-dynamic relative file name can only be open script info or in context of host currentDirectory`);
25232523
Debug.assert(!isRootedDiskPath(fileName) || this.currentDirectory === currentDirectory || !this.openFilesWithNonRootedDiskPath.has(this.toCanonicalFileName(fileName)), "", () => `${JSON.stringify({ fileName, currentDirectory, hostCurrentDirectory: this.currentDirectory, openKeys: arrayFrom(this.openFilesWithNonRootedDiskPath.keys()) })}\nOpen script files with non rooted disk path opened with current directory context cannot have same canonical names`);
25242524
Debug.assert(!isDynamic || this.currentDirectory === currentDirectory || this.useInferredProjectPerProjectRoot, "", () => `${JSON.stringify({ fileName, currentDirectory, hostCurrentDirectory: this.currentDirectory, openKeys: arrayFrom(this.openFilesWithNonRootedDiskPath.keys()) })}\nDynamic files must always be opened with service's current directory or service should support inferred project per projectRootPath.`);
25252525
// If the file is not opened by client and the file doesnot exist on the disk, return

src/tsserver/webServer.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ namespace ts.server {
33
declare const addEventListener: any;
44
declare const postMessage: any;
55
declare const close: any;
6+
declare const location: any;
7+
declare const XMLHttpRequest: any;
8+
69
const nullLogger: Logger = {
710
close: noop,
811
hasLevel: returnFalse,
@@ -49,17 +52,34 @@ namespace ts.server {
4952
function createWebSystem(args: string[]) {
5053
Debug.assert(ts.sys === undefined);
5154
const returnEmptyString = () => "";
55+
5256
const sys: ServerHost = {
5357
args,
54-
newLine: "\r\n", // TODO::
55-
useCaseSensitiveFileNames: false, // TODO::
56-
readFile: returnUndefined, // TODO:: read lib Files
58+
newLine: "\n", // This can be configured by clients
59+
useCaseSensitiveFileNames: false, // Use false as the default on web since that is the safest option
60+
readFile: (path: string, _encoding?: string): string | undefined => {
61+
if (!path.startsWith("http:") && !path.startsWith("https:")) {
62+
return undefined;
63+
}
64+
65+
const request = new XMLHttpRequest();
66+
request.open("GET", path, /* asynchronous */ false);
67+
request.send();
68+
69+
if (request.status !== 200) {
70+
return undefined;
71+
}
72+
73+
return request.responseText;
74+
},
5775

5876
write: postMessage,
5977
watchFile: returnNoopFileWatcher,
6078
watchDirectory: returnNoopFileWatcher,
6179

62-
getExecutingFilePath: returnEmptyString, // TODO:: Locale, lib locations, typesmap location, plugins
80+
getExecutingFilePath: () => {
81+
return location + "";
82+
},
6383
getCurrentDirectory: returnEmptyString, // For inferred project root if projectRoot path is not set, normalizing the paths
6484

6585
/* eslint-disable no-restricted-globals */
@@ -77,11 +97,26 @@ namespace ts.server {
7797
// tryEnableSourceMapsForHost?(): void;
7898
// debugMode?: boolean;
7999

80-
// Future for semantic server mode
81-
fileExists: returnFalse, // Module Resolution
82-
directoryExists: returnFalse, // Module resolution
100+
// For semantic server mode
101+
fileExists: (path: string): boolean => {
102+
if (!path.startsWith("http:") && !path.startsWith("https:")) {
103+
return false;
104+
}
105+
106+
const request = new XMLHttpRequest();
107+
request.open("HEAD", path, /* asynchronous */ false);
108+
request.send();
109+
110+
return request.status === 200;
111+
},
112+
directoryExists: (_path: string): boolean => {
113+
return false;
114+
},
83115
readDirectory: notImplemented, // Configured project, typing installer
84-
getDirectories: () => [], // For automatic type reference directives
116+
getDirectories: (path: string) => {
117+
console.log("paths", path);
118+
return [];
119+
}, // For automatic type reference directives
85120
createDirectory: notImplemented, // compile On save
86121
writeFile: notImplemented, // compile on save
87122
resolvePath: identity, // Plugins

0 commit comments

Comments
 (0)