eslint rules (#1099)

* Off to the races - 3760 problems (1794 errors, 1966 warnings)

* Convert Array<T> -> T[] (not all correct, eg. extends)

* () => { -> (): void  => {

* get is/as -> public get is/as

* public for getters, constructors & base Codec

* Some cleanups from previous overzealous search & replace

* Under 2k, 1874 problems (649 errors, 1225 warnings)

* ^^^ check that commit for details ;)

* Minor.

* rm config header

* Minor cleanups

* Default to tslint, eslint as option

* Trigger

* Update jsonrpc

* 1494 problems (433 errors, 1061 warnings)

* 1103 problems (135 errors, 968 warnings)

* 899 problems (9 errors, 890 warnings)

* 483 problems (9 errors, 474 warnings) (ignore any... for now)

* 366 problems (9 errors, 357 warnings)

* 336 problems (9 errors, 327 warnings)

* 148 problems (9 errors, 139 warnings)

* 31 problems (8 errors, 23 warnings)

* 0, 0
This commit is contained in:
Jaco Greeff
2019-07-14 12:15:14 +02:00
committed by GitHub
parent f770fb973f
commit 71c5920b1f
354 changed files with 3817 additions and 3632 deletions
+18 -18
View File
@@ -2,29 +2,29 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ContractABIFn$Arg } from './types';
import { ContractABIArg } from './types';
import typesAbi from '../test/abi/types.json';
import erc20Abi from '../test/contracts/erc20.json';
import { Abi } from '.';
describe('Abi', () => {
describe('types encoding', () => {
describe('Abi', (): void => {
describe('types encoding', (): void => {
let abi: Abi;
beforeAll(() => {
beforeAll((): void => {
abi = new Abi(typesAbi);
});
function check (method: string, args: Array<ContractABIFn$Arg>, type: string | null): void {
function check (method: string, args: ContractABIArg[], type: string | null): void {
const fn = abi.messages[method];
expect(fn.args).toEqual(args);
expect(fn.type).toEqual(type);
}
it('has the primitive method with args & return', () => {
it('has the primitive method with args & return', (): void => {
check(
'primitive',
[
@@ -35,7 +35,7 @@ describe('Abi', () => {
);
});
it('has the vector method with args & return', () => {
it('has the vector method with args & return', (): void => {
check(
'vector',
[
@@ -46,7 +46,7 @@ describe('Abi', () => {
);
});
it('has the vector_fixed method with args & return', () => {
it('has the vector_fixed method with args & return', (): void => {
check(
'vectorFixed',
[
@@ -56,7 +56,7 @@ describe('Abi', () => {
);
});
it('has the option method with args & return', () => {
it('has the option method with args & return', (): void => {
check(
'option',
[
@@ -66,7 +66,7 @@ describe('Abi', () => {
);
});
it('has the result method with return (empty)', () => {
it('has the result method with return (empty)', (): void => {
check(
'result',
[],
@@ -74,7 +74,7 @@ describe('Abi', () => {
);
});
it('allows for nested args', () => {
it('allows for nested args', (): void => {
check(
'nested',
[
@@ -84,14 +84,14 @@ describe('Abi', () => {
);
});
describe('messages', () => {
it('fails when invalid args are supplied', () => {
describe('messages', (): void => {
it('fails when invalid args are supplied', (): void => {
expect(
() => abi.messages.primitive(false)
(): Uint8Array => abi.messages.primitive(false)
).toThrow(/Expected 2 arguments to contract messages.primitive, found 1/);
});
it('encodes primitives correctly', () => {
it('encodes primitives correctly', (): void => {
expect(
abi.messages.primitive(true, 0x12345678)
).toEqual(new Uint8Array([
@@ -104,14 +104,14 @@ describe('Abi', () => {
});
});
describe('erc20', () => {
describe('erc20', (): void => {
let abi: Abi;
beforeEach(() => {
beforeEach((): void => {
abi = new Abi(erc20Abi);
});
it('has the attached methods', () => {
it('has the attached methods', (): void => {
expect(Object.keys(abi.messages)).toEqual(
['totalSupply', 'balanceOf', 'allowance', 'transfer', 'approve', 'transferFrom']
);
+9 -7
View File
@@ -2,25 +2,27 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ContractABI, ContractABIFn, IAbi, IAbi$Messages } from './types';
import { ContractABI, ContractABIFn, InterfaceAbi, AbiMessages } from './types';
import { stringCamelCase } from '@polkadot/util';
import { createMethod } from './method';
import { validateAbi } from './validation';
export default class ContractAbi implements IAbi {
readonly abi: ContractABI;
readonly deploy: ContractABIFn;
readonly messages: IAbi$Messages = {};
export default class ContractAbi implements InterfaceAbi {
public readonly abi: ContractABI;
constructor (abi: ContractABI) {
public readonly deploy: ContractABIFn;
public readonly messages: AbiMessages = {};
public constructor (abi: ContractABI) {
validateAbi(abi);
this.abi = abi;
this.deploy = createMethod('deploy', abi.deploy);
abi.messages.forEach((method) => {
abi.messages.forEach((method): void => {
const name = stringCamelCase(method.name);
this.messages[name] = createMethod(`messages.${name}`, method);
+8 -6
View File
@@ -3,19 +3,21 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { SubmittableModuleExtrinsics } from '@polkadot/api/types';
import { ApiObject, ContractABI, IContractBase } from './types';
import { ApiObject, ContractABI, ContractBase } from './types';
import { assert } from '@polkadot/util';
import Abi from './Abi';
// NOTE Experimental, POC, bound to change
export default abstract class RxBase implements IContractBase<'rxjs'> {
readonly abi: Abi;
readonly api: ApiObject<'rxjs'>;
readonly apiContracts: SubmittableModuleExtrinsics<'rxjs'>;
export default abstract class RxBase implements ContractBase<'rxjs'> {
public readonly abi: Abi;
constructor (api: ApiObject<'rxjs'>, abi: ContractABI | Abi) {
public readonly api: ApiObject<'rxjs'>;
public readonly apiContracts: SubmittableModuleExtrinsics<'rxjs'>;
public constructor (api: ApiObject<'rxjs'>, abi: ContractABI | Abi) {
this.abi = abi instanceof Abi
? abi
: new Abi(abi);
+10 -10
View File
@@ -16,16 +16,16 @@ import Abi from './Abi';
import RxBase from './RxBase';
import RxContract from './RxContract';
type IBlueprintCreateResultSubscription = Observable<BlueprintCreateResult>;
type BlueprintCreateResultSubscription = Observable<BlueprintCreateResult>;
export interface IBlueprintCreate {
signAndSend (account: IKeyringPair | string | AccountId | Address): IBlueprintCreateResultSubscription;
export interface BlueprintCreate {
signAndSend (account: IKeyringPair | string | AccountId | Address): BlueprintCreateResultSubscription;
}
class BlueprintCreateResult extends SubmittableResult {
readonly contract?: RxContract;
public readonly contract?: RxContract;
constructor (result: ISubmittableResult, contract?: RxContract) {
public constructor (result: ISubmittableResult, contract?: RxContract) {
super(result);
this.contract = contract;
@@ -34,21 +34,21 @@ class BlueprintCreateResult extends SubmittableResult {
// NOTE Experimental, POC, bound to change
export default class Blueprint extends RxBase {
readonly codeHash: Hash;
public readonly codeHash: Hash;
constructor (api: ApiRx, abi: ContractABI | Abi, codeHash: string | Hash) {
public constructor (api: ApiRx, abi: ContractABI | Abi, codeHash: string | Hash) {
super(api, abi);
this.codeHash = new Hash(codeHash);
}
public deployContract (endowment: number | BN, maxGas: number | BN, ...params: Array<any>): IBlueprintCreate {
const signAndSend = (account: IKeyringPair | string | AccountId | Address): IBlueprintCreateResultSubscription => {
public deployContract (endowment: number | BN, maxGas: number | BN, ...params: any[]): BlueprintCreate {
const signAndSend = (account: IKeyringPair | string | AccountId | Address): BlueprintCreateResultSubscription => {
return this.apiContracts
.create(endowment, maxGas, this.codeHash, this.abi.deploy(...params))
.signAndSend(account)
.pipe(
map((result: SubmittableResult) => {
map((result: SubmittableResult): BlueprintCreateResult => {
let contract: RxContract | undefined;
if (result.isFinalized) {
+11 -10
View File
@@ -20,7 +20,7 @@ import RxBlueprint from './RxBlueprint';
// Ok, tried, failed, eventually ... well, we are only trying with RxJs as a
// start, so take a big fat shortcut with this version, real intended version
// follows this and is commented out
type ICodePutCodeResultSubscription = Observable<CodePutCodeResult>;
type CodePutCodeResultSubscription = Observable<CodePutCodeResult>;
// type ICodePutCodeResultSubscription<ApiType> =
// ApiType extends 'rxjs'
// ? Observable<CodePutCodeResult>
@@ -31,14 +31,15 @@ type ICodePutCodeResultSubscription = Observable<CodePutCodeResult>;
// is hanging where, with the bare-mainimum from a simplicity perspective
// Very obvious is that it supports only 1 version of signAndSend
// (we need to revisit this, but probably ok for initial?)
export interface ICodePutCode {
signAndSend (account: IKeyringPair | string | AccountId | Address): ICodePutCodeResultSubscription;
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface CodePutCode {
signAndSend (account: IKeyringPair | string | AccountId | Address): CodePutCodeResultSubscription;
}
class CodePutCodeResult extends SubmittableResult {
readonly blueprint?: RxBlueprint;
public readonly blueprint?: RxBlueprint;
constructor (result: ISubmittableResult, blueprint?: RxBlueprint) {
public constructor (result: ISubmittableResult, blueprint?: RxBlueprint) {
super(result);
this.blueprint = blueprint;
@@ -47,21 +48,21 @@ class CodePutCodeResult extends SubmittableResult {
// NOTE Experimental, POC, bound to change
export default class RxCode extends RxBase {
readonly code: Uint8Array;
public readonly code: Uint8Array;
constructor (api: ApiRx, abi: ContractABI | Abi, wasm: string | Uint8Array) {
public constructor (api: ApiRx, abi: ContractABI | Abi, wasm: string | Uint8Array) {
super(api, abi);
this.code = u8aToU8a(wasm);
}
public createBlueprint (maxGas: number | BN): ICodePutCode {
const signAndSend = (account: IKeyringPair | string | AccountId | Address): ICodePutCodeResultSubscription => {
public createBlueprint (maxGas: number | BN): CodePutCode {
const signAndSend = (account: IKeyringPair | string | AccountId | Address): CodePutCodeResultSubscription => {
return this.apiContracts
.putCode(maxGas, compactAddLength(this.code))
.signAndSend(account)
.pipe(
map((result: ISubmittableResult) => {
map((result: ISubmittableResult): CodePutCodeResult => {
let blueprint: RxBlueprint | undefined;
if (result.isFinalized) {
+15 -12
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ContractABI, ContractABIFn, IContract, IContract$Calls } from './types';
import { ContractABI, ContractABIFn, InterfaceContract, InterfaceContractCalls } from './types';
import { IKeyringPair } from '@polkadot/types/types';
import BN from 'bn.js';
@@ -14,25 +14,28 @@ import { AccountId, Address } from '@polkadot/types';
import Abi from './Abi';
import RxBase from './RxBase';
export type IContractCallResultSubscription = Observable<SubmittableResult>;
export type ContractCallResultSubscription = Observable<SubmittableResult>;
export interface IContractCall {
signAndSend (account: IKeyringPair | string | AccountId | Address): IContractCallResultSubscription;
export interface ContractCall {
signAndSend (account: IKeyringPair | string | AccountId | Address): ContractCallResultSubscription;
}
// NOTE Experimental, POC, bound to change
export default class RxContract extends RxBase implements IContract {
readonly address: Address;
readonly calls: IContract$Calls = {};
type CallResult = (value: BN | number, maxGas: BN | number, ...params: any[]) => ContractCall;
constructor (api: ApiRx, abi: ContractABI | Abi, address: string | AccountId | Address) {
// NOTE Experimental, POC, bound to change
export default class RxContract extends RxBase implements InterfaceContract {
public readonly address: Address;
public readonly calls: InterfaceContractCalls = {};
public constructor (api: ApiRx, abi: ContractABI | Abi, address: string | AccountId | Address) {
super(api, abi);
this.address = new Address(address);
Object.entries(abi.messages).forEach(([name, fn]) => {
this.calls[name] = (fn: ContractABIFn) =>
(value: BN | number, maxGas: BN | number, ...params: Array<any>): IContractCall =>
Object.entries(abi.messages).forEach(([name]): void => {
this.calls[name] = (fn: ContractABIFn): CallResult =>
(value: BN | number, maxGas: BN | number, ...params: any[]): ContractCall =>
this.apiContracts.call(this.address, value, maxGas, fn(...params));
});
}
+7 -7
View File
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { CodecArg, Constructor } from '@polkadot/types/types';
import { ContractABIFn, ContractABIFn$Arg, ContractABIMethod, ContractABIMethodBase, ContractABITypes } from './types';
import { ContractABIFn, ContractABIArg, ContractABIMethod, ContractABIMethodBase, ContractABITypes } from './types';
import { Compact, createClass } from '@polkadot/types';
import { assert, isString, isUndefined, stringCamelCase } from '@polkadot/util';
@@ -12,7 +12,7 @@ export function typeToString (type: ContractABITypes): string {
if (isString(type)) {
return type;
} else if (Array.isArray(type)) {
return `(${type.map((type) => typeToString(type)).join(',')})`;
return `(${type.map((type): string => typeToString(type)).join(',')})`;
} else if (type['Option<T>']) {
return `Option<${typeToString(type['Option<T>'].T)}>`;
} else if (type['Result<T,E>']) {
@@ -26,10 +26,10 @@ export function typeToString (type: ContractABITypes): string {
throw new Error(`Unknown type specified ${JSON.stringify(type)}`);
}
export function createArgClass (args: Array<ContractABIFn$Arg>, baseDef: { [index: string]: string }): Constructor {
export function createArgClass (args: ContractABIArg[], baseDef: Record<string, string>): Constructor {
return createClass(
JSON.stringify(
args.reduce((base: { [index: string]: string }, { name, type }) => {
args.reduce((base: Record<string, any>, { name, type }): Record<string, any> => {
base[name] = type;
return base;
@@ -39,17 +39,17 @@ export function createArgClass (args: Array<ContractABIFn$Arg>, baseDef: { [inde
}
export function createMethod (name: string, method: Partial<ContractABIMethod> & ContractABIMethodBase): ContractABIFn {
const args: Array<ContractABIFn$Arg> = method.args.map(({ name, type }) => ({
const args: ContractABIArg[] = method.args.map(({ name, type }): ContractABIArg => ({
name: stringCamelCase(name),
type: typeToString(type)
}));
const Clazz = createArgClass(args, isUndefined(method.selector) ? {} : { __selector: 'u32' });
const baseStruct: { [index: string]: any } = { __selector: method.selector };
const encoder = (...params: Array<CodecArg>): Uint8Array => {
const encoder = (...params: CodecArg[]): Uint8Array => {
assert(params.length === args.length, `Expected ${args.length} arguments to contract ${name}, found ${params.length}`);
const u8a = new Clazz(
args.reduce((mapped, { name }, index) => {
args.reduce((mapped, { name }, index): Record<string, CodecArg> => {
mapped[name] = params[index];
return mapped;
+44 -46
View File
@@ -12,83 +12,81 @@ export type ApiObject<ApiType> = ApiType extends 'rxjs'
? ApiRx
: ApiPromise;
export type ContractABITypes$Struct = {
export interface ContractABITypesStruct {
'Option<T>'?: {
T: ContractABITypes
},
T: ContractABITypes;
};
'Result<T,E>'?: {
T: ContractABITypes,
E: ContractABITypes
},
T: ContractABITypes;
E: ContractABITypes;
};
'Vec<T>'?: {
T: ContractABITypes
},
T: ContractABITypes;
};
'[T;n]'?: {
T: ContractABITypes,
n: number
}
};
T: ContractABITypes;
n: number;
};
}
export type ContractABITypes = string | ContractABITypes$Struct | Array<string | ContractABITypes$Struct>;
export type ContractABITypes = string | ContractABITypesStruct | (string | ContractABITypesStruct)[];
export type ContractABIArg = {
name: string,
type: ContractABITypes
};
export interface ContractABIArg {
name: string;
type: ContractABITypes;
}
export type ContractABIMethodBase = {
args: Array<ContractABIArg>
};
export interface ContractABIMethodBase {
args: ContractABIArg[];
}
export type ContractABIMethod = ContractABIMethodBase & {
mutates?: boolean,
name: string,
selector: number,
return_type: ContractABITypes | null
};
export interface ContractABIMethod extends ContractABIMethodBase {
mutates?: boolean;
name: string;
selector: number;
return_type: ContractABITypes | null;
}
export type ContractABI = {
deploy: ContractABIMethodBase,
messages: Array<ContractABIMethod>,
name: string
};
export interface ContractABI {
deploy: ContractABIMethodBase;
messages: ContractABIMethod[];
name: string;
}
export interface ContractABIFn$Arg {
export interface ContractABIMetaArg {
name: string;
type: string;
}
export interface ContractABIFn$Meta {
args: Array<ContractABIFn$Arg>;
export interface ContractABIMeta {
args: ContractABIArg[];
isConstant: boolean;
type: string | null;
}
export interface ContractABIFn extends ContractABIFn$Meta {
(...args: Array<CodecArg>): Uint8Array;
export interface ContractABIFn extends ContractABIMeta {
(...args: CodecArg[]): Uint8Array;
}
export interface IAbi$Messages {
[index: string]: ContractABIFn;
}
export type AbiMessages = Record<string, ContractABIFn>;
export interface IAbi {
export interface InterfaceAbi {
readonly abi: ContractABI;
readonly deploy: ContractABIFn;
readonly messages: IAbi$Messages;
readonly messages: AbiMessages;
}
export interface IContractBase<ApiType> {
readonly abi: IAbi;
export interface ContractBase<ApiType> {
readonly abi: InterfaceAbi;
readonly api: ApiObject<ApiType>;
readonly apiContracts: SubmittableModuleExtrinsics<ApiType>;
}
export interface IContract$Calls {
export interface InterfaceContractCalls {
[index: string]: Function;
}
export interface IContract {
export interface InterfaceContract {
readonly address: Address;
readonly calls: IContract$Calls;
readonly calls: InterfaceContractCalls;
}
+7 -7
View File
@@ -6,11 +6,11 @@ import { ContractABI, ContractABIArg } from './types';
import { assert, isNumber, isNull, isString, isObject } from '@polkadot/util';
export function validateArgs (name: string, args: Array<ContractABIArg>): void {
export function validateArgs (name: string, args: ContractABIArg[]): void {
assert(Array.isArray(args), `Expected 'args' to exist on ${name}`);
args.forEach((arg) => {
const unknownKeys = Object.keys(arg).filter((key) => !['name', 'type'].includes(key));
args.forEach((arg): void => {
const unknownKeys = Object.keys(arg).filter((key): boolean => !['name', 'type'].includes(key));
assert(unknownKeys.length === 0, `Unknown keys ${unknownKeys.join(', ')} found in ABI args for ${name}`);
assert(isString(arg.name), `${name} args should have valid name `);
@@ -19,7 +19,7 @@ export function validateArgs (name: string, args: Array<ContractABIArg>): void {
}
export function validateDeploy ({ deploy }: ContractABI): void {
const unknownKeys = Object.keys(deploy).filter((key) => !['args'].includes(key));
const unknownKeys = Object.keys(deploy).filter((key): boolean => !['args'].includes(key));
assert(unknownKeys.length === 0, `Unknown keys ${unknownKeys.join(', ')} found in ABI deploy`);
@@ -27,8 +27,8 @@ export function validateDeploy ({ deploy }: ContractABI): void {
}
export function validateMethods ({ messages }: ContractABI): void {
messages.forEach((method) => {
const unknownKeys = Object.keys(method).filter((key) => !['args', 'mutates', 'name', 'selector', 'return_type'].includes(key));
messages.forEach((method): void => {
const unknownKeys = Object.keys(method).filter((key): boolean => !['args', 'mutates', 'name', 'selector', 'return_type'].includes(key));
assert(unknownKeys.length === 0, `Unknown keys ${unknownKeys.join(', ')} found in ABI args for messages.${method.name}`);
assert(isString(method.name), `Expected name for messages.${method.name}`);
@@ -40,7 +40,7 @@ export function validateMethods ({ messages }: ContractABI): void {
}
export function validateAbi (abi: ContractABI): void {
const unknownKeys = Object.keys(abi).filter((key) => !['deploy', 'messages', 'name'].includes(key));
const unknownKeys = Object.keys(abi).filter((key): boolean => !['deploy', 'messages', 'name'].includes(key));
assert(unknownKeys.length === 0, `Unknown fields ${unknownKeys.join(', ')} found in ABI`);
assert(abi.deploy && abi.messages && isString(abi.name), 'ABI should have deploy, messages & name sections');