Skip to content

Commit f33b910

Browse files
committed
Enable 'require-await', 'no-misused-promises' and 'no-floating-promises'
Except where we have to ignore them individually.
1 parent b0b729a commit f33b910

28 files changed

+183
-179
lines changed

.eslintrc.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
"parser": "@typescript-eslint/parser",
1212
"parserOptions": {
1313
"ecmaVersion": "latest",
14-
"sourceType": "module"
14+
"sourceType": "module",
15+
"tsconfigRootDir": ".",
16+
"project": [
17+
"./tsconfig.json"
18+
]
1519
},
1620
"plugins": [
1721
"@typescript-eslint",
@@ -39,6 +43,15 @@
3943
"argsIgnorePattern": "^_"
4044
}
4145
],
46+
"@typescript-eslint/require-await": [
47+
"error"
48+
],
49+
"@typescript-eslint/no-misused-promises": [
50+
"error"
51+
],
52+
"@typescript-eslint/no-floating-promises": [
53+
"error"
54+
],
4255
"header/header": [
4356
2,
4457
"line",

src/features/CodeActions.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export class CodeActionsFeature implements vscode.Disposable {
1212
constructor(private log: ILogger) {
1313
// TODO: What type is `edit`, what uses this, and is it working?
1414
// eslint-disable-next-line @typescript-eslint/no-explicit-any
15-
this.applyEditsCommand = vscode.commands.registerCommand("PowerShell.ApplyCodeActionEdits", (edit: any) => {
16-
Window.activeTextEditor?.edit((editBuilder) => {
15+
this.applyEditsCommand = vscode.commands.registerCommand("PowerShell.ApplyCodeActionEdits", async (edit: any) => {
16+
await Window.activeTextEditor?.edit((editBuilder) => {
1717
editBuilder.replace(
1818
new vscode.Range(
1919
edit.StartLineNumber - 1,
@@ -25,8 +25,8 @@ export class CodeActionsFeature implements vscode.Disposable {
2525
});
2626

2727
this.showDocumentationCommand =
28-
vscode.commands.registerCommand("PowerShell.ShowCodeActionDocumentation", (ruleName: string) => {
29-
this.showRuleDocumentation(ruleName);
28+
vscode.commands.registerCommand("PowerShell.ShowCodeActionDocumentation", async (ruleName: string) => {
29+
await this.showRuleDocumentation(ruleName);
3030
});
3131
}
3232

@@ -35,7 +35,7 @@ export class CodeActionsFeature implements vscode.Disposable {
3535
this.showDocumentationCommand.dispose();
3636
}
3737

38-
public showRuleDocumentation(ruleId: string) {
38+
public async showRuleDocumentation(ruleId: string) {
3939
const pssaDocBaseURL = "https://docs.microsoft.com/powershell/utility-modules/psscriptanalyzer/rules/";
4040

4141
if (!ruleId) {
@@ -47,6 +47,6 @@ export class CodeActionsFeature implements vscode.Disposable {
4747
ruleId = ruleId.substr(2);
4848
}
4949

50-
vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(pssaDocBaseURL + `${ruleId}`));
50+
await vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(pssaDocBaseURL + `${ruleId}`));
5151
}
5252
}

src/features/CustomViews.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,10 @@ class PowerShellContentProvider implements vscode.TextDocumentContentProvider {
9999
public closeView(id: string) {
100100
const uriString = this.getUri(id);
101101

102-
vscode.workspace.textDocuments.some((doc) => {
102+
vscode.workspace.textDocuments.some(async (doc) => {
103103
if (doc.uri.toString() === uriString) {
104-
vscode.window
105-
.showTextDocument(doc)
106-
.then((_) => vscode.commands.executeCommand("workbench.action.closeActiveEditor"));
107-
104+
await vscode.window.showTextDocument(doc);
105+
await vscode.commands.executeCommand("workbench.action.closeActiveEditor");
108106
return true;
109107
}
110108

src/features/DebugSession.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export class DebugSessionFeature extends LanguageClientConsumer
8080
: this.sessionManager.getSessionDetails();
8181

8282
if (sessionDetails === undefined) {
83+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
8384
this.logger.writeAndShowError(`No session details available for ${session.name}`);
8485
return;
8586
}
@@ -101,15 +102,17 @@ export class DebugSessionFeature extends LanguageClientConsumer
101102
languageClient.onNotification(
102103
StartDebuggerNotificationType,
103104
// TODO: Use a named debug configuration.
104-
async () => await vscode.debug.startDebugging(undefined, {
105+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
106+
() => vscode.debug.startDebugging(undefined, {
105107
request: "launch",
106108
type: "PowerShell",
107109
name: "PowerShell: Interactive Session"
108110
})),
109111

110112
languageClient.onNotification(
111113
StopDebuggerNotificationType,
112-
async () => await vscode.debug.stopDebugging(undefined))
114+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
115+
() => vscode.debug.stopDebugging(undefined))
113116
];
114117
}
115118

@@ -188,7 +191,7 @@ export class DebugSessionFeature extends LanguageClientConsumer
188191

189192
if (config.script === "${file}" || config.script === "${relativeFile}") {
190193
if (vscode.window.activeTextEditor === undefined) {
191-
vscode.window.showErrorMessage("To debug the 'Current File', you must first open a PowerShell script file in the editor.");
194+
await vscode.window.showErrorMessage("To debug the 'Current File', you must first open a PowerShell script file in the editor.");
192195
return undefined;
193196
}
194197
config.current_document = true;
@@ -214,7 +217,7 @@ export class DebugSessionFeature extends LanguageClientConsumer
214217
} else if (config.request === "launch") {
215218
resolvedConfig = await this.resolveLaunchDebugConfiguration(config);
216219
} else {
217-
vscode.window.showErrorMessage(`The request type was invalid: '${config.request}'`);
220+
await vscode.window.showErrorMessage(`The request type was invalid: '${config.request}'`);
218221
return null;
219222
}
220223

@@ -235,7 +238,7 @@ export class DebugSessionFeature extends LanguageClientConsumer
235238
if (config.current_document) {
236239
const currentDocument = vscode.window.activeTextEditor?.document;
237240
if (currentDocument?.languageId !== "powershell") {
238-
vscode.window.showErrorMessage("Please change the current document's language mode to PowerShell.");
241+
await vscode.window.showErrorMessage("Please change the current document's language mode to PowerShell.");
239242
return undefined;
240243
}
241244
}
@@ -244,13 +247,13 @@ export class DebugSessionFeature extends LanguageClientConsumer
244247
// check the document extension for everything else.
245248
if (config.untitled_document) {
246249
if (config.createTemporaryIntegratedConsole) {
247-
vscode.window.showErrorMessage("Debugging untitled files in a temporary console is not supported.");
250+
await vscode.window.showErrorMessage("Debugging untitled files in a temporary console is not supported.");
248251
return undefined;
249252
}
250253
} else if (config.script) {
251254
const ext = path.extname(config.script).toLowerCase();
252255
if (!(ext === ".ps1" || ext === ".psm1")) {
253-
vscode.window.showErrorMessage(`PowerShell does not support debugging this file type: '${path.basename(config.script)}'`);
256+
await vscode.window.showErrorMessage(`PowerShell does not support debugging this file type: '${path.basename(config.script)}'`);
254257
return undefined;
255258
}
256259
}
@@ -262,13 +265,13 @@ export class DebugSessionFeature extends LanguageClientConsumer
262265
const platformDetails = getPlatformDetails();
263266
const versionDetails = this.sessionManager.getPowerShellVersionDetails();
264267
if (versionDetails === undefined) {
265-
vscode.window.showErrorMessage(`Session version details were not found for ${config.name}`);
268+
await vscode.window.showErrorMessage(`Session version details were not found for ${config.name}`);
266269
return null;
267270
}
268271

269272
// Cross-platform attach to process was added in 6.2.0-preview.4.
270273
if (versionDetails.version < "7.0.0" && platformDetails.operatingSystem !== OperatingSystem.Windows) {
271-
vscode.window.showErrorMessage(`Attaching to a PowerShell Host Process on ${OperatingSystem[platformDetails.operatingSystem]} requires PowerShell 7.0 or higher.`);
274+
await vscode.window.showErrorMessage(`Attaching to a PowerShell Host Process on ${OperatingSystem[platformDetails.operatingSystem]} requires PowerShell 7.0 or higher.`);
272275
return undefined;
273276
}
274277

@@ -328,7 +331,7 @@ export class SpecifyScriptArgsFeature implements vscode.Disposable {
328331
// When user cancel's the input box (by pressing Esc), the text value is undefined.
329332
// Let's not blow away the previous setting.
330333
if (text !== undefined) {
331-
this.context.workspaceState.update(powerShellDbgScriptArgsKey, text);
334+
await this.context.workspaceState.update(powerShellDbgScriptArgsKey, text);
332335
}
333336
return text;
334337
}
@@ -393,6 +396,7 @@ export class PickPSHostProcessFeature extends LanguageClientConsumer {
393396
(resolve, reject) => {
394397
this.getLanguageClientResolve = resolve;
395398

399+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
396400
vscode.window
397401
.showQuickPick(
398402
["Cancel"],
@@ -411,6 +415,7 @@ export class PickPSHostProcessFeature extends LanguageClientConsumer {
411415
this.clearWaitingToken();
412416
reject();
413417

418+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
414419
vscode.window.showErrorMessage(
415420
"Attach to PowerShell host process: PowerShell session took too long to start.");
416421
}
@@ -519,6 +524,7 @@ export class PickRunspaceFeature extends LanguageClientConsumer {
519524
(resolve, reject) => {
520525
this.getLanguageClientResolve = resolve;
521526

527+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
522528
vscode.window
523529
.showQuickPick(
524530
["Cancel"],
@@ -537,6 +543,7 @@ export class PickRunspaceFeature extends LanguageClientConsumer {
537543
this.clearWaitingToken();
538544
reject();
539545

546+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
540547
vscode.window.showErrorMessage(
541548
"Attach to PowerShell host process: PowerShell session took too long to start.");
542549
}

src/features/Examples.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export class ExamplesFeature implements vscode.Disposable {
1111

1212
constructor() {
1313
this.examplesPath = vscode.Uri.file(path.resolve(__dirname, "../examples"));
14-
this.command = vscode.commands.registerCommand("PowerShell.OpenExamplesFolder", () => {
15-
vscode.commands.executeCommand("vscode.openFolder", this.examplesPath, true);
14+
this.command = vscode.commands.registerCommand("PowerShell.OpenExamplesFolder", async () => {
15+
await vscode.commands.executeCommand("vscode.openFolder", this.examplesPath, true);
1616
// Return existence of the path for testing. The `vscode.openFolder`
1717
// command should do this, but doesn't (yet).
1818
return utils.checkIfFileExists(this.examplesPath);

src/features/ExpandAlias.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class ExpandAliasFeature extends LanguageClientConsumer {
2222

2323
constructor() {
2424
super();
25-
this.command = vscode.commands.registerCommand("PowerShell.ExpandAlias", () => {
25+
this.command = vscode.commands.registerCommand("PowerShell.ExpandAlias", async () => {
2626
const editor = Window.activeTextEditor;
2727
if (editor === undefined) {
2828
return;
@@ -44,11 +44,12 @@ export class ExpandAliasFeature extends LanguageClientConsumer {
4444
range = new vscode.Range(sls.line, sls.character, sle.line, sle.character);
4545
}
4646

47-
this.languageClient?.sendRequest(ExpandAliasRequestType, { text }).then((result) => {
48-
editor.edit((editBuilder) => {
47+
const result = await this.languageClient?.sendRequest(ExpandAliasRequestType, { text });
48+
if (result !== undefined) {
49+
await editor.edit((editBuilder) => {
4950
editBuilder.replace(range, result.text);
5051
});
51-
});
52+
}
5253
});
5354
}
5455

src/features/ExtensionCommands.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
235235

236236
this.languageClient.onRequest(
237237
InsertTextRequestType,
238+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
238239
(details) => this.insertText(details)),
239240

240241
this.languageClient.onRequest(
@@ -280,6 +281,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
280281
// We check to see if they have TrueClear on. If not, no-op because the
281282
// overriden Clear-Host already calls [System.Console]::Clear()
282283
if (Settings.load().integratedConsole.forceClearScrollbackBuffer) {
284+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
283285
vscode.commands.executeCommand("workbench.action.terminal.clear");
284286
}
285287
})
@@ -309,8 +311,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
309311
private async showExtensionCommands(client: LanguageClient): Promise<void> {
310312
// If no extension commands are available, show a message
311313
if (this.extensionCommands.length === 0) {
312-
vscode.window.showInformationMessage(
313-
"No extension commands have been loaded into the current session.");
314+
await vscode.window.showInformationMessage("No extension commands have been loaded into the current session.");
314315
return;
315316
}
316317

@@ -329,12 +330,12 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
329330
return this.onCommandSelected(selectedCommand, client);
330331
}
331332

332-
private onCommandSelected(
333+
private async onCommandSelected(
333334
chosenItem: IExtensionCommandQuickPickItem | undefined,
334335
client: LanguageClient | undefined) {
335336

336337
if (chosenItem !== undefined) {
337-
client?.sendRequest(
338+
await client?.sendRequest(
338339
InvokeExtensionCommandRequestType,
339340
{
340341
name: chosenItem.command.name,
@@ -343,7 +344,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
343344
}
344345
}
345346

346-
private insertText(details: IInsertTextRequestArguments): EditorOperationResponse {
347+
private async insertText(details: IInsertTextRequestArguments): Promise<EditorOperationResponse> {
347348
const edit = new vscode.WorkspaceEdit();
348349

349350
edit.set(
@@ -359,7 +360,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
359360
],
360361
);
361362

362-
vscode.workspace.applyEdit(edit);
363+
await vscode.workspace.applyEdit(edit);
363364

364365
return EditorOperationResponse.Completed;
365366
}
@@ -461,7 +462,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
461462
if (!saveFileDetails.newPath) {
462463
// TODO: Create a class handle vscode warnings and errors so we can warn easily
463464
// without logging
464-
this.log.writeAndShowWarning(
465+
await this.log.writeAndShowWarning(
465466
"Cannot save untitled file. Try SaveAs(\"path/to/file.ps1\") instead.");
466467
return EditorOperationResponse.Completed;
467468
}
@@ -472,7 +473,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
472473
} else {
473474
// In fresh contexts, workspaceFolders is not defined...
474475
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) {
475-
this.log.writeAndShowWarning("Cannot save file to relative path: no workspaces are open. " +
476+
await this.log.writeAndShowWarning("Cannot save file to relative path: no workspaces are open. " +
476477
"Try saving to an absolute path, or open a workspace.");
477478
return EditorOperationResponse.Completed;
478479
}
@@ -481,7 +482,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
481482
const workspaceRootUri = vscode.workspace.workspaceFolders[0].uri;
482483
// We don't support saving to a non-file URI-schemed workspace
483484
if (workspaceRootUri.scheme !== "file") {
484-
this.log.writeAndShowWarning(
485+
await this.log.writeAndShowWarning(
485486
"Cannot save untitled file to a relative path in an untitled workspace. " +
486487
"Try saving to an absolute path or opening a workspace folder.");
487488
return EditorOperationResponse.Completed;
@@ -521,15 +522,15 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
521522
vscode.Uri.file(destinationAbsolutePath),
522523
Buffer.from(oldDocument.getText()));
523524
} catch (e) {
524-
this.log.writeAndShowWarning(`<${ExtensionCommandsFeature.name}>: ` +
525+
await this.log.writeAndShowWarning(`<${ExtensionCommandsFeature.name}>: ` +
525526
`Unable to save file to path '${destinationAbsolutePath}': ${e}`);
526527
return;
527528
}
528529

529530
// Finally open the new document
530531
const newFileUri = vscode.Uri.file(destinationAbsolutePath);
531532
const newFile = await vscode.workspace.openTextDocument(newFileUri);
532-
vscode.window.showTextDocument(newFile, { preview: true });
533+
await vscode.window.showTextDocument(newFile, { preview: true });
533534
}
534535

535536
private normalizeFilePath(filePath: string): string {

0 commit comments

Comments
 (0)