Skip to content

Commit ec7b8f1

Browse files
committed
Allow unawaited promises explicitly with void
And refactor a bit of logging!
1 parent fac6e1a commit ec7b8f1

16 files changed

+146
-143
lines changed

.eslintrc.json

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
"error",
3737
"always"
3838
],
39+
"@typescript-eslint/no-floating-promises": [
40+
"error",
41+
{
42+
"ignoreVoid": true
43+
}
44+
],
3945
"@typescript-eslint/no-non-null-assertion": [
4046
"off"
4147
],

src/features/Console.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@ export class ConsoleFeature extends LanguageClientConsumer {
170170
private commands: vscode.Disposable[];
171171
private handlers: vscode.Disposable[] = [];
172172

173-
constructor(private log: Logger) {
173+
constructor(private logger: Logger) {
174174
super();
175175
this.commands = [
176176
vscode.commands.registerCommand("PowerShell.RunSelection", async () => {
177177
if (vscode.window.activeTerminal &&
178178
vscode.window.activeTerminal.name !== "PowerShell Extension") {
179-
this.log.write("PowerShell Extension Terminal is not active! Running in current terminal using 'runSelectedText'");
179+
this.logger.write("PowerShell Extension Terminal is not active! Running in current terminal using 'runSelectedText'");
180180
await vscode.commands.executeCommand("workbench.action.terminal.runSelectedText");
181181

182182
// We need to honor the focusConsoleOnExecute setting here too. However, the boolean that `show`

src/features/CustomViews.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,8 @@ class PowerShellContentProvider implements vscode.TextDocumentContentProvider {
101101

102102
vscode.workspace.textDocuments.some((doc) => {
103103
if (doc.uri.toString() === uriString) {
104-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
105-
vscode.window.showTextDocument(doc);
106-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
107-
vscode.commands.executeCommand("workbench.action.closeActiveEditor");
104+
void vscode.window.showTextDocument(doc);
105+
void vscode.commands.executeCommand("workbench.action.closeActiveEditor");
108106
return true;
109107
}
110108

src/features/DebugSession.ts

+16-26
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ export class DebugSessionFeature extends LanguageClientConsumer
8181
: this.sessionManager.getSessionDetails();
8282

8383
if (sessionDetails === undefined) {
84-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
85-
this.logger.writeAndShowError(`No session details available for ${session.name}`);
84+
void this.logger.writeAndShowError(`PowerShell session details not available for ${session.name}`);
8685
return;
8786
}
8887

@@ -103,17 +102,15 @@ export class DebugSessionFeature extends LanguageClientConsumer
103102
languageClient.onNotification(
104103
StartDebuggerNotificationType,
105104
// TODO: Use a named debug configuration.
106-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
107-
() => vscode.debug.startDebugging(undefined, {
105+
() => void vscode.debug.startDebugging(undefined, {
108106
request: "launch",
109107
type: "PowerShell",
110108
name: "PowerShell: Interactive Session"
111109
})),
112110

113111
languageClient.onNotification(
114112
StopDebuggerNotificationType,
115-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
116-
() => vscode.debug.stopDebugging(undefined))
113+
() => void vscode.debug.stopDebugging(undefined))
117114
];
118115
}
119116

@@ -192,7 +189,7 @@ export class DebugSessionFeature extends LanguageClientConsumer
192189

193190
if (config.script === "${file}" || config.script === "${relativeFile}") {
194191
if (vscode.window.activeTextEditor === undefined) {
195-
await vscode.window.showErrorMessage("To debug the 'Current File', you must first open a PowerShell script file in the editor.");
192+
void this.logger.writeAndShowError("To debug the 'Current File', you must first open a PowerShell script file in the editor.");
196193
return undefined;
197194
}
198195
config.current_document = true;
@@ -218,7 +215,7 @@ export class DebugSessionFeature extends LanguageClientConsumer
218215
} else if (config.request === "launch") {
219216
resolvedConfig = await this.resolveLaunchDebugConfiguration(config);
220217
} else {
221-
await vscode.window.showErrorMessage(`The request type was invalid: '${config.request}'`);
218+
void this.logger.writeAndShowError(`PowerShell debug configuration's request type was invalid: '${config.request}'.`);
222219
return null;
223220
}
224221

@@ -267,13 +264,13 @@ export class DebugSessionFeature extends LanguageClientConsumer
267264
const platformDetails = getPlatformDetails();
268265
const versionDetails = this.sessionManager.getPowerShellVersionDetails();
269266
if (versionDetails === undefined) {
270-
await vscode.window.showErrorMessage(`Session version details were not found for ${config.name}`);
267+
void this.logger.writeAndShowError(`PowerShell session version details were not found for '${config.name}'.`);
271268
return null;
272269
}
273270

274271
// Cross-platform attach to process was added in 6.2.0-preview.4.
275272
if (versionDetails.version < "7.0.0" && platformDetails.operatingSystem !== OperatingSystem.Windows) {
276-
await vscode.window.showErrorMessage(`Attaching to a PowerShell Host Process on ${OperatingSystem[platformDetails.operatingSystem]} requires PowerShell 7.0 or higher.`);
273+
void this.logger.writeAndShowError(`Attaching to a PowerShell Host Process on ${OperatingSystem[platformDetails.operatingSystem]} requires PowerShell 7.0 or higher.`);
277274
return undefined;
278275
}
279276

@@ -306,10 +303,9 @@ export class SpecifyScriptArgsFeature implements vscode.Disposable {
306303
constructor(context: vscode.ExtensionContext) {
307304
this.context = context;
308305

309-
this.command =
310-
vscode.commands.registerCommand("PowerShell.SpecifyScriptArgs", () => {
311-
return this.specifyScriptArguments();
312-
});
306+
this.command = vscode.commands.registerCommand("PowerShell.SpecifyScriptArgs", () => {
307+
return this.specifyScriptArguments();
308+
});
313309
}
314310

315311
public dispose() {
@@ -363,7 +359,7 @@ export class PickPSHostProcessFeature extends LanguageClientConsumer {
363359
private waitingForClientToken?: vscode.CancellationTokenSource;
364360
private getLanguageClientResolve?: (value: LanguageClient) => void;
365361

366-
constructor() {
362+
constructor(private logger: Logger) {
367363
super();
368364

369365
this.command =
@@ -398,7 +394,6 @@ export class PickPSHostProcessFeature extends LanguageClientConsumer {
398394
(resolve, reject) => {
399395
this.getLanguageClientResolve = resolve;
400396

401-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
402397
vscode.window
403398
.showQuickPick(
404399
["Cancel"],
@@ -409,17 +404,15 @@ export class PickPSHostProcessFeature extends LanguageClientConsumer {
409404
this.clearWaitingToken();
410405
reject();
411406
}
412-
});
407+
}, undefined);
413408

414409
// Cancel the loading prompt after 60 seconds
415410
setTimeout(() => {
416411
if (this.waitingForClientToken) {
417412
this.clearWaitingToken();
418413
reject();
419414

420-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
421-
vscode.window.showErrorMessage(
422-
"Attach to PowerShell host process: PowerShell session took too long to start.");
415+
void this.logger.writeAndShowError("Attach to PowerShell host process: PowerShell session took too long to start.");
423416
}
424417
}, 60000);
425418
},
@@ -492,7 +485,7 @@ export class PickRunspaceFeature extends LanguageClientConsumer {
492485
private waitingForClientToken?: vscode.CancellationTokenSource;
493486
private getLanguageClientResolve?: (value: LanguageClient) => void;
494487

495-
constructor() {
488+
constructor(private logger: Logger) {
496489
super();
497490
this.command =
498491
vscode.commands.registerCommand("PowerShell.PickRunspace", (processId) => {
@@ -526,7 +519,6 @@ export class PickRunspaceFeature extends LanguageClientConsumer {
526519
(resolve, reject) => {
527520
this.getLanguageClientResolve = resolve;
528521

529-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
530522
vscode.window
531523
.showQuickPick(
532524
["Cancel"],
@@ -537,17 +529,15 @@ export class PickRunspaceFeature extends LanguageClientConsumer {
537529
this.clearWaitingToken();
538530
reject();
539531
}
540-
});
532+
}, undefined);
541533

542534
// Cancel the loading prompt after 60 seconds
543535
setTimeout(() => {
544536
if (this.waitingForClientToken) {
545537
this.clearWaitingToken();
546538
reject();
547539

548-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
549-
vscode.window.showErrorMessage(
550-
"Attach to PowerShell host process: PowerShell session took too long to start.");
540+
void this.logger.writeAndShowError("Attach to PowerShell host process: PowerShell session took too long to start.");
551541
}
552542
}, 60000);
553543
},

src/features/ExtensionCommands.ts

+19-22
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
149149
private handlers: vscode.Disposable[] = [];
150150
private extensionCommands: IExtensionCommand[] = [];
151151

152-
constructor(private log: Logger) {
152+
constructor(private logger: Logger) {
153153
super();
154154
this.commands = [
155155
vscode.commands.registerCommand("PowerShell.ShowAdditionalCommands", async () => {
@@ -216,7 +216,6 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
216216

217217
this.languageClient.onRequest(
218218
InsertTextRequestType,
219-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
220219
(details) => this.insertText(details)),
221220

222221
this.languageClient.onRequest(
@@ -262,8 +261,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
262261
// We check to see if they have TrueClear on. If not, no-op because the
263262
// overriden Clear-Host already calls [System.Console]::Clear()
264263
if (Settings.load().integratedConsole.forceClearScrollbackBuffer) {
265-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
266-
vscode.commands.executeCommand("workbench.action.terminal.clear");
264+
void vscode.commands.executeCommand("workbench.action.terminal.clear");
267265
}
268266
})
269267
];
@@ -292,7 +290,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
292290
private async showExtensionCommands(client: LanguageClient): Promise<void> {
293291
// If no extension commands are available, show a message
294292
if (this.extensionCommands.length === 0) {
295-
await vscode.window.showInformationMessage("No extension commands have been loaded into the current session.");
293+
void this.logger.writeAndShowInformation("No extension commands have been loaded into the current session.");
296294
return;
297295
}
298296

@@ -364,10 +362,10 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
364362
};
365363
}
366364

367-
private newFile(): Thenable<EditorOperationResponse> {
368-
return vscode.workspace.openTextDocument({ content: "" })
369-
.then((doc) => vscode.window.showTextDocument(doc))
370-
.then((_) => EditorOperationResponse.Completed);
365+
private async newFile(): Promise<EditorOperationResponse> {
366+
const doc = await vscode.workspace.openTextDocument({ content: "" });
367+
await vscode.window.showTextDocument(doc);
368+
return EditorOperationResponse.Completed;
371369
}
372370

373371
private openFile(openFileDetails: IOpenFileDetails): Thenable<EditorOperationResponse> {
@@ -416,7 +414,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
416414
case "file": {
417415
// If the file to save can't be found, just complete the request
418416
if (!this.findTextDocument(this.normalizeFilePath(currentFileUri.fsPath))) {
419-
await this.log.writeAndShowError(`File to save not found: ${currentFileUri.fsPath}.`);
417+
void this.logger.writeAndShowError(`File to save not found: ${currentFileUri.fsPath}.`);
420418
return EditorOperationResponse.Completed;
421419
}
422420

@@ -443,8 +441,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
443441
if (!saveFileDetails.newPath) {
444442
// TODO: Create a class handle vscode warnings and errors so we can warn easily
445443
// without logging
446-
await this.log.writeAndShowWarning(
447-
"Cannot save untitled file. Try SaveAs(\"path/to/file.ps1\") instead.");
444+
void this.logger.writeAndShowWarning("Cannot save untitled file. Try SaveAs(\"path/to/file.ps1\") instead.");
448445
return EditorOperationResponse.Completed;
449446
}
450447

@@ -454,7 +451,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
454451
} else {
455452
// In fresh contexts, workspaceFolders is not defined...
456453
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) {
457-
await this.log.writeAndShowWarning("Cannot save file to relative path: no workspaces are open. " +
454+
void this.logger.writeAndShowWarning("Cannot save file to relative path: no workspaces are open. " +
458455
"Try saving to an absolute path, or open a workspace.");
459456
return EditorOperationResponse.Completed;
460457
}
@@ -463,7 +460,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
463460
const workspaceRootUri = vscode.workspace.workspaceFolders[0].uri;
464461
// We don't support saving to a non-file URI-schemed workspace
465462
if (workspaceRootUri.scheme !== "file") {
466-
await this.log.writeAndShowWarning(
463+
void this.logger.writeAndShowWarning(
467464
"Cannot save untitled file to a relative path in an untitled workspace. " +
468465
"Try saving to an absolute path or opening a workspace folder.");
469466
return EditorOperationResponse.Completed;
@@ -475,7 +472,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
475472
default: {
476473
// Other URI schemes are not supported
477474
const msg = JSON.stringify(saveFileDetails);
478-
this.log.writeVerbose(
475+
this.logger.writeVerbose(
479476
`<${ExtensionCommandsFeature.name}>: Saving a document with scheme '${currentFileUri.scheme}' ` +
480477
`is currently unsupported. Message: '${msg}'`);
481478
return EditorOperationResponse.Completed; }
@@ -503,7 +500,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
503500
vscode.Uri.file(destinationAbsolutePath),
504501
Buffer.from(oldDocument.getText()));
505502
} catch (e) {
506-
await this.log.writeAndShowWarning(`<${ExtensionCommandsFeature.name}>: ` +
503+
void this.logger.writeAndShowWarning(`<${ExtensionCommandsFeature.name}>: ` +
507504
`Unable to save file to path '${destinationAbsolutePath}': ${e}`);
508505
return;
509506
}
@@ -572,18 +569,18 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
572569
return EditorOperationResponse.Completed;
573570
}
574571

575-
private async showInformationMessage(message: string): Promise<EditorOperationResponse> {
576-
await vscode.window.showInformationMessage(message);
572+
private showInformationMessage(message: string): EditorOperationResponse {
573+
void this.logger.writeAndShowInformation(message);
577574
return EditorOperationResponse.Completed;
578575
}
579576

580-
private async showErrorMessage(message: string): Promise<EditorOperationResponse> {
581-
await vscode.window.showErrorMessage(message);
577+
private showErrorMessage(message: string): EditorOperationResponse {
578+
void this.logger.writeAndShowError(message);
582579
return EditorOperationResponse.Completed;
583580
}
584581

585-
private async showWarningMessage(message: string): Promise<EditorOperationResponse> {
586-
await vscode.window.showWarningMessage(message);
582+
private showWarningMessage(message: string): EditorOperationResponse {
583+
void this.logger.writeAndShowWarning(message);
587584
return EditorOperationResponse.Completed;
588585
}
589586

src/features/ExternalApi.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class ExternalApiFeature extends LanguageClientConsumer implements IPower
3939
constructor(
4040
private extensionContext: vscode.ExtensionContext,
4141
private sessionManager: SessionManager,
42-
private log: Logger) {
42+
private logger: Logger) {
4343
super();
4444
}
4545

@@ -57,13 +57,13 @@ export class ExternalApiFeature extends LanguageClientConsumer implements IPower
5757
string session uuid
5858
*/
5959
public registerExternalExtension(id: string, apiVersion = "v1"): string {
60-
this.log.writeDiagnostic(`Registering extension '${id}' for use with API version '${apiVersion}'.`);
60+
this.logger.writeDiagnostic(`Registering extension '${id}' for use with API version '${apiVersion}'.`);
6161

6262
// eslint-disable-next-line @typescript-eslint/no-unused-vars
6363
for (const [_name, externalExtension] of ExternalApiFeature.registeredExternalExtension) {
6464
if (externalExtension.id === id) {
6565
const message = `The extension '${id}' is already registered.`;
66-
this.log.writeWarning(message);
66+
this.logger.writeWarning(message);
6767
throw new Error(message);
6868
}
6969
}
@@ -99,7 +99,7 @@ export class ExternalApiFeature extends LanguageClientConsumer implements IPower
9999
true if it worked, otherwise throws an error.
100100
*/
101101
public unregisterExternalExtension(uuid = ""): boolean {
102-
this.log.writeDiagnostic(`Unregistering extension with session UUID: ${uuid}`);
102+
this.logger.writeDiagnostic(`Unregistering extension with session UUID: ${uuid}`);
103103
if (!ExternalApiFeature.registeredExternalExtension.delete(uuid)) {
104104
throw new Error(`No extension registered with session UUID: ${uuid}`);
105105
}
@@ -136,7 +136,7 @@ export class ExternalApiFeature extends LanguageClientConsumer implements IPower
136136
*/
137137
public async getPowerShellVersionDetails(uuid = ""): Promise<IExternalPowerShellDetails> {
138138
const extension = this.getRegisteredExtension(uuid);
139-
this.log.writeDiagnostic(`Extension '${extension.id}' called 'getPowerShellVersionDetails'`);
139+
this.logger.writeDiagnostic(`Extension '${extension.id}' called 'getPowerShellVersionDetails'`);
140140

141141
await this.sessionManager.waitUntilStarted();
142142
const versionDetails = this.sessionManager.getPowerShellVersionDetails();
@@ -164,7 +164,7 @@ export class ExternalApiFeature extends LanguageClientConsumer implements IPower
164164
*/
165165
public async waitUntilStarted(uuid = ""): Promise<void> {
166166
const extension = this.getRegisteredExtension(uuid);
167-
this.log.writeDiagnostic(`Extension '${extension.id}' called 'waitUntilStarted'`);
167+
this.logger.writeDiagnostic(`Extension '${extension.id}' called 'waitUntilStarted'`);
168168
await this.sessionManager.waitUntilStarted();
169169
}
170170

0 commit comments

Comments
 (0)