Skip to content

Commit 8e752ca

Browse files
committed
refactor(server): Prefer over and explicit
Update return types of functions to: * Explicitly have `void` as the return type * Prefer `null` over `undefined` The LSP spec always uses `null` as the return types: https://microsoft.github.io/language-server-protocol/specifications/specification-current/ In fact, if the server is configured to return `undefined`, it will still be `null` after coming through the LSP connection. To avoid any confusion, we should just always use `null`.
1 parent 04b215b commit 8e752ca

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

server/src/session.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export class Session {
332332
return params;
333333
}
334334

335-
private enableLanguageServiceForProject(project: ts.server.Project) {
335+
private enableLanguageServiceForProject(project: ts.server.Project): void {
336336
const {projectName} = project;
337337
if (project.isClosed()) {
338338
this.info(`Cannot enable language service for closed project ${projectName}.`);
@@ -359,7 +359,7 @@ export class Session {
359359
this.runGlobalAnalysisForNewlyLoadedProject(project);
360360
}
361361

362-
private disableLanguageServiceForProject(project: ts.server.Project, reason: string) {
362+
private disableLanguageServiceForProject(project: ts.server.Project, reason: string): void {
363363
if (!project.languageServiceEnabled) {
364364
return;
365365
}
@@ -372,7 +372,7 @@ export class Session {
372372
* Invoke the compiler for the first time so that external templates get
373373
* matched to the project they belong to.
374374
*/
375-
private runGlobalAnalysisForNewlyLoadedProject(project: ts.server.Project) {
375+
private runGlobalAnalysisForNewlyLoadedProject(project: ts.server.Project): void {
376376
if (!project.hasRoots()) {
377377
return;
378378
}
@@ -388,7 +388,7 @@ export class Session {
388388
}
389389
}
390390

391-
private handleCompilerOptionsDiagnostics(project: ts.server.Project) {
391+
private handleCompilerOptionsDiagnostics(project: ts.server.Project): void {
392392
if (!isConfiguredProject(project)) {
393393
return;
394394
}
@@ -550,7 +550,7 @@ export class Session {
550550
* an inferred project.
551551
* @param scriptInfo
552552
*/
553-
getDefaultProjectForScriptInfo(scriptInfo: ts.server.ScriptInfo): ts.server.Project|undefined {
553+
getDefaultProjectForScriptInfo(scriptInfo: ts.server.ScriptInfo): ts.server.Project|null {
554554
let project = this.projectService.getDefaultProjectForFile(
555555
scriptInfo.fileName,
556556
// ensureProject tries to find a default project for the scriptInfo if
@@ -567,11 +567,11 @@ export class Session {
567567
if (!configFileName) {
568568
// Failed to find a config file. There is nothing we could do.
569569
this.error(`No config file for ${scriptInfo.fileName}`);
570-
return;
570+
return null;
571571
}
572572
project = this.projectService.findProject(configFileName);
573573
if (!project) {
574-
return;
574+
return null;
575575
}
576576
scriptInfo.detachAllProjects();
577577
scriptInfo.attachToProject(project);
@@ -676,7 +676,7 @@ export class Session {
676676
* Creates an external project with the same config path as `project` so that TypeScript keeps the
677677
* project open when navigating away from `html` files.
678678
*/
679-
private createExternalProject(project: ts.server.Project) {
679+
private createExternalProject(project: ts.server.Project): void {
680680
if (isConfiguredProject(project) &&
681681
!this.configuredProjToExternalProj.has(project.projectName)) {
682682
const extProjectName = `${project.projectName}-external`;
@@ -705,7 +705,7 @@ export class Session {
705705
* checks if there are no longer any open files in any external project. If there
706706
* aren't, we also close the external project that was created.
707707
*/
708-
private closeOrphanedExternalProjects() {
708+
private closeOrphanedExternalProjects(): void {
709709
for (const [configuredProjName, externalProjName] of this.configuredProjToExternalProj) {
710710
const configuredProj = this.projectService.findProject(configuredProjName);
711711
if (!configuredProj || configuredProj.isClosed()) {
@@ -726,7 +726,7 @@ export class Session {
726726
}
727727
}
728728

729-
private onDidChangeTextDocument(params: lsp.DidChangeTextDocumentParams) {
729+
private onDidChangeTextDocument(params: lsp.DidChangeTextDocumentParams): void {
730730
const {contentChanges, textDocument} = params;
731731
const filePath = uriToFilePath(textDocument.uri);
732732
if (!filePath) {
@@ -755,7 +755,7 @@ export class Session {
755755
this.requestDiagnosticsOnOpenOrChangeFile(scriptInfo.fileName, `Changing ${filePath}`);
756756
}
757757

758-
private onDidSaveTextDocument(params: lsp.DidSaveTextDocumentParams) {
758+
private onDidSaveTextDocument(params: lsp.DidSaveTextDocumentParams): void {
759759
const {text, textDocument} = params;
760760
const filePath = uriToFilePath(textDocument.uri);
761761
if (!filePath) {
@@ -773,51 +773,51 @@ export class Session {
773773
}
774774
}
775775

776-
private onDefinition(params: lsp.TextDocumentPositionParams): lsp.LocationLink[]|undefined {
776+
private onDefinition(params: lsp.TextDocumentPositionParams): lsp.LocationLink[]|null {
777777
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
778778
if (lsInfo === null) {
779-
return;
779+
return null;
780780
}
781781
const {languageService, scriptInfo} = lsInfo;
782782
const offset = lspPositionToTsPosition(scriptInfo, params.position);
783783
const definition = languageService.getDefinitionAndBoundSpan(scriptInfo.fileName, offset);
784784
if (!definition || !definition.definitions) {
785-
return;
785+
return null;
786786
}
787787
const originSelectionRange = tsTextSpanToLspRange(scriptInfo, definition.textSpan);
788788
return this.tsDefinitionsToLspLocationLinks(definition.definitions, originSelectionRange);
789789
}
790790

791-
private onTypeDefinition(params: lsp.TextDocumentPositionParams): lsp.LocationLink[]|undefined {
791+
private onTypeDefinition(params: lsp.TextDocumentPositionParams): lsp.LocationLink[]|null {
792792
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
793793
if (lsInfo === null) {
794-
return;
794+
return null;
795795
}
796796
const {languageService, scriptInfo} = lsInfo;
797797
const offset = lspPositionToTsPosition(scriptInfo, params.position);
798798
const definitions = languageService.getTypeDefinitionAtPosition(scriptInfo.fileName, offset);
799799
if (!definitions) {
800-
return;
800+
return null;
801801
}
802802
return this.tsDefinitionsToLspLocationLinks(definitions);
803803
}
804804

805-
private onRenameRequest(params: lsp.RenameParams): lsp.WorkspaceEdit|undefined {
805+
private onRenameRequest(params: lsp.RenameParams): lsp.WorkspaceEdit|null {
806806
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
807807
if (lsInfo === null) {
808-
return;
808+
return null;
809809
}
810810
const {languageService, scriptInfo} = lsInfo;
811811
const project = this.getDefaultProjectForScriptInfo(scriptInfo);
812-
if (project === undefined || this.renameDisabledProjects.has(project)) {
813-
return;
812+
if (project === null || this.renameDisabledProjects.has(project)) {
813+
return null;
814814
}
815815

816816
const offset = lspPositionToTsPosition(scriptInfo, params.position);
817817
const renameLocations = languageService.findRenameLocations(
818818
scriptInfo.fileName, offset, /*findInStrings*/ false, /*findInComments*/ false);
819819
if (renameLocations === undefined) {
820-
return;
820+
return null;
821821
}
822822

823823
const changes = renameLocations.reduce((changes, location) => {
@@ -847,7 +847,7 @@ export class Session {
847847
}
848848
const {languageService, scriptInfo} = lsInfo;
849849
const project = this.getDefaultProjectForScriptInfo(scriptInfo);
850-
if (project === undefined || this.renameDisabledProjects.has(project)) {
850+
if (project === null || this.renameDisabledProjects.has(project)) {
851851
return null;
852852
}
853853

@@ -863,16 +863,16 @@ export class Session {
863863
};
864864
}
865865

866-
private onReferences(params: lsp.TextDocumentPositionParams): lsp.Location[]|undefined {
866+
private onReferences(params: lsp.TextDocumentPositionParams): lsp.Location[]|null {
867867
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
868868
if (lsInfo === null) {
869-
return;
869+
return null;
870870
}
871871
const {languageService, scriptInfo} = lsInfo;
872872
const offset = lspPositionToTsPosition(scriptInfo, params.position);
873873
const references = languageService.getReferencesAtPosition(scriptInfo.fileName, offset);
874874
if (references === undefined) {
875-
return;
875+
return null;
876876
}
877877
return references.map(ref => {
878878
const scriptInfo = this.projectService.getScriptInfo(ref.fileName);
@@ -940,16 +940,16 @@ export class Session {
940940
};
941941
}
942942

943-
private onHover(params: lsp.TextDocumentPositionParams) {
943+
private onHover(params: lsp.TextDocumentPositionParams): lsp.Hover|null {
944944
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
945945
if (lsInfo === null) {
946-
return;
946+
return null;
947947
}
948948
const {languageService, scriptInfo} = lsInfo;
949949
const offset = lspPositionToTsPosition(scriptInfo, params.position);
950950
const info = languageService.getQuickInfoAtPosition(scriptInfo.fileName, offset);
951951
if (!info) {
952-
return;
952+
return null;
953953
}
954954
const {kind, kindModifiers, textSpan, displayParts, documentation} = info;
955955
let desc = kindModifiers ? kindModifiers + ' ' : '';
@@ -975,10 +975,10 @@ export class Session {
975975
};
976976
}
977977

978-
private onCompletion(params: lsp.CompletionParams) {
978+
private onCompletion(params: lsp.CompletionParams): lsp.CompletionItem[]|null {
979979
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
980980
if (lsInfo === null) {
981-
return;
981+
return null;
982982
}
983983
const {languageService, scriptInfo} = lsInfo;
984984
const offset = lspPositionToTsPosition(scriptInfo, params.position);
@@ -994,7 +994,7 @@ export class Session {
994994
const completions =
995995
languageService.getCompletionsAtPosition(scriptInfo.fileName, offset, options);
996996
if (!completions) {
997-
return;
997+
return null;
998998
}
999999
const clientSupportsInsertReplaceCompletion =
10001000
this.clientCapabilities.textDocument?.completion?.completionItem?.insertReplaceSupport ??
@@ -1081,7 +1081,7 @@ export class Session {
10811081
/**
10821082
* Start listening on the input stream for messages to process.
10831083
*/
1084-
listen() {
1084+
listen(): void {
10851085
this.connection.listen();
10861086
}
10871087

0 commit comments

Comments
 (0)