Additional Codeclimate cleanups (#452)

* Additional Codeclimate cleanups

* Keep original Vector.with

* Cleanup subtype extraction

* Cleanup fromMeta

* Cleanup createType tests
This commit is contained in:
Jaco Greeff
2018-12-09 10:54:43 +01:00
committed by GitHub
parent 871c3db84a
commit 3e7e1c653b
5 changed files with 34 additions and 59 deletions
+2
View File
@@ -1,4 +1,6 @@
exclude_patterns:
- "**/*.spec.js"
- "**/*.spec.ts"
- "**/*.test.js"
- "**/*.test.ts"
- "docs/**/*.js"
+2 -11
View File
@@ -24,9 +24,7 @@ export default function fromMetadata (metadata: Metadata): Extrinsics {
const findIndex = (prefix: string): number => {
indexCount++;
const mod = metadata.calls.find((item) =>
item.prefix.toString() === prefix
);
const mod = metadata.calls.find((item) => item.prefix.toString() === prefix);
if (!mod) {
console.error(`Unable to find module index for '${prefix}'`);
@@ -38,13 +36,6 @@ export default function fromMetadata (metadata: Metadata): Extrinsics {
return mod.index.toNumber();
};
// Dont' clobber the input, create new
const result = Object.keys(extrinsics).reduce((result, key) => {
result[key] = extrinsics[key];
return result;
}, {} as Extrinsics);
return metadata.modules.reduce((result, meta: RuntimeModuleMetadata) => {
if (!meta.module.call || !meta.module.call.functions.length) {
return result;
@@ -63,5 +54,5 @@ export default function fromMetadata (metadata: Metadata): Extrinsics {
}, {} as ModuleExtrinsics);
return result;
}, result);
}, { ...extrinsics });
}
+1 -7
View File
@@ -18,12 +18,6 @@ import storage from './index';
* @param metadata - The metadata to extend the storage object against.
*/
export default function fromMetadata (metadata: Metadata): Storage {
const result = Object.keys(storage).reduce((result, key) => {
result[key] = storage[key];
return result;
}, {} as Storage);
return metadata.modules.reduce((result, moduleMetadata) => {
if (moduleMetadata.storage.isNone) {
return result;
@@ -39,5 +33,5 @@ export default function fromMetadata (metadata: Metadata): Storage {
}, {} as ModuleStorage);
return result;
}, result);
}, { ...storage });
}
+2 -2
View File
@@ -52,13 +52,13 @@ describe('getTypeValue', () => {
it('does not allow invalid tuples, end )', () => {
expect(
() => getTypeDef('(u64, u32')
).toThrow(/tuple wrapped/);
).toThrow(/Expected '\(' closing with '\)'/);
});
it('does not allow invalid vectors, end >', () => {
expect(
() => getTypeDef('Vec<u64')
).toThrow(/Vec wrapped/);
).toThrow(/Expected 'Vec<' closing with '>'/);
});
it('returns a type structure', () => {
+27 -39
View File
@@ -42,33 +42,25 @@ export function typeSplit (type: string): Array<string> {
}
break;
case '(':
// inc tuple depth
tDepth++;
break;
// inc tuple depth, start found
case '(': tDepth++; break;
case ')':
// dec tuple depth
tDepth--;
break;
// dec tuple depth, end found
case ')': tDepth--; break;
case '<':
// inc compact/vec depth
vDepth++;
break;
// inc compact/vec depth, start found
case '<': vDepth++; break;
case '>':
// dec compact/vec depth
vDepth--;
break;
// dec compact/vec depth, end found
case '>': vDepth--; break;
default:
break;
// normal character
default: break;
}
}
assert(tDepth === 0, `Invalid Tuple in ${type}`);
assert(vDepth === 0, `Invalid Compact/Vector in ${type}`);
assert(!tDepth, `Invalid Tuple in ${type}`);
assert(!vDepth, `Invalid Compact/Vector in ${type}`);
// the final leg of the journey
result.push(type.substr(start, type.length - start).trim());
@@ -82,31 +74,27 @@ export function getTypeDef (_type: Text | string): TypeDef {
info: TypeDefInfo.Plain,
type
};
let subType = '';
if (type[0] === '(') {
assert(type[type.length - 1] === ')', `Expected tuple wrapped with ()`);
const startingWith = (type: string, start: string, end: string): boolean => {
if (type.substr(0, start.length) !== start) {
return false;
}
// strip wrapping ()'s
const innerTypes = typeSplit(type.substr(1, type.length - 1 - 1));
assert(type[type.length - 1] === end, `Expected '${start}' closing with '${end}'`);
subType = type.substr(start.length, type.length - start.length - 1);
return true;
};
if (startingWith(type, '(', ')')) {
value.info = TypeDefInfo.Tuple;
value.sub = innerTypes.map((inner) =>
getTypeDef(inner)
);
} else if (type.substr(0, 8) === 'Compact<') {
assert(type[type.length - 1] === '>', `Expected Compact wrapped with <>`);
// strip wrapping Compact<>
const subType = type.substr(8, type.length - 8 - 1);
value.sub = typeSplit(subType).map((inner) => getTypeDef(inner));
} else if (startingWith(type, 'Compact<', '>')) {
value.info = TypeDefInfo.Compact;
value.sub = getTypeDef(subType);
} else if (type.substr(0, 4) === 'Vec<') {
assert(type[type.length - 1] === '>', `Expected Vec wrapped with <>`);
// strip wrapping Vec<>
const subType = type.substr(4, type.length - 4 - 1);
} else if (startingWith(type, 'Vec<', '>')) {
value.info = TypeDefInfo.Vector;
value.sub = getTypeDef(subType);
}