Handle <module>::<Type> in sanitize (#2094)

* Handle <module>::<Type> in santize

* Sanitize all ::'s

* Additional cleanups
This commit is contained in:
Jaco Greeff
2020-03-25 14:01:24 +01:00
committed by GitHub
parent efbeecc8f6
commit 601288b3ec
3 changed files with 52 additions and 12 deletions
+19 -1
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // 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('sanitize', (): void => {
describe('alias', (): 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)');
});
});
}); });
+23 -9
View File
@@ -32,12 +32,10 @@ const mappings: Mapper[] = [
alias(['Lookup::Target'], 'LookupTarget'), alias(['Lookup::Target'], 'LookupTarget'),
// HACK duplication between contracts & primitives, however contracts prefixed with exec // HACK duplication between contracts & primitives, however contracts prefixed with exec
alias(['exec::StorageKey'], 'ContractStorageKey'), 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` // flattens tuples with one value, `(AccountId)` -> `AccountId`
flattenSingleTuple(), flattenSingleTuple(),
// converts ::Type to Type, <T as Trait<I>>::Proposal -> Proposal // converts ::Type to Type, <T as Trait<I>>::Proposal -> Proposal
removeColonPrefix() removeColons()
]; ];
// given a starting index, find the closing > // 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: 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 // remove all whitespaces
.replace(/\s/g, '') .replace(/\s/g, '')
// anything `T::<type>` to end up as `<type>` // anything `T::<type>` to end up as `<type>`
.replace(/(T|Self|wasm)::/g, '') .replace(/(T|Self)::/g, '')
// replace `<T as Trait>::` (whitespaces were removed above) // replace `<T as Trait>::` (whitespaces were removed above)
.replace(/<(T|Self)asTrait>::/g, '') .replace(/<(T|Self)asTrait>::/g, '')
// replace `<T as something::Trait>::` (whitespaces were removed above) // replace `<T as something::Trait>::` (whitespaces were removed above)
@@ -163,9 +179,7 @@ export function removeTraits (): Mapper {
// replace <Lookup as StaticLookup> // replace <Lookup as StaticLookup>
.replace(/<LookupasStaticLookup>/g, 'Lookup') .replace(/<LookupasStaticLookup>/g, 'Lookup')
// replace `<...>::Type` // replace `<...>::Type`
.replace(/::Type/g, '') .replace(/::Type/g, '');
// `sr_std::marker::`
.replace(/(sp_std|sr_std|rstd)::/g, '');
}; };
} }
+10 -2
View File
@@ -48,6 +48,7 @@ const TYPES_POLKADOT_VERSIONED: OverrideVersionedType[] = [
Address: 'GenericAddress', Address: 'GenericAddress',
BalanceLock: 'BalanceLockTo212', BalanceLock: 'BalanceLockTo212',
Keys: 'SessionKeys5', Keys: 'SessionKeys5',
LookupSource: 'Address',
StakingLedger: 'StakingLedgerTo223', StakingLedger: 'StakingLedgerTo223',
Votes: 'VotesTo230' Votes: 'VotesTo230'
} }
@@ -58,6 +59,7 @@ const TYPES_POLKADOT_VERSIONED: OverrideVersionedType[] = [
// Indices optional, not in transaction // Indices optional, not in transaction
Address: 'AccountId', Address: 'AccountId',
Keys: 'SessionKeys5', Keys: 'SessionKeys5',
LookupSource: 'AccountId',
ReferendumInfo: 'ReferendumInfoTo239' ReferendumInfo: 'ReferendumInfoTo239'
} }
}, },
@@ -65,7 +67,8 @@ const TYPES_POLKADOT_VERSIONED: OverrideVersionedType[] = [
minmax: [1005, undefined], minmax: [1005, undefined],
types: { types: {
Address: 'AccountId', Address: 'AccountId',
Keys: 'SessionKeys5' Keys: 'SessionKeys5',
LookupSource: 'AccountId'
} }
} }
]; ];
@@ -79,6 +82,7 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
BalanceLock: 'BalanceLockTo212', BalanceLock: 'BalanceLockTo212',
DispatchError: 'DispatchErrorTo198', DispatchError: 'DispatchErrorTo198',
Keys: 'SessionKeys5', Keys: 'SessionKeys5',
LookupSource: 'Address',
ReferendumInfo: 'ReferendumInfoTo239', ReferendumInfo: 'ReferendumInfoTo239',
SlashingSpans: 'SlashingSpansTo204', SlashingSpans: 'SlashingSpansTo204',
StakingLedger: 'StakingLedgerTo223', StakingLedger: 'StakingLedgerTo223',
@@ -91,6 +95,7 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
Address: 'GenericAddress', Address: 'GenericAddress',
BalanceLock: 'BalanceLockTo212', BalanceLock: 'BalanceLockTo212',
Keys: 'SessionKeys5', Keys: 'SessionKeys5',
LookupSource: 'Address',
ReferendumInfo: 'ReferendumInfoTo239', ReferendumInfo: 'ReferendumInfoTo239',
SlashingSpans: 'SlashingSpansTo204', SlashingSpans: 'SlashingSpansTo204',
StakingLedger: 'StakingLedgerTo223', StakingLedger: 'StakingLedgerTo223',
@@ -104,6 +109,7 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
Address: 'GenericAddress', Address: 'GenericAddress',
BalanceLock: 'BalanceLockTo212', BalanceLock: 'BalanceLockTo212',
Keys: 'SessionKeys5', Keys: 'SessionKeys5',
LookupSource: 'Address',
ReferendumInfo: 'ReferendumInfoTo239', ReferendumInfo: 'ReferendumInfoTo239',
StakingLedger: 'StakingLedgerTo223', StakingLedger: 'StakingLedgerTo223',
Votes: 'VotesTo230' Votes: 'VotesTo230'
@@ -116,6 +122,7 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
// Indices optional, not in transaction // Indices optional, not in transaction
Address: 'AccountId', Address: 'AccountId',
Keys: 'SessionKeys5', Keys: 'SessionKeys5',
LookupSource: 'AccountId',
ReferendumInfo: 'ReferendumInfoTo239' ReferendumInfo: 'ReferendumInfoTo239'
} }
}, },
@@ -123,7 +130,8 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
minmax: [1055, undefined], minmax: [1055, undefined],
types: { types: {
Address: 'AccountId', Address: 'AccountId',
Keys: 'SessionKeys5' Keys: 'SessionKeys5',
LookupSource: 'AccountId'
} }
} }
]; ];