Skip to content

Commit aeb44a4

Browse files
committed
Replace forEach(...) for for (_ of ...)
It's faster...it reads better.
1 parent 27a5b8e commit aeb44a4

File tree

9 files changed

+72
-70
lines changed

9 files changed

+72
-70
lines changed

src/controls/checkboxQuickPick.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ function getQuickPickItems(items: ICheckboxQuickPickItem[]): vscode.QuickPickIte
3838
const quickPickItems: vscode.QuickPickItem[] = [];
3939
quickPickItems.push({ label: confirmItemLabel, description: "" });
4040

41-
items.forEach((item) =>
41+
for (const item of items) {
4242
quickPickItems.push({
43-
label: convertToCheckBox(item),
44-
description: item.description,
45-
}));
43+
label: convertToCheckBox(item),
44+
description: item.description,
45+
});
46+
}
4647

4748
return quickPickItems;
4849
}

src/features/Console.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ function showChoicePrompt(
129129
});
130130

131131
// Select the defaults
132-
promptDetails.defaultChoices.forEach((choiceIndex) => {
133-
checkboxQuickPickItems[choiceIndex].isSelected = true;
134-
});
132+
for (const choice of promptDetails.defaultChoices) {
133+
checkboxQuickPickItems[choice].isSelected = true;
134+
};
135135

136136
resultThenable =
137137
showCheckboxQuickPick(

src/features/CustomViews.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export class CustomViewsFeature extends LanguageClientConsumer {
2222
}
2323

2424
public dispose() {
25-
this.commands.forEach((d) => d.dispose());
25+
for (const command of this.commands) {
26+
command.dispose();
27+
}
2628
}
2729

2830
public setLanguageClient(languageClient: LanguageClient) {
@@ -177,23 +179,17 @@ class HtmlContentView extends CustomView {
177179
let styleTags = "";
178180
if (this.htmlContent.styleSheetPaths &&
179181
this.htmlContent.styleSheetPaths.length > 0) {
180-
this.htmlContent.styleSheetPaths.forEach(
181-
(styleSheetPath) => {
182-
styleTags += `<link rel="stylesheet" href="${
183-
styleSheetPath.toString().replace("file://", "vscode-resource://")
184-
}">\n`;
185-
});
182+
for (const styleSheetPath of this.htmlContent.styleSheetPaths) {
183+
styleTags += `<link rel="stylesheet" href="${styleSheetPath.toString().replace("file://", "vscode-resource://")}">\n`;
184+
}
186185
}
187186

188187
let scriptTags = "";
189188
if (this.htmlContent.javaScriptPaths &&
190189
this.htmlContent.javaScriptPaths.length > 0) {
191-
this.htmlContent.javaScriptPaths.forEach(
192-
(javaScriptPath) => {
193-
scriptTags += `<script src="${
194-
javaScriptPath.toString().replace("file://", "vscode-resource://")
195-
}"></script>\n`;
196-
});
190+
for (const javaScriptPath of this.htmlContent.javaScriptPaths) {
191+
scriptTags += `<script src="${javaScriptPath.toString().replace("file://", "vscode-resource://")}"></script>\n`;
192+
}
197193
}
198194

199195
// Return an HTML page with the specified content

src/features/ExtensionCommands.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
447447
case "file":
448448
// If the file to save can't be found, just complete the request
449449
if (!this.findTextDocument(this.normalizeFilePath(currentFileUri.fsPath))) {
450-
this.log.writeAndShowError(`File to save not found: ${currentFileUri.fsPath}.`);
450+
await this.log.writeAndShowError(`File to save not found: ${currentFileUri.fsPath}.`);
451451
return EditorOperationResponse.Completed;
452452
}
453453

src/logging.ts

+33-33
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
"use strict";
5+
46
import fs = require("fs");
57
import os = require("os");
6-
import path = require("path");
78
import vscode = require("vscode");
89

910
export enum LogLevel {
@@ -19,16 +20,15 @@ export enum LogLevel {
1920
* This will allow for easy mocking of the logger during unit tests.
2021
*/
2122
export interface ILogger {
22-
write(message: string, ...additionalMessages: string[]);
23-
writeDiagnostic(message: string, ...additionalMessages: string[]);
24-
writeVerbose(message: string, ...additionalMessages: string[]);
25-
writeWarning(message: string, ...additionalMessages: string[]);
26-
writeAndShowWarning(message: string, ...additionalMessages: string[]);
27-
writeError(message: string, ...additionalMessages: string[]);
23+
write(message: string, ...additionalMessages: string[]): void;
24+
writeDiagnostic(message: string, ...additionalMessages: string[]): void;
25+
writeVerbose(message: string, ...additionalMessages: string[]): void;
26+
writeWarning(message: string, ...additionalMessages: string[]): void;
27+
writeAndShowWarning(message: string, ...additionalMessages: string[]): void;
28+
writeError(message: string, ...additionalMessages: string[]): void;
2829
}
2930

3031
export class Logger implements ILogger {
31-
3232
public logBasePath: vscode.Uri;
3333
public logSessionPath: vscode.Uri;
3434
public MinimumLogLevel: LogLevel = LogLevel.Normal;
@@ -40,54 +40,55 @@ export class Logger implements ILogger {
4040
constructor(logBasePath: vscode.Uri) {
4141
this.logChannel = vscode.window.createOutputChannel("PowerShell Extension Logs");
4242
this.logBasePath = vscode.Uri.joinPath(logBasePath, "logs");
43-
4443
this.commands = [
4544
vscode.commands.registerCommand(
4645
"PowerShell.ShowLogs",
4746
() => { this.showLogPanel(); }),
4847

4948
vscode.commands.registerCommand(
5049
"PowerShell.OpenLogFolder",
51-
() => { this.openLogFolder(); }),
50+
async () => { await this.openLogFolder(); }),
5251
];
5352
}
5453

5554
public dispose() {
56-
this.commands.forEach((command) => { command.dispose(); });
5755
this.logChannel.dispose();
56+
for (const command of this.commands) {
57+
command.dispose();
58+
}
5859
}
5960

6061
public getLogFilePath(baseName: string): vscode.Uri {
6162
return vscode.Uri.joinPath(this.logSessionPath, `${baseName}.log`);
6263
}
6364

64-
public writeAtLevel(logLevel: LogLevel, message: string, ...additionalMessages: string[]) {
65+
public writeAtLevel(logLevel: LogLevel, message: string, ...additionalMessages: string[]): void {
6566
if (logLevel >= this.MinimumLogLevel) {
6667
this.writeLine(message, logLevel);
6768

68-
additionalMessages.forEach((line) => {
69-
this.writeLine(line, logLevel);
70-
});
69+
for (const additionalMessage of additionalMessages) {
70+
this.writeLine(additionalMessage, logLevel);
71+
};
7172
}
7273
}
7374

74-
public write(message: string, ...additionalMessages: string[]) {
75+
public write(message: string, ...additionalMessages: string[]): void {
7576
this.writeAtLevel(LogLevel.Normal, message, ...additionalMessages);
7677
}
7778

78-
public writeDiagnostic(message: string, ...additionalMessages: string[]) {
79+
public writeDiagnostic(message: string, ...additionalMessages: string[]): void {
7980
this.writeAtLevel(LogLevel.Diagnostic, message, ...additionalMessages);
8081
}
8182

82-
public writeVerbose(message: string, ...additionalMessages: string[]) {
83+
public writeVerbose(message: string, ...additionalMessages: string[]): void {
8384
this.writeAtLevel(LogLevel.Verbose, message, ...additionalMessages);
8485
}
8586

86-
public writeWarning(message: string, ...additionalMessages: string[]) {
87+
public writeWarning(message: string, ...additionalMessages: string[]): void {
8788
this.writeAtLevel(LogLevel.Warning, message, ...additionalMessages);
8889
}
8990

90-
public writeAndShowWarning(message: string, ...additionalMessages: string[]) {
91+
public writeAndShowWarning(message: string, ...additionalMessages: string[]): void {
9192
this.writeWarning(message, ...additionalMessages);
9293

9394
vscode.window.showWarningMessage(message, "Show Logs").then((selection) => {
@@ -97,23 +98,22 @@ export class Logger implements ILogger {
9798
});
9899
}
99100

100-
public writeError(message: string, ...additionalMessages: string[]) {
101+
public writeError(message: string, ...additionalMessages: string[]): void {
101102
this.writeAtLevel(LogLevel.Error, message, ...additionalMessages);
102103
}
103104

104-
public writeAndShowError(message: string, ...additionalMessages: string[]) {
105+
public async writeAndShowError(message: string, ...additionalMessages: string[]): Promise<void> {
105106
this.writeError(message, ...additionalMessages);
106107

107-
vscode.window.showErrorMessage(message, "Show Logs").then((selection) => {
108-
if (selection !== undefined) {
109-
this.showLogPanel();
110-
}
111-
});
108+
const choice = await vscode.window.showErrorMessage(message, "Show Logs");
109+
if (choice !== undefined) {
110+
this.showLogPanel();
111+
}
112112
}
113113

114114
public async writeAndShowErrorWithActions(
115115
message: string,
116-
actions: { prompt: string; action: () => Promise<void> }[]) {
116+
actions: { prompt: string; action: () => Promise<void> }[]): Promise<void> {
117117
this.writeError(message);
118118

119119
const fullActions = [
@@ -134,7 +134,7 @@ export class Logger implements ILogger {
134134
}
135135
}
136136

137-
public async startNewLog(minimumLogLevel: string = "Normal") {
137+
public async startNewLog(minimumLogLevel: string = "Normal"): Promise<void> {
138138
this.MinimumLogLevel = this.logLevelNameToValue(minimumLogLevel.trim());
139139

140140
this.logSessionPath =
@@ -158,19 +158,19 @@ export class Logger implements ILogger {
158158
}
159159
}
160160

161-
private showLogPanel() {
161+
private showLogPanel(): void {
162162
this.logChannel.show();
163163
}
164164

165-
private openLogFolder() {
165+
private async openLogFolder(): Promise<void> {
166166
if (this.logSessionPath) {
167167
// Open the folder in VS Code since there isn't an easy way to
168168
// open the folder in the platform's file browser
169-
vscode.commands.executeCommand("vscode.openFolder", this.logSessionPath, true);
169+
await vscode.commands.executeCommand("vscode.openFolder", this.logSessionPath, true);
170170
}
171171
}
172172

173-
private writeLine(message: string, level: LogLevel = LogLevel.Normal) {
173+
private writeLine(message: string, level: LogLevel = LogLevel.Normal): void {
174174
const now = new Date();
175175
const timestampedMessage =
176176
`${now.toLocaleDateString()} ${now.toLocaleTimeString()} [${LogLevel[level].toUpperCase()}] - ${message}`;

src/main.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ export async function activate(context: vscode.ExtensionContext): Promise<IPower
183183

184184
export function deactivate(): void {
185185
// Clean up all extension features
186-
languageClientConsumers.forEach((languageClientConsumer) => {
186+
for (const languageClientConsumer of languageClientConsumers) {
187187
languageClientConsumer.dispose();
188-
});
188+
};
189189

190-
commandRegistrations.forEach((commandRegistration) => {
190+
for (const commandRegistration of commandRegistrations) {
191191
commandRegistration.dispose();
192-
});
192+
};
193193

194194
// Dispose of the current session
195195
sessionManager.dispose();

src/session.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ export class SessionManager implements Middleware {
9898
private debugSessionProcess: PowerShellProcess;
9999
private debugEventHandler: vscode.Disposable;
100100
private versionDetails: IPowerShellVersionDetails;
101-
// TODO: Perhaps consumers should also register their handler disposables.
102101
private registeredHandlers: vscode.Disposable[] = [];
103102
private registeredCommands: vscode.Disposable[] = [];
104103
private languageClient: LanguageClient = undefined;
@@ -535,27 +534,27 @@ Type 'help' to get help.
535534
this.sessionDetails = await this.languageServerProcess.start("EditorServices");
536535
} catch (error) {
537536
this.log.write("PowerShell process failed to start.");
538-
this.setSessionFailure("PowerShell process failed to start: ", error);
537+
await this.setSessionFailure("PowerShell process failed to start: ", error);
539538
}
540539

541540
if (this.sessionDetails.status === "started") {
542541
this.log.write("Language server started.");
543542
try {
544543
await this.startLanguageClient(this.sessionDetails);
545544
} catch (error) {
546-
this.setSessionFailure("Language client failed to start: ", error);
545+
await this.setSessionFailure("Language client failed to start: ", error);
547546
}
548547
} else if (this.sessionDetails.status === "failed") {
549548
if (this.sessionDetails.reason === "unsupported") {
550-
this.setSessionFailure(
549+
await this.setSessionFailure(
551550
"PowerShell language features are only supported on PowerShell version 5.1 and 7+. " +
552551
`The current version is ${this.sessionDetails.powerShellVersion}.`);
553552
} else if (this.sessionDetails.reason === "languageMode") {
554-
this.setSessionFailure(
553+
await this.setSessionFailure(
555554
"PowerShell language features are disabled due to an unsupported LanguageMode: " +
556555
`${this.sessionDetails.detail}`);
557556
} else {
558-
this.setSessionFailure(
557+
await this.setSessionFailure(
559558
`PowerShell could not be started for an unknown reason '${this.sessionDetails.reason}'`);
560559
}
561560
} else {
@@ -658,7 +657,7 @@ Type 'help' to get help.
658657
await this.languageClient.start();
659658
this.started = true;
660659
} catch (error) {
661-
this.setSessionFailure("Could not start language service: ", error);
660+
await this.setSessionFailure("Could not start language service: ", error);
662661
return;
663662
}
664663

@@ -795,9 +794,9 @@ Type 'help' to get help.
795794
this.setSessionStatus(version, SessionStatus.Running);
796795
}
797796

798-
private setSessionFailure(message: string, ...additionalMessages: string[]) {
799-
this.log.writeAndShowError(message, ...additionalMessages);
797+
private async setSessionFailure(message: string, ...additionalMessages: string[]) {
800798
this.setSessionStatus("Initialization Error", SessionStatus.Failed);
799+
await this.log.writeAndShowError(message, ...additionalMessages);
801800
}
802801

803802
private async changePowerShellDefaultVersion(exePath: IPowerShellExeDetails) {

test/features/CustomViews.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,12 @@ hello
129129
try {
130130
assert.strictEqual(htmlContentView.getContent(), testCase.expectedHtmlString);
131131
} finally {
132-
jsPaths.forEach((jsPath) => fs.unlinkSync(vscode.Uri.parse(jsPath).fsPath));
133-
cssPaths.forEach((cssPath) => fs.unlinkSync(vscode.Uri.parse(cssPath).fsPath));
132+
for (const jsPath of jsPaths) {
133+
vscode.workspace.fs.delete(vscode.Uri.parse(jsPath));
134+
}
135+
for (const cssPath of cssPaths) {
136+
vscode.workspace.fs.delete(vscode.Uri.parse(cssPath));
137+
}
134138
}
135139
});
136140
}

test/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export function run(): Promise<void> {
3131
}
3232

3333
// Add files to the test suite
34-
files.forEach(f => mocha.addFile(path.resolve(__dirname, f)));
34+
for (const file of files) {
35+
mocha.addFile(path.resolve(__dirname, file));
36+
}
3537

3638
try {
3739
// Run the mocha test

0 commit comments

Comments
 (0)