Typescript-eslint 2 (#1353)

* Make a start

* More minor cleanups

* Some more cleanups

* Last round?

* Exclude build folders
This commit is contained in:
Jaco Greeff
2019-09-02 07:01:32 +02:00
committed by GitHub
parent bd5c167c14
commit 2dd7cc0e69
94 changed files with 391 additions and 324 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ import { JsonRpcRequest, JsonRpcResponse, JsonRpcResponseBaseError } from '../ty
import { assert, isUndefined, isNumber } from '@polkadot/util';
export default class RpcCoder {
private id: number = 0;
private id = 0;
public decodeResponse (response: JsonRpcResponse): any {
assert(response, 'Empty response object received');
+2 -2
View File
@@ -113,7 +113,7 @@ export default class HttpProvider implements ProviderInterface {
/**
* @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/require-await
public async subscribe (types: string, method: string, params: any[], cb: ProviderInterfaceCallback): Promise<number> {
l.error(ERROR_SUBSCRIBE);
@@ -123,7 +123,7 @@ export default class HttpProvider implements ProviderInterface {
/**
* @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/require-await
public async unsubscribe (type: string, method: string, id: number): Promise<boolean> {
l.error(ERROR_SUBSCRIBE);
+5 -2
View File
@@ -46,7 +46,7 @@ export default class Mock implements ProviderInterface {
private emitter = new EventEmitter();
public isUpdating: boolean = true;
public isUpdating = true;
private requests: Record<string, (...params: any[]) => string> = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -74,7 +74,7 @@ export default class Mock implements ProviderInterface {
return subs;
}, ({} as unknown as MockStateSubscriptions));
private subscriptionId: number = 0;
private subscriptionId = 0;
private subscriptionMap: Record<number, string> = {};
@@ -102,6 +102,7 @@ export default class Mock implements ProviderInterface {
this.emitter.on(type, sub);
}
// eslint-disable-next-line @typescript-eslint/require-await
public async send (method: string, params: any[]): Promise<any> {
if (!this.requests[method]) {
throw new Error(`provider.send: Invalid method '${method}'`);
@@ -110,6 +111,7 @@ export default class Mock implements ProviderInterface {
return this.requests[method](this.db, params);
}
// eslint-disable-next-line @typescript-eslint/require-await
public async subscribe (type: string, method: string, ...params: any[]): Promise<number> {
l.debug((): any => ['subscribe', method, params]);
@@ -130,6 +132,7 @@ export default class Mock implements ProviderInterface {
throw new Error(`provider.subscribe: Invalid method '${method}'`);
}
// eslint-disable-next-line @typescript-eslint/require-await
public async unsubscribe (type: string, method: string, id: number): Promise<boolean> {
const sub = this.subscriptionMap[id];
+5 -4
View File
@@ -67,7 +67,7 @@ const l = logger('api-ws');
export default class WsProvider implements WSProviderInterface {
private _eventemitter: EventEmitter;
private _isConnected: boolean = false;
private _isConnected = false;
private autoConnect: boolean;
@@ -89,7 +89,7 @@ export default class WsProvider implements WSProviderInterface {
* @param {string} endpoint The endpoint url. Usually `ws://ip:9944` or `wss://ip:9944`
* @param {boolean} autoConnect Whether to connect automatically or not.
*/
public constructor (endpoint: string = defaults.WS_URL, autoConnect: boolean = true) {
public constructor (endpoint: string = defaults.WS_URL, autoConnect = true) {
assert(/^(wss|ws):\/\//.test(endpoint), `Endpoint should start with 'ws://', received '${endpoint}'`);
this._eventemitter = new EventEmitter();
@@ -376,6 +376,7 @@ export default class WsProvider implements WSProviderInterface {
this.subscriptions = {};
// eslint-disable-next-line @typescript-eslint/no-misused-promises
Object.keys(subscriptions).forEach(async (id): Promise<void> => {
const { callback, method, params, type } = subscriptions[id];
@@ -397,8 +398,8 @@ export default class WsProvider implements WSProviderInterface {
private sendQueue (): void {
Object.keys(this.queued).forEach((id): void => {
try {
// @ts-ignore we have done the websocket check in onSocketOpen, if an issue, will catch it
this.websocket.send(
// we have done the websocket check in onSocketOpen, if an issue, will catch it
(this.websocket as WebSocket).send(
this.queued[id]
);
+9 -14
View File
@@ -22,36 +22,31 @@ describe('onConnect', (): void => {
it('Does not connect when autoConnect is false', (): void => {
const provider: WsProvider = new WsProvider(TEST_WS_URL, false);
// We need to access the private WsProvider property 'websocket' here which would otherwise trigger a tslint error.
// @ts-ignore
expect(provider.websocket).toBeNull();
expect((provider as any).websocket).toBeNull();
});
it('Does connect when autoConnect is true', (): void => {
const provider: WsProvider = new WsProvider(TEST_WS_URL, true);
// @ts-ignore
expect(provider.websocket).not.toBeNull();
expect((provider as any).websocket).not.toBeNull();
});
it('Creates a new WebSocket instance by calling the connect() method', (): void => {
const provider: WsProvider = new WsProvider(TEST_WS_URL, false);
// @ts-ignore
expect(provider.websocket).toBeNull();
expect((provider as any).websocket).toBeNull();
provider.connect();
// @ts-ignore
expect(provider.websocket).not.toBeNull();
// @ts-ignore
expect(provider.websocket instanceof WebSocket).toBe(true);
expect((provider as any).websocket).not.toBeNull();
expect((provider as any).websocket instanceof WebSocket).toBe(true);
});
it('Creates the on handlers', (): void => {
const provider: WsProvider = new WsProvider(TEST_WS_URL);
// @ts-ignore
expect(provider.websocket).not.toBeNull();
// @ts-ignore
expect(provider.websocket).toEqual(expect.objectContaining({
expect((provider as any).websocket).not.toBeNull();
expect((provider as any).websocket).toEqual(expect.objectContaining({
listeners: expect.objectContaining({
close: [expect.any(Function)],
error: [expect.any(Function)],
@@ -13,8 +13,7 @@ describe('WsProvider', (): void => {
it('calls the handler when found', (done): void => {
// We need to access the private WsProvider property 'handlers' here which otherwise trigger a tslint error..
// @ts-ignore
provider.handlers[5] = {
(provider as any).handlers[5] = {
callback: (_: any, result: any): void => {
expect(result).toEqual('test');
done();
@@ -24,8 +23,7 @@ describe('WsProvider', (): void => {
};
// We need to access the private WsProvider property 'onSocketMessage' here which would otherwise trigger a tslint error.
// @ts-ignore
provider.onSocketMessage(
(provider as any).onSocketMessage(
new MessageEvent('test', { data: '{"jsonrpc":"2.0","id":5,"result":"test"}' })
);
});
@@ -13,8 +13,7 @@ describe('onMessageSubscribe', (): void => {
it('calls the subscriber with data', (done): void => {
// We need to access the private WsProvider property 'handlers' here which otherwise triggers a tslint error..
// @ts-ignore
provider.handlers[11] = {
(provider as any).handlers[11] = {
callback: (_: any, id: number): void => {
expect(typeof id).toBe('number');
},
@@ -30,16 +29,13 @@ describe('onMessageSubscribe', (): void => {
};
// We need to access the private WsProvider property 'onSocketMessage' here which would otherwise trigger a tslint error.
// @ts-ignore
provider.onSocketMessage(new MessageEvent('test', { data: '{"jsonrpc":"2.0","id":11,"result":22}' }));
// @ts-ignore
provider.onSocketMessage(new MessageEvent('test', { data: '{"jsonrpc":"2.0","method":"test","params":{"subscription":22,"result":"test"}}' }));
(provider as any).onSocketMessage(new MessageEvent('test', { data: '{"jsonrpc":"2.0","id":11,"result":22}' }));
(provider as any).onSocketMessage(new MessageEvent('test', { data: '{"jsonrpc":"2.0","method":"test","params":{"subscription":22,"result":"test"}}' }));
});
it('calls the subscriber with error', (done): void => {
// We need to access the private WsProvider property 'handlers' here which otherwise trigger a tslint error..
// @ts-ignore
provider.handlers[11] = {
(provider as any).handlers[11] = {
callback: (_: any, id: number): void => {
expect(typeof id).toBe('number');
},
@@ -55,9 +51,7 @@ describe('onMessageSubscribe', (): void => {
};
// We need to access the private WsProvider property 'onSocketMessage' here which would otherwise trigger a tslint error.
// @ts-ignore
provider.onSocketMessage(new MessageEvent('test', { data: '{"jsonrpc":"2.0","id":11,"result":22}' }));
// @ts-ignore
provider.onSocketMessage(new MessageEvent('test', { data: '{"jsonrpc":"2.0","method":"test","params":{"subscription":22,"error":{"message":"test"}}}' }));
(provider as any).onSocketMessage(new MessageEvent('test', { data: '{"jsonrpc":"2.0","id":11,"result":22}' }));
(provider as any).onSocketMessage(new MessageEvent('test', { data: '{"jsonrpc":"2.0","method":"test","params":{"subscription":22,"error":{"message":"test"}}}' }));
});
});
@@ -6,7 +6,7 @@ import { Constructor } from '@polkadot/types/types';
import { Global } from './../mock/types';
declare let global: Global;
declare const global: Global;
describe('ws/polyfill', (): void => {
let origWs: Constructor<WebSocket>;
+1 -2
View File
@@ -3,6 +3,5 @@
// of the Apache-2.0 license. See the LICENSE file for details.
if (typeof WebSocket === 'undefined') {
// @ts-ignore
global.WebSocket = require('websocket').w3cwebsocket;
(global as any).WebSocket = require('websocket').w3cwebsocket;
}
+5 -3
View File
@@ -9,6 +9,7 @@ import { Global, Mock } from './../mock/types';
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
declare const global: Global;
let provider: WsProvider;
let mock: Mock;
@@ -16,7 +17,7 @@ function createMock (requests: any[]): void {
mock = mockWs(requests);
}
function createWs (autoConnect: boolean = true): WsProvider {
function createWs (autoConnect = true): WsProvider {
provider = new WsProvider(TEST_WS_URL, autoConnect);
return provider;
@@ -47,8 +48,9 @@ describe('send', (): void => {
};
provider = createWs(true);
// @ts-ignore Accessing private method
provider.websocket.onopen();
// HACK this is a private method...
(provider as any).websocket.onopen();
return provider
.send('test_encoding', ['param'])
@@ -8,7 +8,8 @@ import WsProvider from './';
import { Global, Mock } from './../mock/types';
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
declare var global: Global;
declare const global: Global;
let ws: WsProvider;
let mock: Mock;
@@ -16,7 +17,7 @@ function createMock (requests: any[]): void {
mock = mockWs(requests);
}
function createWs (autoConnect: boolean = true): WsProvider {
function createWs (autoConnect = true): WsProvider {
ws = new WsProvider(TEST_WS_URL, autoConnect);
return ws;
@@ -8,7 +8,8 @@ import WsProvider from './';
import { Global, Mock } from './../mock/types';
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
declare var global: Global;
declare const global: Global;
let ws: WsProvider;
let mock: Mock;
@@ -16,7 +17,7 @@ function createMock (requests: any): void {
mock = mockWs(requests);
}
function createWs (autoConnect: boolean = true): WsProvider {
function createWs (autoConnect = true): WsProvider {
ws = new WsProvider(TEST_WS_URL, autoConnect);
return ws;