Skip to content

Commit 8c68535

Browse files
committedSep 13, 2020
Clean up
1 parent d541596 commit 8c68535

File tree

1 file changed

+20
-48
lines changed

1 file changed

+20
-48
lines changed
 

‎server/src/server.ts

+20-48
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ let diagnosticTimer: null | NodeJS.Timeout = null;
3131

3232
// congrats. A simple UI problem is now a distributed system problem
3333
let stupidFileContentCache: { [key: string]: string } = {}
34-
let previousDiagnosticFiles: Set<string> = new Set()
34+
let previouslyDiagnosedFiles: Set<string> = new Set()
3535

36+
// TODO: races here
3637
let findDirOfFileNearFile = (fileToFind: p.DocumentUri, source: p.DocumentUri): null | p.DocumentUri => {
3738
let dir = path.dirname(source)
3839
if (fs.existsSync(path.join(dir, fileToFind))) {
@@ -113,31 +114,20 @@ let parseDiagnosticLocation = (location: string): Range => {
113114
}
114115

115116
let parseCompilerLogOutput = (content: string, separator: string) => {
116-
// TODO: update this content example
117-
/* example .compiler.log file content:
118-
119-
Cleaning... 6 files.
120-
Cleaning... 87 files.
121-
[1/5] [34mBuilding[39m [2msrc/TestFramework.reiast[22m
122-
[2/5] [34mBuilding[39m [2msrc/TestFramework.reast[22m
123-
[3/5] Building src/test.resast
124-
FAILED: src/test.resast
125-
/Users/chenglou/github/bucklescript/darwin/bsc.exe -bs-jsx 3 -bs-no-version-header -o src/test.resast -bs-syntax-only -bs-binary-ast /Users/chenglou/github/reason-react/src/test.res
117+
/* example .compiler.log file content that we're gonna parse:
126118
127119
Syntax error!
128-
/Users/chenglou/github/reason-react/src/test.res 1:8-2:3
120+
/Users/chenglou/github/reason-react/src/test.res:1:8-2:3
129121
130122
1 │ let a =
131123
2 │ let b =
132124
3 │
133125
134126
This let-binding misses an expression
135127
136-
[8/29] Building src/legacy/ReactDOMServerRe.reast
137-
FAILED: src/test.cmj src/test.cmi
138128
139129
Warning number 8
140-
/Users/chenglou/github/reason-react/src/test.res 3:5-8
130+
/Users/chenglou/github/reason-react/src/test.res:3:5-8
141131
142132
1 │ let a = j`😀`
143133
2 │ let b = `😀`
@@ -148,8 +138,9 @@ FAILED: src/test.cmj src/test.cmi
148138
You forgot to handle a possible case here, for example:
149139
Some _
150140
141+
151142
We've found a bug for you!
152-
/Users/chenglou/github/reason-react/src/test.res 3:9
143+
/Users/chenglou/github/reason-react/src/test.res:3:9
153144
154145
1 │ let a = 1
155146
2 │ let b = "hi"
@@ -160,13 +151,8 @@ FAILED: src/test.cmj src/test.cmi
160151
161152
But somewhere wanted:
162153
int
163-
164-
165-
[15/62] [34mBuilding[39m [2msrc/ReactDOMServer.reast[22m
166154
*/
167155

168-
// we're gonna chop that
169-
170156
type parsedDiagnostic = {
171157
code: number | undefined,
172158
severity: t.DiagnosticSeverity,
@@ -183,9 +169,9 @@ FAILED: src/test.cmj src/test.cmi
183169
content: []
184170
})
185171
} else if (line.startsWith(' Warning number ')) {
186-
let match = line.match(/ Warning number (\d+)/)
172+
let match = parseInt(line.slice(' Warning number '.length))
187173
parsedDiagnostics.push({
188-
code: match ? parseInt(match[1]) : undefined,
174+
code: Number.isNaN(match) ? undefined : match,
189175
severity: t.DiagnosticSeverity.Warning,
190176
content: []
191177
})
@@ -234,7 +220,6 @@ FAILED: src/test.cmj src/test.cmi
234220

235221
let startWatchingCompilerLog = (process: NodeJS.Process) => {
236222
// chokidar.watch()
237-
// TOOD: setTimeout instead
238223
let id = setInterval(() => {
239224
let openFiles = Object.keys(stupidFileContentCache);
240225
let compilerLogDirs: Set<p.DocumentUri> = new Set();
@@ -246,27 +231,26 @@ let startWatchingCompilerLog = (process: NodeJS.Process) => {
246231
}
247232
});
248233

249-
let files: { [key: string]: t.Diagnostic[] } = {}
250-
234+
let diagnosedFiles: { [key: string]: t.Diagnostic[] } = {}
251235
compilerLogDirs.forEach(compilerLogDir => {
252236
let compilerLogPath = path.join(compilerLogDir, compilerLogPartialPath);
253237
let content = fs.readFileSync(compilerLogPath, { encoding: 'utf-8' });
254238
let filesAndErrors = parseCompilerLogOutput(content, ":")
255239
Object.keys(filesAndErrors).forEach(file => {
256240
// assumption: there's no existing files[file] entry
257241
// this is true; see the lines above. A file can only belong to one .compiler.log root
258-
files[file] = filesAndErrors[file]
242+
diagnosedFiles[file] = filesAndErrors[file]
259243
})
260244
});
261245

262246
// Send new diagnostic, wipe old ones
263-
let filePaths = Object.keys(files)
264-
filePaths.forEach(file => {
247+
let diagnosedFilePaths = Object.keys(diagnosedFiles)
248+
diagnosedFilePaths.forEach(file => {
265249
let params: p.PublishDiagnosticsParams = {
266250
uri: file,
267251
// there's a new optional version param from https://github.com/microsoft/language-server-protocol/issues/201
268252
// not using it for now, sigh
269-
diagnostics: files[file],
253+
diagnostics: diagnosedFiles[file],
270254
}
271255
let notification: m.NotificationMessage = {
272256
jsonrpc: jsonrpcVersion,
@@ -276,10 +260,10 @@ let startWatchingCompilerLog = (process: NodeJS.Process) => {
276260
process.send!(notification);
277261

278262
// this file's taken care of already now. Remove from old diagnostic files
279-
previousDiagnosticFiles.delete(file)
263+
previouslyDiagnosedFiles.delete(file)
280264
})
281265
// wipe the errors from the files that are no longer erroring
282-
previousDiagnosticFiles.forEach(remainingPreviousFile => {
266+
previouslyDiagnosedFiles.forEach(remainingPreviousFile => {
283267
let params: p.PublishDiagnosticsParams = {
284268
uri: remainingPreviousFile,
285269
diagnostics: [],
@@ -291,7 +275,7 @@ let startWatchingCompilerLog = (process: NodeJS.Process) => {
291275
};
292276
process.send!(notification);
293277
})
294-
previousDiagnosticFiles = new Set(filePaths)
278+
previouslyDiagnosedFiles = new Set(diagnosedFilePaths)
295279
}, 1000);
296280

297281
return id;
@@ -351,19 +335,11 @@ process.on('message', (a: (m.RequestMessage | m.NotificationMessage)) => {
351335
};
352336
process.send!(response);
353337
} else if (aa.method === 'initialize') {
354-
let param: p.InitializeParams = aa.params
355-
// TODO: sigh what's deprecated now?
356-
let root = param.rootUri
357-
if (root == null) {
358-
// TODO: handle single file
359-
console.log("not handling single file")
360-
} else {
361-
diagnosticTimer = startWatchingCompilerLog(process)
362-
}
338+
diagnosticTimer = startWatchingCompilerLog(process)
363339
// send the list of things we support
364340
let result: p.InitializeResult = {
365341
capabilities: {
366-
// TODO: incremental sync
342+
// TODO: incremental sync?
367343
textDocumentSync: v.TextDocumentSyncKind.Full,
368344
documentFormattingProvider: true,
369345
}
@@ -434,12 +410,8 @@ process.on('message', (a: (m.RequestMessage | m.NotificationMessage)) => {
434410
};
435411
process.send!(response);
436412
} else {
437-
// file to format potentially doesn't exist anymore because of races. But that's ok, the error from bsc should handle it
413+
// code will always be defined here, even though technically it can be undefined
438414
let code = stupidFileContentCache[params.textDocument.uri];
439-
// TODO: error here?
440-
if (code === undefined) {
441-
console.log("can't find file")
442-
}
443415
let formattedResult = formatUsingValidBscPath(
444416
code,
445417
path.join(nodeModulesParentPath, bscPartialPath),

0 commit comments

Comments
 (0)
Please sign in to comment.