Skip to content

Commit a01ea88

Browse files
committedMay 8, 2021
Avoid unchecked type annotations after parsing JSON result.
1 parent 77ba514 commit a01ea88

File tree

2 files changed

+70
-56
lines changed

2 files changed

+70
-56
lines changed
 

‎server/src/server.ts

+28-41
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as chokidar from "chokidar";
1919
import { assert } from "console";
2020
import { fileURLToPath } from "url";
2121
import { ChildProcess } from "child_process";
22-
import { WorkspaceEdit } from "vscode-languageserver";
22+
import { WorkspaceEdit } from "vscode-languageserver";
2323
import { TextEdit } from "vscode-languageserver-types";
2424

2525
// https://microsoft.github.io/language-server-protocol/specification#initialize
@@ -355,58 +355,50 @@ function onMessage(msg: m.Message) {
355355
} else if (msg.method === p.HoverRequest.method) {
356356
let params = msg.params as p.HoverParams;
357357
let filePath = fileURLToPath(params.textDocument.uri);
358-
let result: typeof p.HoverRequest.type = utils.runAnalysisAfterSanityCheck(
358+
let response = utils.runAnalysisCommand(
359359
filePath,
360-
["hover", filePath, params.position.line, params.position.character]
360+
["hover", filePath, params.position.line, params.position.character],
361+
msg
361362
);
362-
let hoverResponse: m.ResponseMessage = {
363-
jsonrpc: c.jsonrpcVersion,
364-
id: msg.id,
365-
// type result = Hover | null
366-
// type Hover = {contents: MarkedString | MarkedString[] | MarkupContent, range?: Range}
367-
result,
368-
};
369-
send(hoverResponse);
363+
send(response);
370364
} else if (msg.method === p.DefinitionRequest.method) {
371365
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition
372366
let params = msg.params as p.DefinitionParams;
373367
let filePath = fileURLToPath(params.textDocument.uri);
374-
let result: typeof p.DefinitionRequest.type = utils.runAnalysisAfterSanityCheck(
368+
let response = utils.runAnalysisCommand(
375369
filePath,
376370
[
377371
"definition",
378372
filePath,
379373
params.position.line,
380374
params.position.character,
381-
]
375+
],
376+
msg
382377
);
383-
let definitionResponse: m.ResponseMessage = {
384-
jsonrpc: c.jsonrpcVersion,
385-
id: msg.id,
386-
result,
387-
// error: code and message set in case an exception happens during the definition request.
388-
};
389-
send(definitionResponse);
378+
send(response);
390379
} else if (msg.method === p.RenameRequest.method) {
391380
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename
392381
let params = msg.params as p.RenameParams;
393382
let filePath = fileURLToPath(params.textDocument.uri);
394-
let locations: p.Location[] | null = utils.getReferencesForPosition(filePath, params.position);
383+
let locations: p.Location[] | null = utils.getReferencesForPosition(
384+
filePath,
385+
params.position
386+
);
395387
let result: WorkspaceEdit | null;
396388
if (locations === null) {
397389
result = null;
398390
} else {
399391
let changes: { [uri: string]: TextEdit[] } = {};
400392
locations.forEach(({ uri, range }) => {
401-
let textEdit: TextEdit = {range, newText: params.newName};
393+
let textEdit: TextEdit = { range, newText: params.newName };
402394
if (uri in changes) {
403395
changes[uri].push(textEdit);
404396
} else {
405-
changes[uri] = [textEdit]
397+
changes[uri] = [textEdit];
406398
}
407399
});
408400

409-
result = {changes};
401+
result = { changes };
410402
}
411403

412404
let renameResponse: m.ResponseMessage = {
@@ -420,7 +412,10 @@ function onMessage(msg: m.Message) {
420412
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
421413
let params = msg.params as p.ReferenceParams;
422414
let filePath = fileURLToPath(params.textDocument.uri);
423-
let result: typeof p.ReferencesRequest.type = utils.getReferencesForPosition(filePath, params.position);
415+
let result: typeof p.ReferencesRequest.type = utils.getReferencesForPosition(
416+
filePath,
417+
params.position
418+
);
424419
let definitionResponse: m.ResponseMessage = {
425420
jsonrpc: c.jsonrpcVersion,
426421
id: msg.id,
@@ -432,39 +427,31 @@ function onMessage(msg: m.Message) {
432427
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol
433428
let params = msg.params as p.DocumentSymbolParams;
434429
let filePath = fileURLToPath(params.textDocument.uri);
435-
let result: typeof p.DocumentSymbolRequest.type = utils.runAnalysisAfterSanityCheck(
430+
let response = utils.runAnalysisCommand(
436431
filePath,
437-
["documentSymbol", filePath]
432+
["documentSymbol", filePath],
433+
msg
438434
);
439-
let definitionResponse: m.ResponseMessage = {
440-
jsonrpc: c.jsonrpcVersion,
441-
id: msg.id,
442-
result,
443-
};
444-
send(definitionResponse);
435+
send(response);
445436
} else if (msg.method === p.CompletionRequest.method) {
446437
let params = msg.params as p.ReferenceParams;
447438
let filePath = fileURLToPath(params.textDocument.uri);
448439
let code = getOpenedFileContent(params.textDocument.uri);
449440
let tmpname = utils.createFileInTempDir();
450441
fs.writeFileSync(tmpname, code, { encoding: "utf-8" });
451-
let result: typeof p.CompletionRequest.type = utils.runAnalysisAfterSanityCheck(
442+
let response = utils.runAnalysisCommand(
452443
filePath,
453444
[
454445
"completion",
455446
filePath,
456447
params.position.line,
457448
params.position.character,
458449
tmpname,
459-
]
450+
],
451+
msg
460452
);
461453
fs.unlink(tmpname, () => null);
462-
let completionResponse: m.ResponseMessage = {
463-
jsonrpc: c.jsonrpcVersion,
464-
id: msg.id,
465-
result,
466-
};
467-
send(completionResponse);
454+
send(response);
468455
} else if (msg.method === p.DocumentFormattingRequest.method) {
469456
// technically, a formatting failure should reply with the error. Sadly
470457
// the LSP alert box for these error replies sucks (e.g. doesn't actually

‎server/src/utils.ts

+42-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import * as childProcess from "child_process";
33
import * as p from "vscode-languageserver-protocol";
44
import * as path from "path";
55
import * as t from "vscode-languageserver-types";
6+
import {
7+
RequestMessage,
8+
ResponseMessage,
9+
} from "vscode-languageserver-protocol";
610
import fs from "fs";
711
import * as os from "os";
812

@@ -82,13 +86,13 @@ export let findNodeBuildOfProjectRoot = (
8286

8387
type execResult =
8488
| {
85-
kind: "success";
86-
result: string;
87-
}
89+
kind: "success";
90+
result: string;
91+
}
8892
| {
89-
kind: "error";
90-
error: string;
91-
};
93+
kind: "error";
94+
error: string;
95+
};
9296
export let formatUsingValidBscNativePath = (
9397
code: string,
9498
bscNativePath: p.DocumentUri,
@@ -144,12 +148,34 @@ export let runAnalysisAfterSanityCheck = (
144148
return JSON.parse(stdout.toString());
145149
};
146150

147-
export let getReferencesForPosition = (filePath: p.DocumentUri, position: p.Position) =>
148-
runAnalysisAfterSanityCheck(filePath, ['references', filePath, position.line, position.character]);
151+
export let runAnalysisCommand = (
152+
filePath: p.DocumentUri,
153+
args: Array<any>,
154+
msg: RequestMessage
155+
) => {
156+
let result = runAnalysisAfterSanityCheck(filePath, args);
157+
let response: ResponseMessage = {
158+
jsonrpc: c.jsonrpcVersion,
159+
id: msg.id,
160+
result,
161+
};
162+
return response;
163+
};
164+
165+
export let getReferencesForPosition = (
166+
filePath: p.DocumentUri,
167+
position: p.Position
168+
) =>
169+
runAnalysisAfterSanityCheck(filePath, [
170+
"references",
171+
filePath,
172+
position.line,
173+
position.character,
174+
]);
149175

150176
export let replaceFileExtension = (filePath: string, ext: string): string => {
151177
let name = path.basename(filePath, path.extname(filePath));
152-
return path.format({ dir: path.dirname(filePath), name, ext })
178+
return path.format({ dir: path.dirname(filePath), name, ext });
153179
};
154180

155181
export let createInterfaceFileUsingValidBscExePath = (
@@ -158,13 +184,14 @@ export let createInterfaceFileUsingValidBscExePath = (
158184
bscExePath: p.DocumentUri
159185
): execResult => {
160186
try {
161-
let resiString = childProcess.execFileSync(
162-
bscExePath,
163-
["-color", "never", cmiPath]
164-
);
187+
let resiString = childProcess.execFileSync(bscExePath, [
188+
"-color",
189+
"never",
190+
cmiPath,
191+
]);
165192

166-
let resiPath = replaceFileExtension(filePath, c.resiExt)
167-
fs.writeFileSync(resiPath, resiString, { encoding: "utf-8"});
193+
let resiPath = replaceFileExtension(filePath, c.resiExt);
194+
fs.writeFileSync(resiPath, resiString, { encoding: "utf-8" });
168195

169196
return {
170197
kind: "success",

0 commit comments

Comments
 (0)
Please sign in to comment.