Skip to content

Commit a9b5603

Browse files
authored
Clean up eslint errors (#13951)
* Clean up eslint errors * Fix linting and address comments.
1 parent 64c8526 commit a9b5603

File tree

7 files changed

+145
-124
lines changed

7 files changed

+145
-124
lines changed

src/client/pythonEnvironments/discovery/locators/helpers.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { EnvironmentType, PythonEnvironment } from '../../info';
1010

1111
const CheckPythonInterpreterRegEx = IS_WINDOWS ? /^python(\d+(.\d+)?)?\.exe$/ : /^python(\d+(.\d+)?)?$/;
1212

13-
export async function lookForInterpretersInDirectory(pathToCheck: string, _: IFileSystem): Promise<string[]> {
13+
export async function lookForInterpretersInDirectory(pathToCheck: string): Promise<string[]> {
1414
// Technically, we should be able to use fs.getFiles(). However,
1515
// that breaks some tests. So we stick with the broader behavior.
1616
try {
@@ -40,9 +40,9 @@ export class InterpreterLocatorHelper implements IInterpreterLocatorHelper {
4040
item.path = path.normalize(item.path);
4141
return item;
4242
})
43-
.reduce<PythonEnvironment[]>((accumulator, current) => {
43+
.reduce<PythonEnvironment[]>((accumulator, current:PythonEnvironment) => {
4444
const currentVersion = current && current.version ? current.version.raw : undefined;
45-
const existingItem = accumulator.find((item) => {
45+
let existingItem = accumulator.find((item) => {
4646
// If same version and same base path, then ignore.
4747
// Could be Python 3.6 with path = python.exe, and Python 3.6 and path = python3.exe.
4848
if (
@@ -76,12 +76,11 @@ export class InterpreterLocatorHelper implements IInterpreterLocatorHelper {
7676
'sysVersion',
7777
'version',
7878
];
79-
for (const prop of props) {
80-
if (!existingItem[prop] && current[prop]) {
81-
// tslint:disable-next-line: no-any
82-
(existingItem as any)[prop] = current[prop];
79+
props.forEach((prop) => {
80+
if (existingItem && !existingItem[prop] && current[prop]) {
81+
existingItem = { ...existingItem, [prop]: current[prop] };
8382
}
84-
}
83+
});
8584
}
8685
return accumulator;
8786
}, []);

src/client/pythonEnvironments/discovery/locators/index.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
// tslint:disable-next-line: no-single-line-block-comment
2+
/* eslint-disable max-classes-per-file */
13
import { inject, injectable } from 'inversify';
4+
import { flatten } from 'lodash';
25
import {
36
Disposable, Event, EventEmitter, Uri,
47
} from 'vscode';
@@ -37,9 +40,6 @@ import { PythonEnvironment } from '../../info';
3740
import { isHiddenInterpreter } from './services/interpreterFilter';
3841
import { GetInterpreterLocatorOptions } from './types';
3942

40-
// tslint:disable-next-line:no-require-imports no-var-requires
41-
const flatten = require('lodash/flatten') as typeof import('lodash/flatten');
42-
4343
/**
4444
* A wrapper around all locators used by the extension.
4545
*/
@@ -49,7 +49,7 @@ export class ExtensionLocators extends Locators {
4949
nonWorkspace: ILocator[],
5050
// This is expected to be a locator wrapping any found in
5151
// the workspace (i.e. WorkspaceLocators).
52-
workspace: ILocator
52+
workspace: ILocator,
5353
) {
5454
super([...nonWorkspace, workspace]);
5555
}
@@ -72,10 +72,12 @@ type RootURI = string;
7272
*/
7373
export class WorkspaceLocators extends Locator {
7474
private readonly locators: Record<RootURI, DisableableLocator> = {};
75+
7576
private readonly roots: Record<RootURI, Uri> = {};
77+
7678
constructor(
7779
// used to produce the per-root locators:
78-
private readonly factories: WorkspaceLocatorFactory[]
80+
private readonly factories: WorkspaceLocatorFactory[],
7981
) {
8082
super();
8183
}
@@ -85,10 +87,10 @@ export class WorkspaceLocators extends Locator {
8587
*
8688
* @param folders - the info used to keep track of the workspace folders
8789
*/
88-
public activate(folders: IWorkspaceFolders) {
89-
for (const root of folders.roots) {
90+
public activate(folders: IWorkspaceFolders):void {
91+
folders.roots.forEach((root) => {
9092
this.addRoot(root);
91-
}
93+
});
9294
folders.onAdded((root: Uri) => this.addRoot(root));
9395
folders.onRemoved((root: Uri) => this.removeRoot(root));
9496
}
@@ -116,7 +118,11 @@ export class WorkspaceLocators extends Locator {
116118
}
117119
}
118120
// Fall back to checking all the roots.
121+
// The eslint disable below should be removed after we have a
122+
// better solution for these. We need asyncFind for this.
123+
// eslint-disable-next-line no-restricted-syntax
119124
for (const key of Object.keys(this.locators)) {
125+
// eslint-disable-next-line no-await-in-loop
120126
const resolved = await this.locators[key].resolveEnv(env);
121127
if (resolved !== undefined) {
122128
return resolved;
@@ -130,9 +136,9 @@ export class WorkspaceLocators extends Locator {
130136
this.removeRoot(root);
131137
// Create the root's locator, wrapping each factory-generated locator.
132138
const locators: ILocator[] = [];
133-
for (const create of this.factories) {
139+
this.factories.forEach((create) => {
134140
locators.push(...create(root));
135-
}
141+
});
136142
const locator = new DisableableLocator(new Locators(locators));
137143
// Cache it.
138144
const key = root.toString();
@@ -168,17 +174,10 @@ export class WorkspaceLocators extends Locator {
168174
* or the URI must be a parent of one of the candidates.
169175
*/
170176
function matchURI(uri: Uri, ...candidates: Uri[]): boolean {
171-
const uriPath = uri.path.endsWith('/') ? uri.path : `{uri.path}/`;
172-
for (const candidate of candidates) {
173-
if (candidate.scheme === uri.scheme) {
174-
if (candidate.path === uri.path) {
175-
return true;
176-
} else if (candidate.path.startsWith(uriPath)) {
177-
return true;
178-
}
179-
}
180-
}
181-
return false;
177+
const uriPath = uri.path.endsWith('/') ? uri.path : '{uri.path}/';
178+
const matchedUri = candidates.find((candidate) => (candidate.scheme === uri.scheme)
179+
&& (candidate.path === uri.path || candidate.path.startsWith(uriPath)));
180+
return matchedUri !== undefined;
182181
}
183182

184183
/**
@@ -196,6 +195,9 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
196195

197196
private readonly _hasInterpreters: Deferred<boolean>;
198197

198+
private readonly onLocatingEmitter:EventEmitter<Promise<PythonEnvironment[]>> =
199+
new EventEmitter<Promise<PythonEnvironment[]>>();
200+
199201
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) {
200202
this._hasInterpreters = createDeferred<boolean>();
201203
serviceContainer.get<Disposable[]>(IDisposableRegistry).push(this);
@@ -206,14 +208,14 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
206208

207209
/**
208210
* This class should never emit events when we're locating.
209-
* The events will be fired by the indivitual locators retrieved in `getLocators`.
211+
* The events will be fired by the individual locators retrieved in `getLocators`.
210212
*
211213
* @readonly
212214
* @type {Event<Promise<PythonEnvironment[]>>}
213215
* @memberof PythonInterpreterLocatorService
214216
*/
215217
public get onLocating(): Event<Promise<PythonEnvironment[]>> {
216-
return new EventEmitter<Promise<PythonEnvironment[]>>().event;
218+
return this.onLocatingEmitter.event;
217219
}
218220

219221
public get hasInterpreters(): Promise<boolean> {
@@ -225,7 +227,7 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
225227
*
226228
* Called by VS Code to indicate it is done with the resource.
227229
*/
228-
public dispose() {
230+
public dispose():void {
229231
this.disposables.forEach((disposable) => disposable.dispose());
230232
}
231233

@@ -286,7 +288,9 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
286288
// Set it to true the first time the user selects an interpreter
287289
if (!this.didTriggerInterpreterSuggestions && options?.onSuggestion === true) {
288290
this.didTriggerInterpreterSuggestions = true;
289-
locators.forEach((locator) => (locator.didTriggerInterpreterSuggestions = true));
291+
locators.forEach((locator) => {
292+
locator.didTriggerInterpreterSuggestions = true;
293+
});
290294
}
291295

292296
return locators;

src/client/pythonEnvironments/discovery/locators/progressService.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ export class InterpreterLocatorProgressService implements IInterpreterLocatorPro
6161
this.refreshing.fire();
6262
}
6363

64-
private checkProgress() {
64+
private checkProgress(): void {
6565
if (this.deferreds.length === 0) {
6666
return;
6767
}
6868
if (this.areAllItemsComplete()) {
69-
return this.notifyCompleted();
69+
this.notifyCompleted();
70+
return;
7071
}
7172
Promise.all(this.deferreds.map((item) => item.promise))
7273
.catch(noop)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class KnownPathsService extends CacheableLocatorService {
8383
const fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
8484
return fs
8585
.directoryExists(dir)
86-
.then((exists) => (exists ? lookForInterpretersInDirectory(dir, fs) : Promise.resolve<string[]>([])));
86+
.then((exists) => (exists ? lookForInterpretersInDirectory(dir) : Promise.resolve<string[]>([])));
8787
}
8888
}
8989

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// tslint:disable:no-unnecessary-callback-wrapper no-require-imports no-var-requires
22

33
import { injectable, unmanaged } from 'inversify';
4+
import { flatten, noop } from 'lodash';
45
import * as path from 'path';
56
import { Uri } from 'vscode';
67
import { traceError } from '../../../../common/logger';
@@ -12,8 +13,6 @@ import { EnvironmentType, PythonEnvironment } from '../../../info';
1213
import { lookForInterpretersInDirectory } from '../helpers';
1314
import { CacheableLocatorService } from './cacheableLocatorService';
1415

15-
const flatten = require('lodash/flatten') as typeof import('lodash/flatten');
16-
1716
@injectable()
1817
export class BaseVirtualEnvService extends CacheableLocatorService {
1918
private readonly virtualEnvMgr: IVirtualEnvironmentManager;
@@ -34,8 +33,10 @@ export class BaseVirtualEnvService extends CacheableLocatorService {
3433
this.fileSystem = serviceContainer.get<IFileSystem>(IFileSystem);
3534
}
3635

37-
// tslint:disable-next-line:no-empty
38-
public dispose() {}
36+
// eslint-disable-next-line class-methods-use-this
37+
public dispose(): void {
38+
noop();
39+
}
3940

4041
protected getInterpretersImplementation(resource?: Uri): Promise<PythonEnvironment[]> {
4142
return this.suggestionsFromKnownVenvs(resource);
@@ -53,7 +54,7 @@ export class BaseVirtualEnvService extends CacheableLocatorService {
5354
.getSubDirectories(pathToCheck)
5455
.then((subDirs) => Promise.all(this.getProspectiveDirectoriesForLookup(subDirs)))
5556
.then((dirs) => dirs.filter((dir) => dir.length > 0))
56-
.then((dirs) => Promise.all(dirs.map((d) => lookForInterpretersInDirectory(d, this.fileSystem))))
57+
.then((dirs) => Promise.all(dirs.map((d) => lookForInterpretersInDirectory(d))))
5758
.then((pathsWithInterpreters) => flatten(pathsWithInterpreters))
5859
.then((interpreters) => Promise.all(
5960
interpreters.map((interpreter) => this.getVirtualEnvDetails(interpreter, resource)),
@@ -92,16 +93,16 @@ export class BaseVirtualEnvService extends CacheableLocatorService {
9293
this.helper.getInterpreterInformation(interpreter),
9394
this.virtualEnvMgr.getEnvironmentName(interpreter, resource),
9495
this.virtualEnvMgr.getEnvironmentType(interpreter, resource),
95-
]).then(([details, virtualEnvName, type]) => {
96+
]).then(([details, virtualEnvName, type]):Promise<PythonEnvironment | undefined> => {
9697
if (!details) {
97-
return;
98+
return Promise.resolve(undefined);
9899
}
99100
this._hasInterpreters.resolve(true);
100-
return {
101+
return Promise.resolve({
101102
...(details as PythonEnvironment),
102103
envName: virtualEnvName,
103104
type: type! as EnvironmentType,
104-
};
105+
});
105106
});
106107
}
107108
}

0 commit comments

Comments
 (0)