Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.

Commit 930f2da

Browse files
authored
Merge pull request #52 from paritytech/am-strict
Use TS strict mode
2 parents dc0bb7d + 222a64c commit 930f2da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+311
-192
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ module.exports = {
1111
'^.+\\.tsx?$': 'ts-jest'
1212
},
1313
// testRegex: 'spec\\.(ts|tsx)$' // TODO Skip api/ tests for now, as it's still WIP
14-
testRegex: `packages/(abi|electron|light\.js|light\.js-react)/.*spec\\.(ts|tsx)$`
14+
testRegex: `packages/(abi|contracts|electron|light\.js|light\.js-react)/.*spec\\.(ts|tsx)$`
1515
};

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"rimraf": "^2.6.2",
3131
"ts-jest": "^23.0.0",
3232
"tslint": "^5.10.0",
33-
"tslint-config-prettier": "^1.14.0",
3433
"tslint-config-semistandard": "^7.0.0",
3534
"typedoc": "^0.12.0",
3635
"typedoc-plugin-markdown": "^1.1.13",

packages/abi/src/decoder/decoder.spec.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import { padU32 } from '../util/pad';
1212

1313
describe('decoder/Decoder', () => {
1414
const stringToBytes = function (str: string) {
15-
return str.match(/.{1,2}/g).map(code => parseInt(code, 16));
15+
const matches = str.match(/.{1,2}/g);
16+
if (!matches) {
17+
throw new Error('stringToBytes: mo matches');
18+
}
19+
return matches.map(code => parseInt(code, 16));
1620
};
1721

1822
const address1 =
@@ -118,9 +122,9 @@ describe('decoder/Decoder', () => {
118122

119123
describe('decodeParam', () => {
120124
it('throws an error on non ParamType param', () => {
121-
expect(() => Decoder.decodeParam({} as ParamType, undefined, undefined)).toThrow(
122-
/ParamType/
123-
);
125+
expect(() =>
126+
Decoder.decodeParam({} as ParamType, undefined, undefined)
127+
).toThrow(/ParamType/);
124128
});
125129

126130
it('throws an error on invalid param type', () => {
@@ -172,8 +176,11 @@ describe('decoder/Decoder', () => {
172176

173177
it('decodes fixedBytes', () => {
174178
expect(
175-
Decoder.decodeParam(new ParamType('fixedBytes', null, 2), [bytes1], 0)
176-
.token
179+
Decoder.decodeParam(
180+
new ParamType('fixedBytes', undefined, 2),
181+
[bytes1],
182+
0
183+
).token
177184
).toEqual(tokenFixedBytes1);
178185
});
179186

@@ -209,10 +216,14 @@ describe('decoder/Decoder', () => {
209216

210217
it('decodes string (indexed)', () => {
211218
expect(
212-
Decoder.decodeParam(new ParamType('string', null, 0, true), [bytes1], 0)
219+
Decoder.decodeParam(
220+
new ParamType('string', undefined, 0, true),
221+
[bytes1],
222+
0
223+
)
213224
).toEqual(
214225
Decoder.decodeParam(
215-
new ParamType('fixedBytes', null, 32, true),
226+
new ParamType('fixedBytes', undefined, 32, true),
216227
[bytes1],
217228
0
218229
)
@@ -222,7 +233,7 @@ describe('decoder/Decoder', () => {
222233

223234
describe('decode', () => {
224235
it('throws an error on invalid params', () => {
225-
expect(() => Decoder.decode(null, '123')).toThrow(/array/);
236+
expect(() => Decoder.decode(undefined, '123')).toThrow(/array/);
226237
});
227238

228239
describe('address', () => {
@@ -300,7 +311,10 @@ describe('decoder/Decoder', () => {
300311
describe('fixedBytes', () => {
301312
it('decodes fixedBytes', () => {
302313
expect(
303-
Decoder.decode([new ParamType('fixedBytes', null, 2)], `${bytes1}`)
314+
Decoder.decode(
315+
[new ParamType('fixedBytes', undefined, 2)],
316+
`${bytes1}`
317+
)
304318
).toEqual([tokenFixedBytes1]);
305319
});
306320
});

packages/abi/src/decoder/decoder.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import ParamType from '../spec/paramType/paramType';
1212
import { sliceData } from '../util/slice';
1313
import { asAddress, asBool, asI32, asU32 } from '../util/sliceAs';
1414
import { isArray, isInstanceOf } from '../util/types';
15-
import { TokenValue } from '../types';
15+
import { Slices } from '../types';
1616

1717
const NULL = '0000000000000000000000000000000000000000000000000000000000000000';
1818

1919
class Decoder {
20-
static decode (params: ParamType[], data: string) {
20+
static decode (params: ParamType[] | undefined, data: string) {
2121
if (!isArray(params)) {
2222
throw new Error('Parameters should be array of ParamType');
2323
}
@@ -33,15 +33,15 @@ class Decoder {
3333
});
3434
}
3535

36-
static peek (slices: string[], position: number) {
36+
static peek (slices: Slices, position: number) {
3737
if (!slices || !slices[position]) {
3838
return NULL;
3939
}
4040

4141
return slices[position];
4242
}
4343

44-
static takeBytes (slices: string[], position: number, length: number) {
44+
static takeBytes (slices: Slices, position: number, length: number) {
4545
const slicesLength = Math.floor((length + 31) / 32);
4646
let bytesStr = '';
4747

@@ -58,8 +58,8 @@ class Decoder {
5858

5959
static decodeParam (
6060
param: ParamType,
61-
slices: string[],
62-
offset: number
61+
slices: Slices,
62+
offset: number = 0
6363
): DecodeResult {
6464
if (!isInstanceOf(param, ParamType)) {
6565
throw new Error('param should be instanceof ParamType');
@@ -96,24 +96,24 @@ class Decoder {
9696
offset + 1
9797
);
9898

99-
case 'fixedBytes':
99+
case 'fixedBytes': {
100100
taken = Decoder.takeBytes(slices, offset, param.length);
101101

102102
return new DecodeResult(
103103
new Token(param.type, taken.bytes),
104104
taken.newOffset
105105
);
106-
107-
case 'bytes':
106+
}
107+
case 'bytes': {
108108
lengthOffset = asU32(Decoder.peek(slices, offset))
109109
.div(32)
110110
.toNumber();
111111
length = asU32(Decoder.peek(slices, lengthOffset)).toNumber();
112112
taken = Decoder.takeBytes(slices, lengthOffset + 1, length);
113113

114114
return new DecodeResult(new Token(param.type, taken.bytes), offset + 1);
115-
116-
case 'string':
115+
}
116+
case 'string': {
117117
if (param.indexed) {
118118
taken = Decoder.takeBytes(slices, offset, 32);
119119

@@ -142,8 +142,14 @@ class Decoder {
142142
}
143143

144144
return new DecodeResult(new Token(param.type, decoded), offset + 1);
145+
}
146+
case 'array': {
147+
if (!param.subtype) {
148+
throw new Error(
149+
`decodeParam: param of type '${param.type}' must have a subtype`
150+
);
151+
}
145152

146-
case 'array':
147153
lengthOffset = asU32(Decoder.peek(slices, offset))
148154
.div(32)
149155
.toNumber();
@@ -158,8 +164,14 @@ class Decoder {
158164
}
159165

160166
return new DecodeResult(new Token(param.type, tokens), offset + 1);
167+
}
168+
case 'fixedArray': {
169+
if (!param.subtype) {
170+
throw new Error(
171+
`decodeParam: param of type '${param.type}' must have a subtype`
172+
);
173+
}
161174

162-
case 'fixedArray':
163175
newOffset = offset;
164176

165177
for (let index = 0; index < param.length; index++) {
@@ -170,7 +182,7 @@ class Decoder {
170182
}
171183

172184
return new DecodeResult(new Token(param.type, tokens), newOffset);
173-
185+
}
174186
default:
175187
throw new Error(`Invalid param type ${param.type} in decodeParam`);
176188
}

packages/abi/src/encoder/encoder.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { padAddress, padFixedBytes, padU32 } from '../util/pad';
1010
describe('encoder/Encoder', () => {
1111
describe('encodeToken', () => {
1212
it('requires token as Token', () => {
13-
expect(() => Encoder.encodeToken(undefined)).toThrow(/Token/);
13+
expect(() => Encoder.encodeToken(undefined as any)).toThrow(/Token/);
1414
});
1515

1616
it('encodes address tokens in Mediate(raw)', () => {
@@ -105,7 +105,7 @@ describe('encoder/Encoder', () => {
105105

106106
describe('encode', () => {
107107
it('requires tokens array', () => {
108-
expect(() => Encoder.encode(undefined)).toThrow(/array/);
108+
expect(() => Encoder.encode(undefined as any)).toThrow(/array/);
109109
});
110110

111111
describe('addresses', () => {

packages/abi/src/encoder/encoder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Encoder {
4545
}
4646

4747
static encodeToken (token: Token, index = 0): Mediate {
48-
if (!isInstanceOf(token, Token)) {
48+
if (!token || !isInstanceOf(token, Token)) {
4949
throw new Error('token should be instanceof Token');
5050
}
5151

packages/abi/src/spec/event/decodedLogParam.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('spec/event/DecodedLogParam', () => {
1414

1515
it('disallows kind not instanceof ParamType', () => {
1616
expect(
17-
() => new DecodedLogParam('test', 'param' as any, undefined)
17+
() => new DecodedLogParam('test', 'param' as any, undefined as any)
1818
).toThrow(/ParamType/);
1919
});
2020

packages/abi/src/spec/event/event.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('spec/event/Event', () => {
5151
it('returns all the types', () => {
5252
expect(event.inputParamTypes()).toEqual([
5353
new ParamType('bool'),
54-
new ParamType('uint', null, 256, true)
54+
new ParamType('uint', undefined, 256, true)
5555
]);
5656
});
5757
});
@@ -98,12 +98,12 @@ describe('spec/event/Event', () => {
9898
expect(decoded.params).toEqual([
9999
new DecodedLogParam(
100100
'a',
101-
new ParamType('int', null, 256),
101+
new ParamType('int', undefined, 256),
102102
new Token('int', new BigNumber(3))
103103
),
104104
new DecodedLogParam(
105105
'b',
106-
new ParamType('int', null, 256, true),
106+
new ParamType('int', undefined, 256, true),
107107
new Token('int', new BigNumber(2))
108108
),
109109
new DecodedLogParam(
@@ -113,7 +113,7 @@ describe('spec/event/Event', () => {
113113
),
114114
new DecodedLogParam(
115115
'd',
116-
new ParamType('address', null, 0, true),
116+
new ParamType('address', undefined, 0, true),
117117
new Token('address', '0x1111111111111111111111111111111111111111')
118118
)
119119
]);
@@ -134,7 +134,7 @@ describe('spec/event/Event', () => {
134134
expect(decoded.params).toEqual([
135135
new DecodedLogParam(
136136
'a',
137-
new ParamType('int', null, 256),
137+
new ParamType('int', undefined, 256),
138138
new Token('int', new BigNumber(3))
139139
)
140140
]);

packages/abi/src/spec/event/event.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ class Event {
2828
this.inputParamTypes()
2929
);
3030

31+
if (!name) {
32+
throw new Error(
33+
`Event constructor: abi item does not have a name: ${JSON.stringify(
34+
abi
35+
)}`
36+
);
37+
}
38+
3139
this._id = id;
3240
this._name = name;
3341
this._signature = signature;
@@ -69,7 +77,7 @@ class Event {
6977
const topicParams = this.indexedParams(true);
7078
const dataParams = this.indexedParams(false);
7179

72-
let address;
80+
let address = '';
7381
let toSkip: number;
7482

7583
if (!this.anonymous) {

packages/abi/src/spec/function.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Func {
2323
constructor (abi: AbiItem) {
2424
this._abi = abi;
2525
this._constant = !!abi.constant;
26-
this._payable = abi.payable;
26+
this._payable = abi.payable || false;
2727
this._inputs = Param.toParams(abi.inputs || []);
2828
this._outputs = Param.toParams(abi.outputs || []);
2929

@@ -32,6 +32,14 @@ class Func {
3232
this.inputParamTypes()
3333
);
3434

35+
if (!name) {
36+
throw new Error(
37+
`Event constructor: abi item does not have a name: ${JSON.stringify(
38+
abi
39+
)}`
40+
);
41+
}
42+
3543
this._id = id;
3644
this._name = name;
3745
this._signature = signature;

0 commit comments

Comments
 (0)