From 274438bc1755da303ad16e57a2cb1e4eb66eef7a Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 18 Dec 2017 19:59:58 +0100 Subject: [PATCH] Prefer static checking --- .flowconfig | 5 +-- jest.config.js | 4 +- package.json | 2 +- packages/api-format/package.json | 4 +- packages/api-format/src/input.spec.js | 4 +- packages/api-format/src/output.spec.js | 6 +-- packages/api-format/src/util.js | 7 +--- packages/api-format/src/util.spec.js | 6 --- packages/api-provider/package.json | 2 +- packages/api-provider/src/http/index.js | 1 - packages/api-provider/src/http/index.spec.js | 16 ++------ packages/api-provider/src/ws/index.js | 1 - packages/api-provider/src/ws/index.spec.js | 16 ++------ packages/api/package.json | 2 +- packages/api/src/api.js | 4 +- packages/api/src/api.spec.js | 10 +---- yarn.lock | 39 ++++++++++++-------- 17 files changed, 49 insertions(+), 80 deletions(-) diff --git a/.flowconfig b/.flowconfig index 957f1f4c9112..f6c238a02b52 100644 --- a/.flowconfig +++ b/.flowconfig @@ -8,7 +8,4 @@ [options] unsafe.enable_getters_and_setters=true -module.name_mapper='^@polkadot/api-format\(.*\)$' -> '/packages/api-format/src\1' -module.name_mapper='^@polkadot/api-jsonrpc\(.*\)$' -> '/packages/api-jsonrpc/src\1' -module.name_mapper='^@polkadot/api-provider\(.*\)$' -> '/packages/api-provider/src\1' -module.name_mapper='^@polkadot/jsonrpc\(.*\)$' -> '/packages/api-jsonrpc/src\1' +module.name_mapper='^@polkadot/api-\(format\|jsonrpc\|provider\)\(.*\)$' -> '/packages/api-\1/src\2' diff --git a/jest.config.js b/jest.config.js index 78c2127f210b..e1e235c1c6d6 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,8 +2,6 @@ const config = require('@polkadot/dev/config/jest'); module.exports = Object.assign({}, config, { moduleNameMapper: { - '@polkadot/api-format(.*)$': '/packages/api-format/src/$1', - '@polkadot/api-jsonrpc(.*)$': '/packages/api-jsonrpc/src/$1', - '@polkadot/api-provider(.*)$': '/packages/api-provider/src/$1' + '@polkadot/api-(format|jsonrpc|provider)(.*)$': '/packages/api-$1/src/$2' } }); diff --git a/package.json b/package.json index fd265000c4dc..a55da7779fe1 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "test": "jest --coverage" }, "devDependencies": { - "@polkadot/dev": "^0.13.4", + "@polkadot/dev": "^0.13.5", "lerna": "^2.5.1" } } diff --git a/packages/api-format/package.json b/packages/api-format/package.json index a2f9d0d2db36..1f595192f28d 100644 --- a/packages/api-format/package.json +++ b/packages/api-format/package.json @@ -33,8 +33,8 @@ "@polkadot/api-jsonrpc": "^0.6.7" }, "dependencies": { - "@polkadot/primitives-json": "^0.3.1", - "@polkadot/util": "^0.9.1", + "@polkadot/primitives-json": "^0.4.1", + "@polkadot/util": "^0.10.1", "babel-runtime": "^6.26.0" } } diff --git a/packages/api-format/src/input.spec.js b/packages/api-format/src/input.spec.js index 73cb3c0bdf08..4a0b42c07bf6 100644 --- a/packages/api-format/src/input.spec.js +++ b/packages/api-format/src/input.spec.js @@ -13,8 +13,8 @@ describe('formatInputs', () => { ['0x1234', '0xabcd'] ) ).toEqual([ - '0x0000000000000000000000000000000000001234', - '0x000000000000000000000000000000000000000000000000000000000000abcd' + '0x1234', + '0xabcd' ]); }); }); diff --git a/packages/api-format/src/output.spec.js b/packages/api-format/src/output.spec.js index 80f5b99bfb1c..3288c7672e26 100644 --- a/packages/api-format/src/output.spec.js +++ b/packages/api-format/src/output.spec.js @@ -21,10 +21,10 @@ describe('formatOutput', () => { ) ).toEqual( JSON.stringify({ - parentHash: '0x0000000000000000000000000000000000000000000000000000000000001234', + parentHash: '0x1234', number: new BN(0x1234), - stateRoot: '0x0000000000000000000000000000000000000000000000000000000000005678', - transactionRoot: '0x000000000000000000000000000000000000000000000000000000000000abcd', + stateRoot: '0x5678', + transactionRoot: '0xabcd', digest: { parachainActivityBitfield: Buffer.from([0x12, 0x34]), logs: [ diff --git a/packages/api-format/src/util.js b/packages/api-format/src/util.js index fb867469c475..95997d089e7b 100644 --- a/packages/api-format/src/util.js +++ b/packages/api-format/src/util.js @@ -3,7 +3,6 @@ import type { FormatterFunction } from './types'; -const assert = require('@polkadot/util/assert'); const isUndefined = require('@polkadot/util/is/undefined'); const echo = require('./echo'); @@ -25,12 +24,10 @@ function formatSingleType (formatters: { [any]: FormatterFunction }, type: strin } } -function formatArrayType (formatters: { [any]: FormatterFunction }, type: string, value: any): any { - assert(Array.isArray(value), `Unable to format non-array '${value}' as '${type}'`); - +function formatArrayType (formatters: { [any]: FormatterFunction }, type: string, value: Array): any { type = type.replace(arrayTypeRegex, ''); - return (value: Array).map((value) => { + return value.map((value) => { return formatSingleType(formatters, type, value); }); } diff --git a/packages/api-format/src/util.spec.js b/packages/api-format/src/util.spec.js index e51a4dbe64a3..f762a36cf59c 100644 --- a/packages/api-format/src/util.spec.js +++ b/packages/api-format/src/util.spec.js @@ -53,12 +53,6 @@ describe('util', () => { }); describe('primitive[]', () => { - it('throws exception with array type, but non-array value', () => { - expect( - () => format(formatters, 'Address[]', 'not-an-array') - ).toThrow(/Unable to format non-array/); - }); - it('formats using the primitive type', () => { format(formatters, 'Address[]', ['0x123', '0x234']); diff --git a/packages/api-provider/package.json b/packages/api-provider/package.json index d7ea6b9f0b3e..3f59d1a8e8fd 100644 --- a/packages/api-provider/package.json +++ b/packages/api-provider/package.json @@ -34,7 +34,7 @@ "nock": "^9.1.0" }, "dependencies": { - "@polkadot/util": "^0.9.1", + "@polkadot/util": "^0.10.1", "babel-runtime": "^6.26.0", "isomorphic-fetch": "^2.2.1", "websocket": "^1.0.25" diff --git a/packages/api-provider/src/http/index.js b/packages/api-provider/src/http/index.js index aeab5e8cf345..abfb182e09a0 100644 --- a/packages/api-provider/src/http/index.js +++ b/packages/api-provider/src/http/index.js @@ -14,7 +14,6 @@ module.exports = class HttpProvider extends JsonRpcCoder implements ProviderInte constructor (endpoint: string) { super(); - assert(endpoint, 'Endpoint should be provided'); assert(/^http:\/\//.test(endpoint), `Endpoint should start with 'http://', received '${endpoint}'`); this._endpoint = endpoint; diff --git a/packages/api-provider/src/http/index.spec.js b/packages/api-provider/src/http/index.spec.js index 0d3594a74f53..65ce22d404c0 100644 --- a/packages/api-provider/src/http/index.spec.js +++ b/packages/api-provider/src/http/index.spec.js @@ -27,18 +27,10 @@ describe('Http', () => { } }); - describe('constructor', () => { - it('requires the endpoint to be set', () => { - expect( - () => new Http() - ).toThrow(/should be provided/); - }); - - it('requires an http:// prefixed endpoint', () => { - expect( - () => new Http('ws://') - ).toThrow(/with 'http/); - }); + it('requires an http:// prefixed endpoint', () => { + expect( + () => new Http('ws://') + ).toThrow(/with 'http/); }); describe('isConnected', () => { diff --git a/packages/api-provider/src/ws/index.js b/packages/api-provider/src/ws/index.js index 8c2dcf4fd72e..44a6ac1b87aa 100644 --- a/packages/api-provider/src/ws/index.js +++ b/packages/api-provider/src/ws/index.js @@ -27,7 +27,6 @@ module.exports = class WsProvider extends JsonRpcCoder implements ProviderInterf constructor (endpoint: string, autoConnect: boolean = true) { super(); - assert(endpoint, 'Endpoint should be provided'); assert(/^ws:\/\//.test(endpoint), `Endpoint should start with 'ws://', received '${endpoint}'`); this._endpoint = endpoint; diff --git a/packages/api-provider/src/ws/index.spec.js b/packages/api-provider/src/ws/index.spec.js index 0a967139d2c6..381fa8551b7f 100644 --- a/packages/api-provider/src/ws/index.spec.js +++ b/packages/api-provider/src/ws/index.spec.js @@ -37,18 +37,10 @@ describe('Ws', () => { } }); - describe('constructor', () => { - it('requires the endpoint to be set', () => { - expect( - () => new Ws() - ).toThrow(/should be provided/); - }); - - it('requires an ws:// prefixed endpoint', () => { - expect( - () => new Ws('http://') - ).toThrow(/with 'ws/); - }); + it('requires an ws:// prefixed endpoint', () => { + expect( + () => new Ws('http://') + ).toThrow(/with 'ws/); }); describe('websocket', () => { diff --git a/packages/api/package.json b/packages/api/package.json index 2cac19535a90..9f0a471e9db4 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -33,7 +33,7 @@ "@polkadot/api-format": "^0.6.7", "@polkadot/api-jsonrpc": "^0.6.7", "@polkadot/api-provider": "^0.6.7", - "@polkadot/util": "^0.9.1", + "@polkadot/util": "^0.10.1", "babel-runtime": "^6.26.0" } } diff --git a/packages/api/src/api.js b/packages/api/src/api.js index ba989aeb4e7b..0e53e9947599 100644 --- a/packages/api/src/api.js +++ b/packages/api/src/api.js @@ -18,9 +18,7 @@ module.exports = class Api implements ApiInterface { _stateInterface: any = null; constructor (provider: ProviderInterface) { - assert(provider, 'Instantiate the Api with `new Api(new Provider(...))`'); - - assert(isFunction(provider.send), 'Provider does not expose send method'); + assert(provider && isFunction(provider.send), 'Expected Provider'); this._provider = provider; diff --git a/packages/api/src/api.spec.js b/packages/api/src/api.spec.js index f6fc9e682dd9..2b69e202660c 100644 --- a/packages/api/src/api.spec.js +++ b/packages/api/src/api.spec.js @@ -24,16 +24,10 @@ describe('Api', () => { }); describe('constructor', () => { - it('requires a provider', () => { - expect( - () => new Api() - ).toThrow(/Instantiate the Api/); - }); - it('requires a provider with a send method', () => { expect( () => new Api({}) - ).toThrow(/does not expose send/); + ).toThrow(/Expected Provider/); }); it('sets up the chain interface', () => { @@ -100,7 +94,7 @@ describe('Api', () => { .blah('0x123') .then(() => { expect(provider.send).toHaveBeenCalledWith('test_blah', [ - '0x0000000000000000000000000000000000000123' + '0x0123' ]); }); }); diff --git a/yarn.lock b/yarn.lock index 1b224c2eb0c2..1d3a3cc6f213 100644 --- a/yarn.lock +++ b/yarn.lock @@ -54,9 +54,9 @@ lodash "^4.2.0" to-fast-properties "^2.0.0" -"@polkadot/dev@^0.13.4": - version "0.13.4" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.13.4.tgz#beaa45da1655f8335b4a8294db3803f95cd3cc4e" +"@polkadot/dev@^0.13.5": + version "0.13.5" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.13.5.tgz#01dc2e720d5f9a4133a944fb42ae44623cd02880" dependencies: babel-cli "^6.26.0" babel-core "^6.26.0" @@ -91,24 +91,33 @@ stylelint "^8.2.0" stylelint-config-standard "^18.0.0" -"@polkadot/primitives-json@^0.3.1": - version "0.3.1" - resolved "https://registry.yarnpkg.com/@polkadot/primitives-json/-/primitives-json-0.3.1.tgz#7c18589b944d792019ed08155363a29504604d23" +"@polkadot/primitives-json@^0.4.1": + version "0.4.2" + resolved "https://registry.yarnpkg.com/@polkadot/primitives-json/-/primitives-json-0.4.2.tgz#5d1ca426a76bd1ff459d759f00fbc4771431fa00" dependencies: - "@polkadot/primitives" "^0.3.1" - "@polkadot/util" "^0.9.1" + "@polkadot/primitives" "^0.4.2" + "@polkadot/util" "^0.9.2" babel-runtime "^6.26.0" -"@polkadot/primitives@^0.3.1": - version "0.3.1" - resolved "https://registry.yarnpkg.com/@polkadot/primitives/-/primitives-0.3.1.tgz#420220a8e9c3118f613e47218b038ca7c3724922" +"@polkadot/primitives@^0.4.2": + version "0.4.2" + resolved "https://registry.yarnpkg.com/@polkadot/primitives/-/primitives-0.4.2.tgz#50a015a3b4ff682f7673ebe9aaaab39ecb699ee2" dependencies: - "@polkadot/util" "^0.9.1" + "@polkadot/util" "^0.9.2" babel-runtime "^6.26.0" -"@polkadot/util@^0.9.1": - version "0.9.1" - resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.9.1.tgz#222a31f0325468044736792bd5652e483b8336db" +"@polkadot/util@^0.10.1": + version "0.10.1" + resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.10.1.tgz#3e62e413e59f63eae5ddb36524b2bec8b3ac4265" + dependencies: + babel-runtime "^6.26.0" + bn.js "^4.11.8" + ip-regex "^2.1.0" + keccak "^1.3.0" + +"@polkadot/util@^0.9.2": + version "0.9.3" + resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.9.3.tgz#bd8d035225024eb0a95d56dc483912db9ed73e9e" dependencies: babel-runtime "^6.26.0" bn.js "^4.11.8"