Skip to content

Commit 38709f2

Browse files
authored
Address some Sonar warnings (microsoft#14879)
1 parent d88c119 commit 38709f2

14 files changed

Lines changed: 73 additions & 58 deletions

File tree

.vscode/extensions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"recommendations": [
55
"ms-vscode.vscode-typescript-tslint-plugin",
66
"editorconfig.editorconfig",
7-
"esbenp.prettier-vscode"
7+
"esbenp.prettier-vscode",
8+
"dbaeumer.vscode-eslint"
89
]
910
}

src/client/activation/languageServer/analysisOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class DotNetLanguageServerAnalysisOptions extends LanguageServerAnalysisO
5656
if (
5757
workspaceFolder &&
5858
Array.isArray(this.workspace.workspaceFolders) &&
59-
this.workspace.workspaceFolders!.length > 1
59+
this.workspace.workspaceFolders.length > 1
6060
) {
6161
filters[0].pattern = `${workspaceFolder.uri.fsPath}/**/*`;
6262
}

src/client/common/application/debugSessionTelemetry.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import { IDisposableRegistry } from '../types';
1313
import { StopWatch } from '../utils/stopWatch';
1414
import { IDebugService } from './types';
1515

16+
// tslint:disable-next-line no-any
17+
function isResponse(a: any): a is DebugProtocol.Response {
18+
return a.type === 'response';
19+
}
1620
class TelemetryTracker implements DebugAdapterTracker {
1721
private timer = new StopWatch();
1822
private readonly trigger: TriggerType = 'launch';
@@ -28,26 +32,25 @@ class TelemetryTracker implements DebugAdapterTracker {
2832
this.sendTelemetry(EventName.DEBUG_SESSION_START);
2933
}
3034

31-
// tslint:disable-next-line:no-any
32-
public onDidSendMessage(message: DebugProtocol.ProtocolMessage) {
33-
if (message.type === 'response') {
34-
const response = message as DebugProtocol.Response;
35-
if (response.command === 'configurationDone') {
35+
// tslint:disable-next-line no-any
36+
public onDidSendMessage(message: any): void {
37+
if (isResponse(message)) {
38+
if (message.command === 'configurationDone') {
3639
// "configurationDone" response is sent immediately after user code starts running.
3740
this.sendTelemetry(EventName.DEBUG_SESSION_USER_CODE_RUNNING);
3841
}
3942
}
4043
}
4144

42-
public onWillStopSession() {
45+
public onWillStopSession(): void {
4346
this.sendTelemetry(EventName.DEBUG_SESSION_STOP);
4447
}
4548

46-
public onError?(_error: Error) {
49+
public onError?(_error: Error): void {
4750
this.sendTelemetry(EventName.DEBUG_SESSION_ERROR);
4851
}
4952

50-
private sendTelemetry(eventName: EventName) {
53+
private sendTelemetry(eventName: EventName): void {
5154
if (eventName === EventName.DEBUG_SESSION_START) {
5255
this.timer.reset();
5356
}

src/client/common/cancellation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export function createPromiseFromCancellation<T>(options: {
3333
return;
3434
}
3535
const complete = () => {
36+
// NOSONAR
3637
if (options.token!.isCancellationRequested) {
3738
if (options.cancelAction === 'resolve') {
3839
return resolve(options.defaultValue);

src/client/common/installer/condaInstaller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class CondaInstaller extends ModuleInstaller {
6969
if (info && info.name) {
7070
// If we have the name of the conda environment, then use that.
7171
args.push('--name');
72-
args.push(info.name!.toCommandArgument());
72+
args.push(info.name.toCommandArgument());
7373
} else if (info && info.path) {
7474
// Else provide the full path to the environment path.
7575
args.push('--prefix');

src/client/common/terminal/activator/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class BaseTerminalActivator implements ITerminalActivator {
2828
);
2929
let activated = false;
3030
if (activationCommands) {
31-
for (const command of activationCommands!) {
31+
for (const command of activationCommands) {
3232
terminal.show(options?.preserveFocus);
3333
terminal.sendText(command);
3434
await this.waitForCommandToProcess(terminalShellType);

src/client/debugger/extension/hooks/childProcessAttachHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class ChildProcessAttachEventHandler implements IDebugSessionEventHandler
3434
event.event === DebuggerEvents.PtvsdAttachToSubprocess ||
3535
event.event === DebuggerEvents.DebugpyAttachToSubprocess
3636
) {
37-
data = event.body! as AttachRequestArguments & DebugConfiguration;
37+
data = event.body as AttachRequestArguments & DebugConfiguration;
3838
} else {
3939
return;
4040
}

src/client/pythonEnvironments/discovery/locators/services/baseVirtualEnvService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class BaseVirtualEnvService extends CacheableLocatorService {
6161
))
6262
.then((interpreters) => interpreters.filter(
6363
(interpreter) => !!interpreter,
64-
).map((interpreter) => interpreter!))
64+
).map((interpreter) => interpreter!)) // NOSONAR
6565
.catch((err) => {
6666
traceError('Python Extension (lookForInterpretersInVenvs):', err);
6767
// Ignore exceptions.

src/client/pythonEnvironments/discovery/locators/services/condaService.ts

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const untildify: (value: string) => string = require('untildify');
2525

2626
// This glob pattern will match all of the following:
2727
// ~/anaconda/bin/conda, ~/anaconda3/bin/conda, ~/miniconda/bin/conda, ~/miniconda3/bin/conda
28-
// /usr/share/anaconda/bin/conda, /usr/share/anaconda3/bin/conda, /usr/share/miniconda/bin/conda, /usr/share/miniconda3/bin/conda
28+
// /usr/share/anaconda/bin/conda, /usr/share/anaconda3/bin/conda, /usr/share/miniconda/bin/conda,
29+
// /usr/share/miniconda3/bin/conda
2930

3031
const condaGlobPathsForLinuxMac = [
3132
untildify('~/opt/*conda*/bin/conda'),
@@ -62,6 +63,11 @@ interface IComponent {
6263
*/
6364
@injectable()
6465
export class CondaService implements ICondaService {
66+
public get condaEnvironmentsFile(): string | undefined {
67+
const homeDir = this.platform.isWindows ? process.env.USERPROFILE : process.env.HOME || process.env.HOMEPATH;
68+
return homeDir ? path.join(homeDir, '.conda', 'environments.txt') : undefined;
69+
}
70+
6571
private condaFile?: Promise<string | undefined>;
6672

6773
private isAvailable: boolean | undefined;
@@ -83,9 +89,30 @@ export class CondaService implements ICondaService {
8389
this.addCondaPathChangedHandler();
8490
}
8591

86-
public get condaEnvironmentsFile(): string | undefined {
87-
const homeDir = this.platform.isWindows ? process.env.USERPROFILE : process.env.HOME || process.env.HOMEPATH;
88-
return homeDir ? path.join(homeDir, '.conda', 'environments.txt') : undefined;
92+
/**
93+
* Return the highest Python version from the given list.
94+
*/
95+
private static getLatestVersion(interpreters: PythonEnvironment[]): PythonEnvironment | undefined {
96+
const sortedInterpreters = interpreters.slice();
97+
// tslint:disable-next-line:no-non-null-assertion
98+
sortedInterpreters.sort((a, b) => (a.version && b.version ? compare(a.version.raw, b.version.raw) : 0));
99+
if (sortedInterpreters.length > 0) {
100+
return sortedInterpreters[sortedInterpreters.length - 1];
101+
}
102+
103+
return undefined;
104+
}
105+
106+
/**
107+
* Is the given interpreter from conda?
108+
*/
109+
private static detectCondaEnvironment(env: PythonEnvironment): boolean {
110+
return (
111+
env.envType === EnvironmentType.Conda
112+
|| (env.displayName ? env.displayName : '').toUpperCase().indexOf('ANACONDA') >= 0
113+
|| (env.companyDisplayName ? env.companyDisplayName : '').toUpperCase().indexOf('ANACONDA') >= 0
114+
|| (env.companyDisplayName ? env.companyDisplayName : '').toUpperCase().indexOf('CONTINUUM') >= 0
115+
);
89116
}
90117

91118
/**
@@ -94,7 +121,7 @@ export class CondaService implements ICondaService {
94121
* Called by VS Code to indicate it is done with the resource.
95122
*/
96123
// tslint:disable-next-line:no-empty
97-
public dispose() {}
124+
public dispose(): void {} // eslint-disable-line
98125

99126
/**
100127
* Return the path to the "conda file".
@@ -116,8 +143,9 @@ export class CondaService implements ICondaService {
116143
return this.isAvailable;
117144
}
118145
return this.getCondaVersion()
119-
.then((version) => (this.isAvailable = version !== undefined))
120-
.catch(() => (this.isAvailable = false));
146+
147+
.then((version) => (this.isAvailable = version !== undefined)) // eslint-disable-line no-return-assign
148+
.catch(() => (this.isAvailable = false)); // eslint-disable-line no-return-assign
121149
}
122150

123151
/**
@@ -143,7 +171,7 @@ export class CondaService implements ICondaService {
143171
versionString = stdOut && stdOut.startsWith('conda ') ? stdOut.substring('conda '.length).trim() : stdOut;
144172
}
145173
if (!versionString) {
146-
return;
174+
return undefined;
147175
}
148176
const version = parse(versionString, true);
149177
if (version) {
@@ -157,7 +185,7 @@ export class CondaService implements ICondaService {
157185
/**
158186
* Can the shell find conda (to run it)?
159187
*/
160-
public async isCondaInCurrentPath() {
188+
public async isCondaInCurrentPath(): Promise<boolean> {
161189
const processService = await this.processServiceFactory.create();
162190
return processService
163191
.exec('conda', ['--version'])
@@ -181,6 +209,7 @@ export class CondaService implements ICondaService {
181209
// Failed because either:
182210
// 1. conda is not installed.
183211
// 2. `conda info --json` has changed signature.
212+
return undefined;
184213
}
185214
}
186215

@@ -212,7 +241,7 @@ export class CondaService implements ICondaService {
212241
}
213242
const isCondaEnv = await this.isCondaEnvironment(interpreterPath);
214243
if (!isCondaEnv) {
215-
return;
244+
return undefined;
216245
}
217246
let environments = await this.getCondaEnvironments(false);
218247
const dir = path.dirname(interpreterPath);
@@ -239,6 +268,7 @@ export class CondaService implements ICondaService {
239268

240269
// If still not available, then the user created the env after starting vs code.
241270
// The only solution is to get the user to re-start vscode.
271+
return undefined;
242272
}
243273

244274
/**
@@ -249,8 +279,7 @@ export class CondaService implements ICondaService {
249279
// Global cache.
250280
const globalPersistence = this.persistentStateFactory.createGlobalPersistentState<{
251281
data: CondaEnvironmentInfo[] | undefined;
252-
// tslint:disable-next-line:no-any
253-
}>('CONDA_ENVIRONMENTS', undefined as any);
282+
}>('CONDA_ENVIRONMENTS', undefined);
254283
if (!ignoreCache && globalPersistence.value) {
255284
return globalPersistence.value.data;
256285
}
@@ -284,6 +313,7 @@ export class CondaService implements ICondaService {
284313
// 1. conda is not installed.
285314
// 2. `conda env list has changed signature.
286315
traceError('Failed to get conda environment list from conda', ex);
316+
return undefined;
287317
}
288318
}
289319

@@ -297,7 +327,9 @@ export class CondaService implements ICondaService {
297327
}
298328

299329
/**
300-
* Get the conda exe from the path to an interpreter's python. This might be different than the globally registered conda.exe
330+
* Get the conda exe from the path to an interpreter's python. This might be different than the
331+
* globally registered conda.exe.
332+
*
301333
* The value is cached for a while.
302334
* The only way this can change is if user installs conda into this same environment.
303335
* Generally we expect that to happen the other way, the user creates a conda environment with conda in it.
@@ -338,30 +370,8 @@ export class CondaService implements ICondaService {
338370
if (await this.fileSystem.fileExists(condaPath2)) {
339371
return condaPath2;
340372
}
341-
}
342373

343-
/**
344-
* Is the given interpreter from conda?
345-
*/
346-
private detectCondaEnvironment(env: PythonEnvironment) {
347-
return (
348-
env.envType === EnvironmentType.Conda
349-
|| (env.displayName ? env.displayName : '').toUpperCase().indexOf('ANACONDA') >= 0
350-
|| (env.companyDisplayName ? env.companyDisplayName : '').toUpperCase().indexOf('ANACONDA') >= 0
351-
|| (env.companyDisplayName ? env.companyDisplayName : '').toUpperCase().indexOf('CONTINUUM') >= 0
352-
);
353-
}
354-
355-
/**
356-
* Return the highest Python version from the given list.
357-
*/
358-
private getLatestVersion(interpreters: PythonEnvironment[]) {
359-
const sortedInterpreters = interpreters.slice();
360-
// tslint:disable-next-line:no-non-null-assertion
361-
sortedInterpreters.sort((a, b) => (a.version && b.version ? compare(a.version.raw, b.version.raw) : 0));
362-
if (sortedInterpreters.length > 0) {
363-
return sortedInterpreters[sortedInterpreters.length - 1];
364-
}
374+
return undefined;
365375
}
366376

367377
private addCondaPathChangedHandler() {
@@ -396,8 +406,8 @@ export class CondaService implements ICondaService {
396406
}
397407
if (this.platform.isWindows && this.registryLookupForConda) {
398408
const interpreters = await this.registryLookupForConda.getInterpreters();
399-
const condaInterpreters = interpreters.filter(this.detectCondaEnvironment);
400-
const condaInterpreter = this.getLatestVersion(condaInterpreters);
409+
const condaInterpreters = interpreters.filter(CondaService.detectCondaEnvironment);
410+
const condaInterpreter = CondaService.getLatestVersion(condaInterpreters);
401411
if (condaInterpreter) {
402412
const interpreterPath = await this.getCondaFileFromInterpreter(
403413
condaInterpreter.path,

src/client/testing/common/debugLauncher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class DebugLauncher implements ITestDebugLauncher {
3232
}
3333

3434
public async launchDebugger(options: LaunchOptions) {
35-
if (options.token && options.token!.isCancellationRequested) {
35+
if (options.token && options.token.isCancellationRequested) {
3636
return;
3737
}
3838

0 commit comments

Comments
 (0)