Skip to content

Commit

Permalink
Clean up eslint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig committed Sep 16, 2020
1 parent b21b2e1 commit ffffb6d
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 119 deletions.
15 changes: 7 additions & 8 deletions src/client/pythonEnvironments/discovery/locators/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { EnvironmentType, PythonEnvironment } from '../../info';

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

export async function lookForInterpretersInDirectory(pathToCheck: string, _: IFileSystem): Promise<string[]> {
export async function lookForInterpretersInDirectory(pathToCheck: string): Promise<string[]> {
// Technically, we should be able to use fs.getFiles(). However,
// that breaks some tests. So we stick with the broader behavior.
try {
Expand Down Expand Up @@ -40,9 +40,9 @@ export class InterpreterLocatorHelper implements IInterpreterLocatorHelper {
item.path = path.normalize(item.path);
return item;
})
.reduce<PythonEnvironment[]>((accumulator, current) => {
.reduce<PythonEnvironment[]>((accumulator, current:PythonEnvironment) => {
const currentVersion = current && current.version ? current.version.raw : undefined;
const existingItem = accumulator.find((item) => {
let existingItem = accumulator.find((item) => {
// If same version and same base path, then ignore.
// Could be Python 3.6 with path = python.exe, and Python 3.6 and path = python3.exe.
if (
Expand Down Expand Up @@ -76,12 +76,11 @@ export class InterpreterLocatorHelper implements IInterpreterLocatorHelper {
'sysVersion',
'version',
];
for (const prop of props) {
if (!existingItem[prop] && current[prop]) {
// tslint:disable-next-line: no-any
(existingItem as any)[prop] = current[prop];
props.forEach((prop) => {
if (existingItem && !existingItem[prop] && current[prop]) {
existingItem = { ...existingItem, [prop]: current[prop] };
}
}
});
}
return accumulator;
}, []);
Expand Down
53 changes: 33 additions & 20 deletions src/client/pythonEnvironments/discovery/locators/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable max-classes-per-file */
import { inject, injectable } from 'inversify';
import { flatten } from 'lodash';
import {
Disposable, Event, EventEmitter, Uri,
} from 'vscode';
Expand All @@ -21,15 +23,14 @@ import {
} from '../../../interpreter/contracts';
import { IServiceContainer } from '../../../ioc/types';
import { PythonEnvInfo } from '../../base/info';
import { ILocator, Locator, NOOP_ITERATOR, PythonEnvsIterator, PythonLocatorQuery } from '../../base/locator';
import {
ILocator, Locator, NOOP_ITERATOR, PythonEnvsIterator, PythonLocatorQuery,
} from '../../base/locator';
import { DisableableLocator, Locators } from '../../base/locators';
import { PythonEnvironment } from '../../info';
import { isHiddenInterpreter } from './services/interpreterFilter';
import { GetInterpreterLocatorOptions } from './types';

// tslint:disable-next-line:no-require-imports no-var-requires
const flatten = require('lodash/flatten') as typeof import('lodash/flatten');

/**
* A wrapper around all locators used by the extension.
*/
Expand All @@ -39,7 +40,7 @@ export class ExtensionLocators extends Locators {
nonWorkspace: ILocator[],
// This is expected to be a locator wrapping any found in
// the workspace (i.e. WorkspaceLocators).
workspace: ILocator
workspace: ILocator,
) {
super([...nonWorkspace, workspace]);
}
Expand All @@ -62,10 +63,12 @@ type RootURI = string;
*/
export class WorkspaceLocators extends Locator {
private readonly locators: Record<RootURI, DisableableLocator> = {};

private readonly roots: Record<RootURI, Uri> = {};

constructor(
// used to produce the per-root locators:
private readonly factories: WorkspaceLocatorFactory[]
private readonly factories: WorkspaceLocatorFactory[],
) {
super();
}
Expand All @@ -75,10 +78,10 @@ export class WorkspaceLocators extends Locator {
*
* @param folders - the info used to keep track of the workspace folders
*/
public activate(folders: IWorkspaceFolders) {
for (const root of folders.roots) {
public activate(folders: IWorkspaceFolders):void {
folders.roots.forEach((root) => {
this.addRoot(root);
}
});
folders.onAdded((root: Uri) => this.addRoot(root));
folders.onRemoved((root: Uri) => this.removeRoot(root));
}
Expand Down Expand Up @@ -106,7 +109,11 @@ export class WorkspaceLocators extends Locator {
}
}
// Fall back to checking all the roots.
// The eslint disable below should be removed after we have a
// better solution for these. We need asyncFind for this.
// eslint-disable-next-line no-restricted-syntax
for (const key of Object.keys(this.locators)) {
// eslint-disable-next-line no-await-in-loop
const resolved = await this.locators[key].resolveEnv(env);
if (resolved !== undefined) {
return resolved;
Expand All @@ -120,9 +127,9 @@ export class WorkspaceLocators extends Locator {
this.removeRoot(root);
// Create the root's locator, wrapping each factory-generated locator.
const locators: ILocator[] = [];
for (const create of this.factories) {
this.factories.forEach((create) => {
locators.push(...create(root));
}
});
const locator = new DisableableLocator(new Locators(locators));
// Cache it.
const key = root.toString();
Expand Down Expand Up @@ -158,17 +165,18 @@ export class WorkspaceLocators extends Locator {
* or the URI must be a parent of one of the candidates.
*/
function matchURI(uri: Uri, ...candidates: Uri[]): boolean {
const uriPath = uri.path.endsWith('/') ? uri.path : `{uri.path}/`;
for (const candidate of candidates) {
const uriPath = uri.path.endsWith('/') ? uri.path : '{uri.path}/';
const matchedUri = candidates.find((candidate) => {
if (candidate.scheme === uri.scheme) {
if (candidate.path === uri.path) {
return true;
} else if (candidate.path.startsWith(uriPath)) {
} if (candidate.path.startsWith(uriPath)) {
return true;
}
}
}
return false;
return false;
});
return matchedUri !== undefined;
}

/**
Expand All @@ -186,6 +194,9 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi

private readonly _hasInterpreters: Deferred<boolean>;

private readonly onLocatingEmitter:EventEmitter<Promise<PythonEnvironment[]>> =
new EventEmitter<Promise<PythonEnvironment[]>>();

constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) {
this._hasInterpreters = createDeferred<boolean>();
serviceContainer.get<Disposable[]>(IDisposableRegistry).push(this);
Expand All @@ -196,14 +207,14 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi

/**
* This class should never emit events when we're locating.
* The events will be fired by the indivitual locators retrieved in `getLocators`.
* The events will be fired by the individual locators retrieved in `getLocators`.
*
* @readonly
* @type {Event<Promise<PythonEnvironment[]>>}
* @memberof PythonInterpreterLocatorService
*/
public get onLocating(): Event<Promise<PythonEnvironment[]>> {
return new EventEmitter<Promise<PythonEnvironment[]>>().event;
return this.onLocatingEmitter.event;
}

public get hasInterpreters(): Promise<boolean> {
Expand All @@ -215,7 +226,7 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
*
* Called by VS Code to indicate it is done with the resource.
*/
public dispose() {
public dispose():void {
this.disposables.forEach((disposable) => disposable.dispose());
}

Expand Down Expand Up @@ -276,7 +287,9 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
// Set it to true the first time the user selects an interpreter
if (!this.didTriggerInterpreterSuggestions && options?.onSuggestion === true) {
this.didTriggerInterpreterSuggestions = true;
locators.forEach((locator) => (locator.didTriggerInterpreterSuggestions = true));
locators.forEach((locator) => {
locator.didTriggerInterpreterSuggestions = true;
});
}

return locators;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ export class InterpreterLocatorProgressService implements IInterpreterLocatorPro
this.refreshing.fire();
}

private checkProgress() {
private checkProgress(): void {
if (this.deferreds.length === 0) {
return;
}
if (this.areAllItemsComplete()) {
return this.notifyCompleted();
this.notifyCompleted();
return;
}
Promise.all(this.deferreds.map((item) => item.promise))
.catch(noop)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class KnownPathsService extends CacheableLocatorService {
const fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
return fs
.directoryExists(dir)
.then((exists) => (exists ? lookForInterpretersInDirectory(dir, fs) : Promise.resolve<string[]>([])));
.then((exists) => (exists ? lookForInterpretersInDirectory(dir) : Promise.resolve<string[]>([])));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// tslint:disable:no-unnecessary-callback-wrapper no-require-imports no-var-requires

import { injectable, unmanaged } from 'inversify';
import { flatten, noop } from 'lodash';
import * as path from 'path';
import { Uri } from 'vscode';
import { traceError } from '../../../../common/logger';
Expand All @@ -12,8 +13,6 @@ import { EnvironmentType, PythonEnvironment } from '../../../info';
import { lookForInterpretersInDirectory } from '../helpers';
import { CacheableLocatorService } from './cacheableLocatorService';

const flatten = require('lodash/flatten') as typeof import('lodash/flatten');

@injectable()
export class BaseVirtualEnvService extends CacheableLocatorService {
private readonly virtualEnvMgr: IVirtualEnvironmentManager;
Expand All @@ -34,8 +33,10 @@ export class BaseVirtualEnvService extends CacheableLocatorService {
this.fileSystem = serviceContainer.get<IFileSystem>(IFileSystem);
}

// tslint:disable-next-line:no-empty
public dispose() {}
// eslint-disable-next-line class-methods-use-this
public dispose(): void {
noop();
}

protected getInterpretersImplementation(resource?: Uri): Promise<PythonEnvironment[]> {
return this.suggestionsFromKnownVenvs(resource);
Expand All @@ -53,7 +54,7 @@ export class BaseVirtualEnvService extends CacheableLocatorService {
.getSubDirectories(pathToCheck)
.then((subDirs) => Promise.all(this.getProspectiveDirectoriesForLookup(subDirs)))
.then((dirs) => dirs.filter((dir) => dir.length > 0))
.then((dirs) => Promise.all(dirs.map((d) => lookForInterpretersInDirectory(d, this.fileSystem))))
.then((dirs) => Promise.all(dirs.map((d) => lookForInterpretersInDirectory(d))))
.then((pathsWithInterpreters) => flatten(pathsWithInterpreters))
.then((interpreters) => Promise.all(
interpreters.map((interpreter) => this.getVirtualEnvDetails(interpreter, resource)),
Expand Down Expand Up @@ -92,16 +93,16 @@ export class BaseVirtualEnvService extends CacheableLocatorService {
this.helper.getInterpreterInformation(interpreter),
this.virtualEnvMgr.getEnvironmentName(interpreter, resource),
this.virtualEnvMgr.getEnvironmentType(interpreter, resource),
]).then(([details, virtualEnvName, type]) => {
]).then(([details, virtualEnvName, type]):Promise<PythonEnvironment | undefined> => {
if (!details) {
return;
return Promise.resolve(undefined);
}
this._hasInterpreters.resolve(true);
return {
return Promise.resolve({
...(details as PythonEnvironment),
envName: virtualEnvName,
type: type! as EnvironmentType,
};
});
});
}
}
Loading

0 comments on commit ffffb6d

Please sign in to comment.