Skip to content

Commit

Permalink
Remove any types
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Jul 22, 2022
1 parent 0e52a3c commit 8180561
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 77 deletions.
6 changes: 2 additions & 4 deletions dev-packages/ovsx-client/src/ovsx-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,8 @@ export interface VSXResponseError extends Error {
}

export namespace VSXResponseError {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function is(error: any): error is VSXResponseError {
return !!error && typeof error === 'object'
&& 'statusCode' in error && typeof error['statusCode'] === 'number';
export function is(error: unknown): error is VSXResponseError {
return !!error && typeof error === 'object' && typeof (error as VSXResponseError).statusCode === 'number';
}
}

Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/browser/decorations-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// *****************************************************************************

import { injectable } from 'inversify';
import { isThenable } from '../common/promise-util';
import { CancellationToken, CancellationTokenSource, Disposable, Emitter, Event } from '../common';
import { TernarySearchTree } from '../common/ternary-search-tree';
import URI from '../common/uri';
Expand Down Expand Up @@ -157,12 +158,6 @@ class DecorationProviderWrapper {
this.data.set(uri, request);
return undefined;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isThenable<T>(obj: any): obj is Promise<T> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return obj && typeof (<Promise<any>>obj).then === 'function';
}
}

private keepItem(uri: URI, data: Decoration | undefined): Decoration | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export interface OverridePreferenceName {
overrideIdentifier: string
}
export namespace OverridePreferenceName {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function is(arg: any): arg is OverridePreferenceName {
export function is(arg: unknown): arg is OverridePreferenceName {
return !!arg && typeof arg === 'object' && 'preferenceName' in arg && 'overrideIdentifier' in arg;
}
}
Expand All @@ -37,8 +36,7 @@ export const getOverridePattern = (identifier: string) => `\\[(${identifier})\\]
export class PreferenceLanguageOverrideService {
protected readonly overrideIdentifiers = new Set<string>();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
testOverrideValue(name: string, value: any): value is PreferenceSchemaProperties {
testOverrideValue(name: string, value: unknown): value is PreferenceSchemaProperties {
return PreferenceSchemaProperties.is(value) && OVERRIDE_PROPERTY_PATTERN.test(name);
}

Expand Down
24 changes: 9 additions & 15 deletions packages/core/src/browser/saveable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,13 @@ export namespace Saveable {
}

export type Snapshot = { value: string } | { read(): string | null };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isSource(arg: any): arg is SaveableSource {
return !!arg && ('saveable' in arg) && is(arg.saveable);
export function isSource(arg: unknown): arg is SaveableSource {
return typeof arg === 'object' && !!arg && is((arg as SaveableSource).saveable);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function is(arg: any): arg is Saveable {
return !!arg && ('dirty' in arg) && ('onDirtyChanged' in arg);
export function is(arg: unknown): arg is Saveable {
return typeof arg === 'object' && !!arg && 'dirty' in arg && 'onDirtyChanged' in arg;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function get(arg: any): Saveable | undefined {
export function get(arg: unknown): Saveable | undefined {
if (is(arg)) {
return arg;
}
Expand All @@ -77,20 +74,17 @@ export namespace Saveable {
}
return undefined;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getDirty(arg: any): Saveable | undefined {
export function getDirty(arg: unknown): Saveable | undefined {
const saveable = get(arg);
if (saveable && saveable.dirty) {
return saveable;
}
return undefined;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isDirty(arg: any): boolean {
export function isDirty(arg: unknown): boolean {
return !!getDirty(arg);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function save(arg: any, options?: SaveOptions): Promise<void> {
export async function save(arg: unknown, options?: SaveOptions): Promise<void> {
const saveable = get(arg);
if (saveable) {
await saveable.save(options);
Expand Down Expand Up @@ -229,7 +223,7 @@ export namespace SaveableWidget {
return !!widget && 'closeWithoutSaving' in widget;
}
export function getDirty<T extends Widget>(widgets: Iterable<T>): IterableIterator<SaveableWidget & T> {
return get(widgets, Saveable.isDirty);
return get<T>(widgets, Saveable.isDirty);
}
export function* get<T extends Widget>(
widgets: Iterable<T>,
Expand Down
9 changes: 4 additions & 5 deletions packages/core/src/browser/shell/application-shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1934,10 +1934,9 @@ export namespace ApplicationShell {
return area === 'left' || area === 'right' || area === 'bottom';
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isValidArea(area?: any): area is ApplicationShell.Area {
export function isValidArea(area?: unknown): area is ApplicationShell.Area {
const areas = ['main', 'top', 'left', 'right', 'bottom'];
return (area !== undefined && typeof area === 'string' && areas.includes(area));
return typeof area === 'string' && areas.includes(area);
}

/**
Expand Down Expand Up @@ -1981,8 +1980,8 @@ export namespace ApplicationShell {
* Whether a widget should be opened to the side tab bar relatively to the reference widget.
*/
export type OpenToSideMode = 'open-to-left' | 'open-to-right';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isOpenToSideMode(mode: OpenToSideMode | any): mode is OpenToSideMode {

export function isOpenToSideMode(mode: unknown): mode is OpenToSideMode {
return mode === 'open-to-left' || mode === 'open-to-right';
}

Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/browser/shell/shell-layout-restorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ export interface StatefulWidget {
}

export namespace StatefulWidget {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function is(arg: any): arg is StatefulWidget {
return arg !== undefined && typeof arg['storeState'] === 'function' && typeof arg['restoreState'] === 'function';
export function is(arg: unknown): arg is StatefulWidget {
return !!arg && typeof arg === 'object' && typeof (arg as StatefulWidget).storeState === 'function' && typeof (arg as StatefulWidget).restoreState === 'function';
}
}

Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/browser/source-tree/tree-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ export interface CompositeTreeElement extends TreeElement {
getElements(): MaybePromise<IterableIterator<TreeElement>>
}
export namespace CompositeTreeElement {
/* eslint-disable @typescript-eslint/no-explicit-any */
export function is(element: CompositeTreeElement | any): element is CompositeTreeElement {
return !!element && 'getElements' in element;
export function is(element: unknown): element is CompositeTreeElement {
return !!element && typeof element === 'object' && 'getElements' in element;
}
export function hasElements(element: CompositeTreeElement | any): element is CompositeTreeElement {
export function hasElements(element: unknown): element is CompositeTreeElement {
return is(element) && element.hasElements !== false;
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/common/cancellation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ export namespace CancellationToken {
onCancellationRequested: shortcutEvent
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function is(value: any): value is CancellationToken {
export function is(value: unknown): value is CancellationToken {
const candidate = value as CancellationToken;
return candidate && (candidate === CancellationToken.None
|| candidate === CancellationToken.Cancelled
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/common/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ export interface Command {

export namespace Command {
/* Determine whether object is a Command */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function is(arg: Command | any): arg is Command {
return !!arg && arg === Object(arg) && 'id' in arg;
export function is(arg: unknown): arg is Command {
return !!arg && typeof arg === 'object' && 'id' in arg;
}

/** Utility function to easily translate commands */
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/common/disposable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ export interface Disposable {
}

export namespace Disposable {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function is(arg: any): arg is Disposable {
return !!arg && typeof arg === 'object' && 'dispose' in arg && typeof arg['dispose'] === 'function';
export function is(arg: unknown): arg is Disposable {
return !!arg && typeof arg === 'object' && typeof (arg as Disposable).dispose === 'function';
}
export function create(func: () => void): Disposable {
return { dispose: func };
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/common/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,11 +549,10 @@ function listToMap(list: string[]): Record<string, true> {
return map;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isRelativePattern(obj: any): obj is IRelativePattern {
export function isRelativePattern(obj: unknown): obj is IRelativePattern {
const rp = obj as IRelativePattern;

return rp && typeof rp.base === 'string' && typeof rp.pattern === 'string' && typeof rp.pathToRelative === 'function';
return !!rp && typeof rp === 'object' && typeof rp.base === 'string' && typeof rp.pattern === 'string' && typeof rp.pathToRelative === 'function';
}

/**
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/common/keybinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ export namespace Keybinding {
}

/* Determine whether object is a KeyBinding */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function is(arg: Keybinding | any): arg is Keybinding {
return !!arg && arg === Object(arg) && 'command' in arg && 'keybinding' in arg;
export function is(arg: unknown): arg is Keybinding {
return !!arg && typeof arg === 'object' && 'command' in arg && 'keybinding' in arg;
}
}

Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/common/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,8 @@ export namespace SpecialCases {

export namespace Key {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isKey(arg: any): arg is Key {
return typeof arg === 'object' && ('code' in arg) && ('keyCode' in arg);
export function isKey(arg: unknown): arg is Key {
return !!arg && typeof arg === 'object' && 'code' in arg && 'keyCode' in arg;
}

export function getKey(arg: string | number): Key | undefined {
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/common/lsp-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ export interface TextDocumentContentChangeDelta {
}
export namespace TextDocumentContentChangeDelta {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function is(arg: any): arg is TextDocumentContentChangeDelta {
return !!arg && typeof arg['text'] === 'string' && (typeof arg['rangeLength'] === 'number' || typeof arg['rangeLength'] === 'undefined') && Range.is(arg['range']);
export function is(arg: unknown): arg is TextDocumentContentChangeDelta {
const changeDelta = arg as TextDocumentContentChangeDelta;
return !!changeDelta
&& typeof changeDelta === 'object'
&& typeof changeDelta.text === 'string'
&& (typeof changeDelta.rangeLength === 'number' || typeof changeDelta.rangeLength === 'undefined')
&& Range.is(changeDelta.range);
}

}
5 changes: 2 additions & 3 deletions packages/core/src/common/menu/menu-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ export interface MenuAction extends MenuNodeRenderingData, Pick<MenuNodeMetadata

export namespace MenuAction {
/* Determine whether object is a MenuAction */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function is(arg: MenuAction | any): arg is MenuAction {
return !!arg && arg === Object(arg) && 'commandId' in arg;
export function is(arg: unknown): arg is MenuAction {
return !!arg && typeof arg === 'object' && 'commandId' in arg;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/preferences/preference-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface PreferenceSchemaProperties {
[name: string]: PreferenceSchemaProperty
}
export namespace PreferenceSchemaProperties {
export function is(obj: Object | undefined): obj is PreferenceSchemaProperties {
export function is(obj: unknown): obj is PreferenceSchemaProperties {
return !!obj && typeof obj === 'object';
}
}
Expand Down
50 changes: 50 additions & 0 deletions packages/core/src/common/preferences/preference-scope.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// *****************************************************************************
// Copyright (C) 2022 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { expect } from 'chai';
import { PreferenceScope } from './preference-scope';

describe('PreferenceScope', () => {

it('getScopes() should return numbers from broadest to narrowest', () => {
expect(PreferenceScope.getScopes()).deep.equal([0, 1, 2, 3]);
});

it('getReversedScopes() should return numbers from narrowest to broadest', () => {
expect(PreferenceScope.getReversedScopes()).deep.equal([3, 2, 1, 0]);
});

it('getScopeNames() should return the names of scopes broader than the current one', () => {
expect(PreferenceScope.getScopeNames(PreferenceScope.Workspace)).deep.equal(['Default', 'User', 'Workspace']);
});

it('is() returns whether a value is a preference scope', () => {
/* eslint-disable no-unused-expressions */
expect(PreferenceScope.is(PreferenceScope.Default)).to.be.true;
expect(PreferenceScope.is(PreferenceScope.User)).to.be.true;
expect(PreferenceScope.is(PreferenceScope.Workspace)).to.be.true;
expect(PreferenceScope.is(PreferenceScope.Folder)).to.be.true;
expect(PreferenceScope.is(0)).to.be.true;
expect(PreferenceScope.is(1)).to.be.true;
expect(PreferenceScope.is(2)).to.be.true;
expect(PreferenceScope.is(3)).to.be.true;
expect(PreferenceScope.is(4)).to.be.false;
expect(PreferenceScope.is(-1)).to.be.false;
expect(PreferenceScope.is({})).to.be.false;
expect(PreferenceScope.is('Default')).to.be.false;
/* eslint-enable no-unused-expressions */
});
});
19 changes: 7 additions & 12 deletions packages/core/src/common/preferences/preference-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

/* eslint-disable @typescript-eslint/no-explicit-any */

export enum PreferenceScope {
Default,
User,
Expand All @@ -24,17 +22,15 @@ export enum PreferenceScope {
}

export namespace PreferenceScope {
export function is(scope: any): scope is PreferenceScope {
return typeof scope === 'number' && getScopes().findIndex(s => s === scope) >= 0;
export function is(scope: unknown): scope is PreferenceScope {
return typeof scope === 'number' && getScopes().includes(scope);
}

/**
* @returns preference scopes from broadest to narrowest: Default -> Folder.
*/
export function getScopes(): PreferenceScope[] {
return Object.keys(PreferenceScope)
.filter(k => typeof PreferenceScope[k as any] === 'string')
.map(v => <PreferenceScope>Number(v));
return Object.values(PreferenceScope).filter(nameOrIndex => !isNaN(Number(nameOrIndex))) as PreferenceScope[];
}

/**
Expand All @@ -46,12 +42,11 @@ export namespace PreferenceScope {

export function getScopeNames(scope?: PreferenceScope): string[] {
const names: string[] = [];
const allNames = Object.keys(PreferenceScope)
.filter(k => typeof PreferenceScope[k as any] === 'number');
const scopes = getScopes();
if (scope) {
for (const name of allNames) {
if ((<any>PreferenceScope)[name] <= scope) {
names.push(name);
for (const scopeIndex of scopes) {
if (scopeIndex <= scope) {
names.push(PreferenceScope[scopeIndex]);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/common/promise-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@ export function waitForEvent<T>(event: Event<T>, ms: number, thisArg?: any, disp

});
}

export function isThenable<T>(obj: unknown): obj is Promise<T> {
return typeof obj === 'object' && !!obj && typeof (obj as Promise<unknown>).then === 'function';
}

0 comments on commit 8180561

Please sign in to comment.