From 601288b3ec00f8c63b5590246dbc2843dc0b437f Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 25 Mar 2020 14:01:24 +0100 Subject: [PATCH] Handle :: in sanitize (#2094) * Handle :: in santize * Sanitize all ::'s * Additional cleanups --- packages/types/src/create/sanitize.spec.ts | 20 +++++++++++++- packages/types/src/create/sanitize.ts | 32 ++++++++++++++++------ packages/types/src/known/overrides.ts | 12 ++++++-- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/packages/types/src/create/sanitize.spec.ts b/packages/types/src/create/sanitize.spec.ts index 87a5865bf1..8e901c7985 100644 --- a/packages/types/src/create/sanitize.spec.ts +++ b/packages/types/src/create/sanitize.spec.ts @@ -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 { alias } from './sanitize'; +import { alias, removeColons } from './sanitize'; describe('sanitize', (): void => { describe('alias', (): void => { @@ -27,4 +27,22 @@ describe('sanitize', (): void => { ); }); }); + + describe('removeColons', (): void => { + it('removes preceding ::Text -> Text', (): void => { + expect(removeColons()('::Text')).toEqual('Text'); + }); + + it('removes middle voting::TallyType -> TallyType', (): void => { + expect(removeColons()('voting::TallyType')).toEqual('TallyType'); + }); + + it('removes on embedded values (one)', (): void => { + expect(removeColons()('(T::AccountId, SpanIndex)')).toEqual('(AccountId, SpanIndex)'); + }); + + it('removes on embedded values (all)', (): void => { + expect(removeColons()('(T::AccountId, slashing::SpanIndex)')).toEqual('(AccountId, SpanIndex)'); + }); + }); }); diff --git a/packages/types/src/create/sanitize.ts b/packages/types/src/create/sanitize.ts index 536d76ebdd..a04eadbe8b 100644 --- a/packages/types/src/create/sanitize.ts +++ b/packages/types/src/create/sanitize.ts @@ -32,12 +32,10 @@ const mappings: Mapper[] = [ alias(['Lookup::Target'], 'LookupTarget'), // HACK duplication between contracts & primitives, however contracts prefixed with exec alias(['exec::StorageKey'], 'ContractStorageKey'), - // alias for internal module mappings - alias(['exec', 'grandpa', 'marker', 'session', 'slashing'].map((s) => `${s}::`), ''), // flattens tuples with one value, `(AccountId)` -> `AccountId` flattenSingleTuple(), // converts ::Type to Type, >::Proposal -> Proposal - removeColonPrefix() + removeColons() ]; // given a starting index, find the closing > @@ -98,9 +96,27 @@ export function flattenSingleTuple (): Mapper { }; } -export function removeColonPrefix (): Mapper { +export function removeColons (): Mapper { return (value: string): string => { - return value.replace(/^::/, ''); + let index = 0; + + while (index !== -1) { + index = value.indexOf('::'); + + if (index === 0) { + value = value.substr(2); + } else if (index !== -1) { + let start = index; + + while (start !== -1 && !BOX_PRECEDING.includes(value[start])) { + start--; + } + + value = `${value.substr(0, start + 1)}${value.substr(index + 2)}`; + } + } + + return value; }; } @@ -155,7 +171,7 @@ export function removeTraits (): Mapper { // remove all whitespaces .replace(/\s/g, '') // anything `T::` to end up as `` - .replace(/(T|Self|wasm)::/g, '') + .replace(/(T|Self)::/g, '') // replace `::` (whitespaces were removed above) .replace(/<(T|Self)asTrait>::/g, '') // replace `::` (whitespaces were removed above) @@ -163,9 +179,7 @@ export function removeTraits (): Mapper { // replace .replace(//g, 'Lookup') // replace `<...>::Type` - .replace(/::Type/g, '') - // `sr_std::marker::` - .replace(/(sp_std|sr_std|rstd)::/g, ''); + .replace(/::Type/g, ''); }; } diff --git a/packages/types/src/known/overrides.ts b/packages/types/src/known/overrides.ts index d15c3d4519..9cf99ecfb2 100644 --- a/packages/types/src/known/overrides.ts +++ b/packages/types/src/known/overrides.ts @@ -48,6 +48,7 @@ const TYPES_POLKADOT_VERSIONED: OverrideVersionedType[] = [ Address: 'GenericAddress', BalanceLock: 'BalanceLockTo212', Keys: 'SessionKeys5', + LookupSource: 'Address', StakingLedger: 'StakingLedgerTo223', Votes: 'VotesTo230' } @@ -58,6 +59,7 @@ const TYPES_POLKADOT_VERSIONED: OverrideVersionedType[] = [ // Indices optional, not in transaction Address: 'AccountId', Keys: 'SessionKeys5', + LookupSource: 'AccountId', ReferendumInfo: 'ReferendumInfoTo239' } }, @@ -65,7 +67,8 @@ const TYPES_POLKADOT_VERSIONED: OverrideVersionedType[] = [ minmax: [1005, undefined], types: { Address: 'AccountId', - Keys: 'SessionKeys5' + Keys: 'SessionKeys5', + LookupSource: 'AccountId' } } ]; @@ -79,6 +82,7 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [ BalanceLock: 'BalanceLockTo212', DispatchError: 'DispatchErrorTo198', Keys: 'SessionKeys5', + LookupSource: 'Address', ReferendumInfo: 'ReferendumInfoTo239', SlashingSpans: 'SlashingSpansTo204', StakingLedger: 'StakingLedgerTo223', @@ -91,6 +95,7 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [ Address: 'GenericAddress', BalanceLock: 'BalanceLockTo212', Keys: 'SessionKeys5', + LookupSource: 'Address', ReferendumInfo: 'ReferendumInfoTo239', SlashingSpans: 'SlashingSpansTo204', StakingLedger: 'StakingLedgerTo223', @@ -104,6 +109,7 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [ Address: 'GenericAddress', BalanceLock: 'BalanceLockTo212', Keys: 'SessionKeys5', + LookupSource: 'Address', ReferendumInfo: 'ReferendumInfoTo239', StakingLedger: 'StakingLedgerTo223', Votes: 'VotesTo230' @@ -116,6 +122,7 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [ // Indices optional, not in transaction Address: 'AccountId', Keys: 'SessionKeys5', + LookupSource: 'AccountId', ReferendumInfo: 'ReferendumInfoTo239' } }, @@ -123,7 +130,8 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [ minmax: [1055, undefined], types: { Address: 'AccountId', - Keys: 'SessionKeys5' + Keys: 'SessionKeys5', + LookupSource: 'AccountId' } } ];