Skip to content

Commit 333b8a7

Browse files
committed
Enable '@typescript-eslint/strict'
1 parent 7d62af7 commit 333b8a7

12 files changed

+79
-108
lines changed

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"extends": [
77
"eslint:recommended",
88
"plugin:@typescript-eslint/recommended",
9-
"plugin:@typescript-eslint/recommended-requiring-type-checking"
9+
"plugin:@typescript-eslint/recommended-requiring-type-checking",
10+
"plugin:@typescript-eslint/strict"
1011
],
1112
"overrides": [],
1213
"parser": "@typescript-eslint/parser",

src/features/Console.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function showChoicePrompt(promptDetails: IShowChoicePromptRequestArgs): Thenable
7171
};
7272
});
7373

74-
if (promptDetails.defaultChoices && promptDetails.defaultChoices.length > 0) {
74+
if (promptDetails.defaultChoices.length > 0) {
7575
// Shift the default items to the front of the
7676
// array so that the user can select it easily
7777
const defaultChoice = promptDetails.defaultChoices[0];

src/features/CustomViews.ts

+18-26
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class CustomViewsFeature extends LanguageClientConsumer {
7272

7373
class PowerShellContentProvider implements vscode.TextDocumentContentProvider {
7474

75-
private viewIndex: { [id: string]: CustomView } = {};
75+
private viewIndex: Record<string, CustomView> = {};
7676
private didChangeEvent: vscode.EventEmitter<vscode.Uri> = new vscode.EventEmitter<vscode.Uri>();
7777

7878
public onDidChange: vscode.Event<vscode.Uri> = this.didChangeEvent.event;
@@ -99,10 +99,12 @@ class PowerShellContentProvider implements vscode.TextDocumentContentProvider {
9999
public closeView(id: string) {
100100
const uriString = this.getUri(id);
101101

102-
vscode.workspace.textDocuments.some(async (doc) => {
102+
vscode.workspace.textDocuments.some((doc) => {
103103
if (doc.uri.toString() === uriString) {
104-
await vscode.window.showTextDocument(doc);
105-
await vscode.commands.executeCommand("workbench.action.closeActiveEditor");
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");
106108
return true;
107109
}
108110

@@ -114,20 +116,16 @@ class PowerShellContentProvider implements vscode.TextDocumentContentProvider {
114116
const uriString = this.getUri(id);
115117
const view: CustomView = this.viewIndex[uriString];
116118

117-
if (view.viewType === CustomViewType.HtmlContent) {
118-
(view as HtmlContentView).setContent(content);
119-
this.didChangeEvent.fire(vscode.Uri.parse(uriString));
120-
}
119+
(view as HtmlContentView).setContent(content);
120+
this.didChangeEvent.fire(vscode.Uri.parse(uriString));
121121
}
122122

123123
public appendHtmlOutputView(id: string, content: string) {
124124
const uriString = this.getUri(id);
125125
const view: CustomView = this.viewIndex[uriString];
126126

127-
if (view.viewType === CustomViewType.HtmlContent) {
128-
(view as HtmlContentView).appendContent(content);
129-
this.didChangeEvent.fire(vscode.Uri.parse(uriString));
130-
}
127+
(view as HtmlContentView).appendContent(content);
128+
this.didChangeEvent.fire(vscode.Uri.parse(uriString));
131129
}
132130

133131
private getUri(id: string) {
@@ -172,16 +170,14 @@ class HtmlContentView extends CustomView {
172170

173171
public getContent(): string {
174172
let styleTags = "";
175-
if (this.htmlContent.styleSheetPaths &&
176-
this.htmlContent.styleSheetPaths.length > 0) {
173+
if (this.htmlContent.styleSheetPaths.length > 0) {
177174
for (const styleSheetPath of this.htmlContent.styleSheetPaths) {
178175
styleTags += `<link rel="stylesheet" href="${styleSheetPath.toString().replace("file://", "vscode-resource://")}">\n`;
179176
}
180177
}
181178

182179
let scriptTags = "";
183-
if (this.htmlContent.javaScriptPaths &&
184-
this.htmlContent.javaScriptPaths.length > 0) {
180+
if (this.htmlContent.javaScriptPaths.length > 0) {
185181
for (const javaScriptPath of this.htmlContent.javaScriptPaths) {
186182
scriptTags += `<script src="${javaScriptPath.toString().replace("file://", "vscode-resource://")}"></script>\n`;
187183
}
@@ -195,17 +191,13 @@ class HtmlContentView extends CustomView {
195191
this.webviewPanel?.dispose();
196192

197193
let localResourceRoots: vscode.Uri[] = [];
198-
if (this.htmlContent.javaScriptPaths) {
199-
localResourceRoots = localResourceRoots.concat(this.htmlContent.javaScriptPaths.map((p) => {
200-
return vscode.Uri.parse(path.dirname(p));
201-
}));
202-
}
194+
localResourceRoots = localResourceRoots.concat(this.htmlContent.javaScriptPaths.map((p) => {
195+
return vscode.Uri.parse(path.dirname(p));
196+
}));
203197

204-
if (this.htmlContent.styleSheetPaths) {
205-
localResourceRoots = localResourceRoots.concat(this.htmlContent.styleSheetPaths.map((p) => {
206-
return vscode.Uri.parse(path.dirname(p));
207-
}));
208-
}
198+
localResourceRoots = localResourceRoots.concat(this.htmlContent.styleSheetPaths.map((p) => {
199+
return vscode.Uri.parse(path.dirname(p));
200+
}));
209201

210202
this.webviewPanel = vscode.window.createWebviewPanel(
211203
this.id,

src/features/ExtensionCommands.ts

+4-23
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,15 @@ export interface IExtensionCommandAddedNotificationBody {
4848
displayName: string;
4949
}
5050

51-
function asRange(value: vscode.Range): Range | undefined | null {
52-
if (value === undefined) {
53-
return undefined;
54-
} else if (value === null) {
55-
return null;
56-
}
57-
return { start: asPosition(value.start)!, end: asPosition(value.end)! };
51+
function asRange(value: vscode.Range): Range {
52+
return { start: asPosition(value.start), end: asPosition(value.end) };
5853
}
5954

60-
function asPosition(value: vscode.Position): Position | undefined | null {
61-
if (value === undefined) {
62-
return undefined;
63-
} else if (value === null) {
64-
return null;
65-
}
55+
function asPosition(value: vscode.Position): Position {
6656
return { line: value.line, character: value.character };
6757
}
6858

69-
function asCodePosition(value: Position): vscode.Position | undefined | null {
70-
if (value === undefined) {
71-
return undefined;
72-
} else if (value === null) {
73-
return null;
74-
}
59+
function asCodePosition(value: Position): vscode.Position {
7560
return new vscode.Position(value.line, value.character);
7661
}
7762

@@ -218,10 +203,6 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
218203
// only relevant to the previous session
219204
this.extensionCommands = [];
220205

221-
if (languageclient === undefined) {
222-
this.log.write("Language client given to ExtensionCommandsFeature is undefined");
223-
return;
224-
}
225206
this.languageClient = languageclient;
226207

227208
this.handlers = [

src/features/GetCommands.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class CommandsExplorerProvider implements vscode.TreeDataProvider<Command> {
110110
}
111111

112112
public getChildren(_element?: Command): Thenable<Command[]> {
113-
return Promise.resolve(this.powerShellCommands || []);
113+
return Promise.resolve(this.powerShellCommands);
114114
}
115115
}
116116

src/features/HelpCompletion.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class HelpCompletionFeature extends LanguageClientConsumer {
5757
return;
5858
}
5959

60-
if (!(changeEvent && changeEvent.contentChanges)) {
60+
if (changeEvent.contentChanges.length === 0) {
6161
this.log.writeWarning(`<${HelpCompletionFeature.name}>: ` +
6262
`Bad TextDocumentChangeEvent message: ${JSON.stringify(changeEvent)}`);
6363
return;
@@ -95,6 +95,7 @@ class TriggerFinder {
9595
public updateState(document: TextDocument, changeText: string): void {
9696
switch (this.state) {
9797
case SearchState.Searching:
98+
// eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
9899
if (changeText.length === 1 && changeText[0] === this.triggerCharacters[this.count]) {
99100
this.state = SearchState.Locked;
100101
this.document = document;
@@ -103,9 +104,8 @@ class TriggerFinder {
103104
break;
104105

105106
case SearchState.Locked:
106-
if (document === this.document &&
107-
changeText.length === 1 &&
108-
changeText[0] === this.triggerCharacters[this.count]) {
107+
// eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
108+
if (document === this.document && changeText.length === 1 && changeText[0] === this.triggerCharacters[this.count]) {
109109
this.count++;
110110
if (this.count === this.triggerCharacters.length) {
111111
this.state = SearchState.Found;
@@ -171,13 +171,13 @@ class HelpCompletionProvider {
171171
blockComment: this.settings.helpCompletion === Settings.CommentType.BlockComment,
172172
});
173173

174-
if (!(result && result.content)) {
174+
if (result.content.length === 0) {
175175
return;
176176
}
177177

178178
const replaceRange = new Range(triggerStartPos.translate(0, -1), triggerStartPos.translate(0, 1));
179179

180-
// TODO add indentation level to the help content
180+
// TODO: add indentation level to the help content
181181
// Trim leading whitespace (used by the rule for indentation) as VSCode takes care of the indentation.
182182
// Trim the last empty line and join the strings.
183183
const lines: string[] = result.content;

src/features/PesterTests.ts

+31-33
Original file line numberDiff line numberDiff line change
@@ -13,75 +13,73 @@ enum LaunchType {
1313
}
1414

1515
export class PesterTestsFeature implements vscode.Disposable {
16-
17-
private command: vscode.Disposable;
16+
private commands: vscode.Disposable[];
1817
private invokePesterStubScriptPath: string;
1918

2019
constructor(private sessionManager: SessionManager) {
2120
this.invokePesterStubScriptPath = path.resolve(__dirname, "../modules/PowerShellEditorServices/InvokePesterStub.ps1");
22-
23-
// File context-menu command - Run Pester Tests
24-
this.command = vscode.commands.registerCommand(
25-
"PowerShell.RunPesterTestsFromFile",
26-
(fileUri) => {
27-
return this.launchAllTestsInActiveEditor(LaunchType.Run, fileUri);
28-
});
29-
// File context-menu command - Debug Pester Tests
30-
this.command = vscode.commands.registerCommand(
31-
"PowerShell.DebugPesterTestsFromFile",
32-
(fileUri) => {
33-
return this.launchAllTestsInActiveEditor(LaunchType.Debug, fileUri);
34-
});
35-
// This command is provided for usage by PowerShellEditorServices (PSES) only
36-
this.command = vscode.commands.registerCommand(
37-
"PowerShell.RunPesterTests",
38-
(uriString, runInDebugger, describeBlockName?, describeBlockLineNumber?, outputPath?) => {
39-
return this.launchTests(uriString, runInDebugger, describeBlockName, describeBlockLineNumber, outputPath);
40-
});
21+
this.commands = [
22+
// File context-menu command - Run Pester Tests
23+
vscode.commands.registerCommand(
24+
"PowerShell.RunPesterTestsFromFile",
25+
(fileUri) => {
26+
return this.launchAllTestsInActiveEditor(LaunchType.Run, fileUri);
27+
}),
28+
29+
// File context-menu command - Debug Pester Tests
30+
vscode.commands.registerCommand(
31+
"PowerShell.DebugPesterTestsFromFile",
32+
(fileUri) => {
33+
return this.launchAllTestsInActiveEditor(LaunchType.Debug, fileUri);
34+
}),
35+
36+
// This command is provided for usage by PowerShellEditorServices (PSES) only
37+
vscode.commands.registerCommand(
38+
"PowerShell.RunPesterTests",
39+
(uriString, runInDebugger, describeBlockName?, describeBlockLineNumber?, outputPath?) => {
40+
return this.launchTests(vscode.Uri.parse(uriString), runInDebugger, describeBlockName, describeBlockLineNumber, outputPath);
41+
})
42+
];
4143
}
4244

4345
public dispose() {
44-
this.command.dispose();
46+
for (const command of this.commands) {
47+
command.dispose();
48+
}
4549
}
4650

4751
private async launchAllTestsInActiveEditor(
4852
launchType: LaunchType,
4953
fileUri: vscode.Uri): Promise<boolean> {
5054

51-
if (fileUri === undefined && vscode.window.activeTextEditor === undefined) {
52-
return false;
53-
}
54-
55-
const uriString = (fileUri || vscode.window.activeTextEditor!.document.uri).toString();
56-
const launchConfig = this.createLaunchConfig(uriString, launchType);
55+
const launchConfig = this.createLaunchConfig(fileUri, launchType);
5756
return this.launch(launchConfig);
5857
}
5958

6059
private async launchTests(
61-
uriString: string,
60+
fileUri: vscode.Uri,
6261
runInDebugger: boolean,
6362
describeBlockName?: string,
6463
describeBlockLineNumber?: number,
6564
outputPath?: string): Promise<boolean> {
6665

6766
const launchType = runInDebugger ? LaunchType.Debug : LaunchType.Run;
68-
const launchConfig = this.createLaunchConfig(uriString, launchType, describeBlockName, describeBlockLineNumber, outputPath);
67+
const launchConfig = this.createLaunchConfig(fileUri, launchType, describeBlockName, describeBlockLineNumber, outputPath);
6968
return this.launch(launchConfig);
7069
}
7170

7271
private createLaunchConfig(
73-
uriString: string,
72+
fileUri: vscode.Uri,
7473
launchType: LaunchType,
7574
testName?: string,
7675
lineNum?: number,
7776
outputPath?: string): vscode.DebugConfiguration {
7877

79-
const uri = vscode.Uri.parse(uriString);
8078
const settings = Settings.load();
8179

8280
// Since we pass the script path to PSES in single quotes to avoid issues with PowerShell
8381
// special chars like & $ @ () [], we do have to double up the interior single quotes.
84-
const scriptPath = uri.fsPath.replace(/'/g, "''");
82+
const scriptPath = fileUri.fsPath.replace(/'/g, "''");
8583

8684
const launchConfig = {
8785
request: "launch",

src/features/RemoteFiles.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class RemoteFilesFeature extends LanguageClientConsumer {
4848
}
4949

5050
public dispose() {
51-
this.command?.dispose();
51+
this.command.dispose();
5252
// Close any leftover remote files before exiting
5353
this.closeRemoteFiles();
5454
}

src/platform.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ export class PowerShellExeFinder {
9494
platformDetails?: IPlatformDetails,
9595
additionalPowerShellExes?: IPowerShellAdditionalExePathSettings) {
9696

97-
this.platformDetails = platformDetails || getPlatformDetails();
98-
this.additionalPSExeSettings = additionalPowerShellExes || {};
97+
this.platformDetails = platformDetails ?? getPlatformDetails();
98+
this.additionalPSExeSettings = additionalPowerShellExes ?? {};
9999
}
100100

101101
/**
@@ -157,7 +157,7 @@ export class PowerShellExeFinder {
157157
// Also show any additionally configured PowerShells
158158
// These may be duplicates of the default installations, but given a different name.
159159
for (const additionalPwsh of this.enumerateAdditionalPowerShellInstallations()) {
160-
if (additionalPwsh && await additionalPwsh.exists()) {
160+
if (await additionalPwsh.exists()) {
161161
yield additionalPwsh;
162162
}
163163
}

src/session.ts

+3
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,11 @@ export class SessionManager implements Middleware {
327327
return codeLensToFix;
328328
};
329329

330+
// TODO: This makes zero sense, but appears to be "working" and copied by others per https://github.com/microsoft/vscode-languageserver-node/issues/495. Thing is, ESLint says these conditionals are always truthy.
331+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
330332
if ((resolvedCodeLens as Thenable<vscode.CodeLens>).then) {
331333
return (resolvedCodeLens as Thenable<vscode.CodeLens>).then(resolveFunc);
334+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
332335
} else if (resolvedCodeLens as vscode.CodeLens) {
333336
return resolveFunc(resolvedCodeLens as vscode.CodeLens);
334337
}

src/settings.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ export enum CommentType {
2525
LineComment = "LineComment",
2626
}
2727

28-
export interface IPowerShellAdditionalExePathSettings {
29-
[versionName: string]: string;
30-
}
28+
export type IPowerShellAdditionalExePathSettings = Record<string, string>;
3129

3230
export interface IBugReportingSettings {
3331
project: string;
@@ -316,9 +314,7 @@ function getWorkspaceSettingsWithDefaults<TSettings>(
316314
const importedSettings: TSettings = workspaceConfiguration.get<TSettings>(settingName, defaultSettings);
317315

318316
for (const setting in importedSettings) {
319-
if (importedSettings[setting]) {
320-
defaultSettings[setting] = importedSettings[setting];
321-
}
317+
defaultSettings[setting] = importedSettings[setting];
322318
}
323319
return defaultSettings;
324320
}
@@ -336,13 +332,13 @@ export async function validateCwdSetting(): Promise<string> {
336332

337333
// If there is no workspace, or there is but it has no folders, fallback.
338334
if (vscode.workspace.workspaceFolders === undefined
339-
|| vscode.workspace.workspaceFolders?.length === 0) {
335+
|| vscode.workspace.workspaceFolders.length === 0) {
340336
cwd = undefined;
341337
// If there is exactly one workspace folder, use that.
342-
} else if (vscode.workspace.workspaceFolders?.length === 1) {
343-
cwd = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
338+
} else if (vscode.workspace.workspaceFolders.length === 1) {
339+
cwd = vscode.workspace.workspaceFolders[0].uri.fsPath;
344340
// If there is more than one workspace folder, prompt the user once.
345-
} else if (vscode.workspace.workspaceFolders?.length > 1 && !hasPrompted) {
341+
} else if (vscode.workspace.workspaceFolders.length > 1 && !hasPrompted) {
346342
hasPrompted = true;
347343
const options: vscode.WorkspaceFolderPickOptions = {
348344
placeHolder: "Select a folder to use as the PowerShell extension's working directory.",

0 commit comments

Comments
 (0)