Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up eslint errors #13951

Merged
merged 2 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 29 additions & 25 deletions src/client/pythonEnvironments/discovery/locators/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// tslint:disable-next-line: no-single-line-block-comment
/* eslint-disable max-classes-per-file */
import { inject, injectable } from 'inversify';
import { flatten } from 'lodash';
import {
Disposable, Event, EventEmitter, Uri,
} from 'vscode';
Expand Down Expand Up @@ -37,9 +40,6 @@ 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');
karthiknadig marked this conversation as resolved.
Show resolved Hide resolved

/**
* A wrapper around all locators used by the extension.
*/
Expand All @@ -49,7 +49,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 @@ -72,10 +72,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 @@ -85,10 +87,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 @@ -116,7 +118,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 @@ -130,9 +136,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 @@ -168,17 +174,10 @@ 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) {
if (candidate.scheme === uri.scheme) {
if (candidate.path === uri.path) {
return true;
} else if (candidate.path.startsWith(uriPath)) {
return true;
}
}
}
return false;
const uriPath = uri.path.endsWith('/') ? uri.path : '{uri.path}/';
const matchedUri = candidates.find((candidate) => (candidate.scheme === uri.scheme)
&& (candidate.path === uri.path || candidate.path.startsWith(uriPath)));
return matchedUri !== undefined;
}

/**
Expand All @@ -196,6 +195,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 @@ -206,14 +208,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 @@ -225,7 +227,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 @@ -286,7 +288,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