Skip to content

Commit 6b8d49e

Browse files
authored
refactor(server): Prefer null over undefined and explicit void (#1493)
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 81b8fab commit 6b8d49e

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
@@ -354,7 +354,7 @@ export class Session {
354354
return params;
355355
}
356356

357-
private enableLanguageServiceForProject(project: ts.server.Project) {
357+
private enableLanguageServiceForProject(project: ts.server.Project): void {
358358
const {projectName} = project;
359359
if (project.isClosed()) {
360360
this.info(`Cannot enable language service for closed project ${projectName}.`);
@@ -381,7 +381,7 @@ export class Session {
381381
this.runGlobalAnalysisForNewlyLoadedProject(project);
382382
}
383383

384-
private disableLanguageServiceForProject(project: ts.server.Project, reason: string) {
384+
private disableLanguageServiceForProject(project: ts.server.Project, reason: string): void {
385385
if (!project.languageServiceEnabled) {
386386
return;
387387
}
@@ -394,7 +394,7 @@ export class Session {
394394
* Invoke the compiler for the first time so that external templates get
395395
* matched to the project they belong to.
396396
*/
397-
private runGlobalAnalysisForNewlyLoadedProject(project: ts.server.Project) {
397+
private runGlobalAnalysisForNewlyLoadedProject(project: ts.server.Project): void {
398398
if (!project.hasRoots()) {
399399
return;
400400
}
@@ -410,7 +410,7 @@ export class Session {
410410
}
411411
}
412412

413-
private handleCompilerOptionsDiagnostics(project: ts.server.Project) {
413+
private handleCompilerOptionsDiagnostics(project: ts.server.Project): void {
414414
if (!isConfiguredProject(project)) {
415415
return;
416416
}
@@ -572,7 +572,7 @@ export class Session {
572572
* an inferred project.
573573
* @param scriptInfo
574574
*/
575-
getDefaultProjectForScriptInfo(scriptInfo: ts.server.ScriptInfo): ts.server.Project|undefined {
575+
getDefaultProjectForScriptInfo(scriptInfo: ts.server.ScriptInfo): ts.server.Project|null {
576576
let project = this.projectService.getDefaultProjectForFile(
577577
scriptInfo.fileName,
578578
// ensureProject tries to find a default project for the scriptInfo if
@@ -589,11 +589,11 @@ export class Session {
589589
if (!configFileName) {
590590
// Failed to find a config file. There is nothing we could do.
591591
this.error(`No config file for ${scriptInfo.fileName}`);
592-
return;
592+
return null;
593593
}
594594
project = this.projectService.findProject(configFileName);
595595
if (!project) {
596-
return;
596+
return null;
597597
}
598598
scriptInfo.detachAllProjects();
599599
scriptInfo.attachToProject(project);
@@ -698,7 +698,7 @@ export class Session {
698698
* Creates an external project with the same config path as `project` so that TypeScript keeps the
699699
* project open when navigating away from `html` files.
700700
*/
701-
private createExternalProject(project: ts.server.Project) {
701+
private createExternalProject(project: ts.server.Project): void {
702702
if (isConfiguredProject(project) &&
703703
!this.configuredProjToExternalProj.has(project.projectName)) {
704704
const extProjectName = `${project.projectName}-external`;
@@ -727,7 +727,7 @@ export class Session {
727727
* checks if there are no longer any open files in any external project. If there
728728
* aren't, we also close the external project that was created.
729729
*/
730-
private closeOrphanedExternalProjects() {
730+
private closeOrphanedExternalProjects(): void {
731731
for (const [configuredProjName, externalProjName] of this.configuredProjToExternalProj) {
732732
const configuredProj = this.projectService.findProject(configuredProjName);
733733
if (!configuredProj || configuredProj.isClosed()) {
@@ -748,7 +748,7 @@ export class Session {
748748
}
749749
}
750750

751-
private onDidChangeTextDocument(params: lsp.DidChangeTextDocumentParams) {
751+
private onDidChangeTextDocument(params: lsp.DidChangeTextDocumentParams): void {
752752
const {contentChanges, textDocument} = params;
753753
const filePath = uriToFilePath(textDocument.uri);
754754
if (!filePath) {
@@ -777,7 +777,7 @@ export class Session {
777777
this.requestDiagnosticsOnOpenOrChangeFile(scriptInfo.fileName, `Changing ${filePath}`);
778778
}
779779

780-
private onDidSaveTextDocument(params: lsp.DidSaveTextDocumentParams) {
780+
private onDidSaveTextDocument(params: lsp.DidSaveTextDocumentParams): void {
781781
const {text, textDocument} = params;
782782
const filePath = uriToFilePath(textDocument.uri);
783783
if (!filePath) {
@@ -795,51 +795,51 @@ export class Session {
795795
}
796796
}
797797

798-
private onDefinition(params: lsp.TextDocumentPositionParams): lsp.LocationLink[]|undefined {
798+
private onDefinition(params: lsp.TextDocumentPositionParams): lsp.LocationLink[]|null {
799799
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
800800
if (lsInfo === null) {
801-
return;
801+
return null;
802802
}
803803
const {languageService, scriptInfo} = lsInfo;
804804
const offset = lspPositionToTsPosition(scriptInfo, params.position);
805805
const definition = languageService.getDefinitionAndBoundSpan(scriptInfo.fileName, offset);
806806
if (!definition || !definition.definitions) {
807-
return;
807+
return null;
808808
}
809809
const originSelectionRange = tsTextSpanToLspRange(scriptInfo, definition.textSpan);
810810
return this.tsDefinitionsToLspLocationLinks(definition.definitions, originSelectionRange);
811811
}
812812

813-
private onTypeDefinition(params: lsp.TextDocumentPositionParams): lsp.LocationLink[]|undefined {
813+
private onTypeDefinition(params: lsp.TextDocumentPositionParams): lsp.LocationLink[]|null {
814814
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
815815
if (lsInfo === null) {
816-
return;
816+
return null;
817817
}
818818
const {languageService, scriptInfo} = lsInfo;
819819
const offset = lspPositionToTsPosition(scriptInfo, params.position);
820820
const definitions = languageService.getTypeDefinitionAtPosition(scriptInfo.fileName, offset);
821821
if (!definitions) {
822-
return;
822+
return null;
823823
}
824824
return this.tsDefinitionsToLspLocationLinks(definitions);
825825
}
826826

827-
private onRenameRequest(params: lsp.RenameParams): lsp.WorkspaceEdit|undefined {
827+
private onRenameRequest(params: lsp.RenameParams): lsp.WorkspaceEdit|null {
828828
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
829829
if (lsInfo === null) {
830-
return;
830+
return null;
831831
}
832832
const {languageService, scriptInfo} = lsInfo;
833833
const project = this.getDefaultProjectForScriptInfo(scriptInfo);
834-
if (project === undefined || this.renameDisabledProjects.has(project)) {
835-
return;
834+
if (project === null || this.renameDisabledProjects.has(project)) {
835+
return null;
836836
}
837837

838838
const offset = lspPositionToTsPosition(scriptInfo, params.position);
839839
const renameLocations = languageService.findRenameLocations(
840840
scriptInfo.fileName, offset, /*findInStrings*/ false, /*findInComments*/ false);
841841
if (renameLocations === undefined) {
842-
return;
842+
return null;
843843
}
844844

845845
const changes = renameLocations.reduce((changes, location) => {
@@ -869,7 +869,7 @@ export class Session {
869869
}
870870
const {languageService, scriptInfo} = lsInfo;
871871
const project = this.getDefaultProjectForScriptInfo(scriptInfo);
872-
if (project === undefined || this.renameDisabledProjects.has(project)) {
872+
if (project === null || this.renameDisabledProjects.has(project)) {
873873
return null;
874874
}
875875

@@ -885,16 +885,16 @@ export class Session {
885885
};
886886
}
887887

888-
private onReferences(params: lsp.TextDocumentPositionParams): lsp.Location[]|undefined {
888+
private onReferences(params: lsp.TextDocumentPositionParams): lsp.Location[]|null {
889889
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
890890
if (lsInfo === null) {
891-
return;
891+
return null;
892892
}
893893
const {languageService, scriptInfo} = lsInfo;
894894
const offset = lspPositionToTsPosition(scriptInfo, params.position);
895895
const references = languageService.getReferencesAtPosition(scriptInfo.fileName, offset);
896896
if (references === undefined) {
897-
return;
897+
return null;
898898
}
899899
return references.map(ref => {
900900
const scriptInfo = this.projectService.getScriptInfo(ref.fileName);
@@ -962,16 +962,16 @@ export class Session {
962962
};
963963
}
964964

965-
private onHover(params: lsp.TextDocumentPositionParams) {
965+
private onHover(params: lsp.TextDocumentPositionParams): lsp.Hover|null {
966966
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
967967
if (lsInfo === null) {
968-
return;
968+
return null;
969969
}
970970
const {languageService, scriptInfo} = lsInfo;
971971
const offset = lspPositionToTsPosition(scriptInfo, params.position);
972972
const info = languageService.getQuickInfoAtPosition(scriptInfo.fileName, offset);
973973
if (!info) {
974-
return;
974+
return null;
975975
}
976976
const {kind, kindModifiers, textSpan, displayParts, documentation} = info;
977977
let desc = kindModifiers ? kindModifiers + ' ' : '';
@@ -997,10 +997,10 @@ export class Session {
997997
};
998998
}
999999

1000-
private onCompletion(params: lsp.CompletionParams) {
1000+
private onCompletion(params: lsp.CompletionParams): lsp.CompletionItem[]|null {
10011001
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
10021002
if (lsInfo === null) {
1003-
return;
1003+
return null;
10041004
}
10051005
const {languageService, scriptInfo} = lsInfo;
10061006
const offset = lspPositionToTsPosition(scriptInfo, params.position);
@@ -1016,7 +1016,7 @@ export class Session {
10161016
const completions =
10171017
languageService.getCompletionsAtPosition(scriptInfo.fileName, offset, options);
10181018
if (!completions) {
1019-
return;
1019+
return null;
10201020
}
10211021
const clientSupportsInsertReplaceCompletion =
10221022
this.clientCapabilities.textDocument?.completion?.completionItem?.insertReplaceSupport ??
@@ -1103,7 +1103,7 @@ export class Session {
11031103
/**
11041104
* Start listening on the input stream for messages to process.
11051105
*/
1106-
listen() {
1106+
listen(): void {
11071107
this.connection.listen();
11081108
}
11091109

0 commit comments

Comments
 (0)