Make Strict Mode Strict Again (#1938)
* fix: don't format "unorthodox" query input types
Also add support for tuples in `getSimilarTypes`
* fix: refactor type ignore logic into `formatType`
Had to use a regexp solution because types like `([AccountId | string | Uint8Array, Data | { none: any } | { raw: any } | { blakeTwo256: any } | { sha256: any } | { keccak256: any } | { shaThree256: any } | string | Uint8Array])[]` were not being ignored with the current logic, so they were passed to `getTypeDef` and causing delimiter errors.
* refactor: remove superfluous "FirstKeyType" param
* fix: ignore tuples with structs when formatting
* feat: eliminate all index signatures with --strict
* docs: fix doc generation
This commit is contained in:
@@ -57,6 +57,7 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: Record<strin
|
||||
|
||||
return header
|
||||
.concat(interfaceStart)
|
||||
.concat(isStrict ? '' : indent(4)('[index: string]: ModuleConstants;\n'))
|
||||
.concat(body.join('\n'))
|
||||
.concat(interfaceEnd)
|
||||
.concat(FOOTER);
|
||||
|
||||
@@ -86,7 +86,7 @@ function generateModule (allDefs: object, registry: Registry, { name, storage }:
|
||||
}
|
||||
|
||||
return createDocComments(6, storageEntry.documentation) +
|
||||
indent(6)(`${stringLowerFirst(storageEntry.name.toString())}: ${entryType}<ApiType, (${args}) => Observable<${returnType}>> & QueryableStorageEntry<ApiType>;`);
|
||||
indent(6)(`${stringLowerFirst(storageEntry.name.toString())}: ${entryType}<ApiType, (${args}) => Observable<${returnType}>>${isStrict ? '' : ' & QueryableStorageEntry<ApiType>'};`);
|
||||
}))
|
||||
.concat([indent(4)('};')]);
|
||||
}
|
||||
@@ -110,18 +110,28 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr
|
||||
{
|
||||
file: 'rxjs',
|
||||
types: ['Observable']
|
||||
},
|
||||
{
|
||||
file: '@polkadot/api/types',
|
||||
types: ['ApiTypes']
|
||||
}
|
||||
]);
|
||||
const interfaceStart = [
|
||||
"declare module '@polkadot/api/types/storage' {",
|
||||
indent(2)('export interface AugmentedQueries<ApiType> {\n')
|
||||
].join('\n');
|
||||
const interfaceEnd = `\n${indent(2)('}')}\n}`;
|
||||
const interfaceEnd = `\n${indent(2)('}')}\n\n`;
|
||||
const queryableStorageInterface = indent(2)('export interface QueryableStorage<ApiType extends ApiTypes> extends AugmentedQueries<ApiType> {')
|
||||
.concat('\n')
|
||||
.concat(isStrict ? '' : indent(4)('[index: string]: QueryableModuleStorage<ApiType>;\n'))
|
||||
.concat(indent(2)('}\n'));
|
||||
|
||||
return header
|
||||
.concat(interfaceStart)
|
||||
.concat(body.join('\n'))
|
||||
.concat(interfaceEnd)
|
||||
.concat(queryableStorageInterface)
|
||||
.concat('}')
|
||||
.concat(FOOTER);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -78,18 +78,29 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr
|
||||
{
|
||||
file: '@polkadot/api/submittable/types',
|
||||
types: ['SubmittableExtrinsic']
|
||||
},
|
||||
{
|
||||
file: '@polkadot/api/types',
|
||||
types: ['ApiTypes']
|
||||
}
|
||||
]);
|
||||
const interfaceStart = [
|
||||
"declare module '@polkadot/api/types/submittable' {",
|
||||
indent(2)('export interface AugmentedSubmittables<ApiType> {\n')
|
||||
].join('\n');
|
||||
const interfaceEnd = `\n${indent(2)('}')}\n}`;
|
||||
const interfaceEnd = `\n${indent(2)('}')}\n\n`;
|
||||
const submittableExtrinsicsInterface = indent(2)('export interface SubmittableExtrinsics<ApiType extends ApiTypes> extends AugmentedSubmittables<ApiType> {')
|
||||
.concat('\n')
|
||||
.concat(indent(4)('(extrinsic: Uint8Array | string): SubmittableExtrinsic<ApiType>;\n'))
|
||||
.concat(isStrict ? '' : indent(4)('[index: string]: SubmittableModuleExtrinsics<ApiType>;\n'))
|
||||
.concat(indent(2)('}\n'));
|
||||
|
||||
return header
|
||||
.concat(interfaceStart)
|
||||
.concat(body.join('\n'))
|
||||
.concat(interfaceEnd)
|
||||
.concat(submittableExtrinsicsInterface)
|
||||
.concat('}')
|
||||
.concat(FOOTER);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ export function formatType (definitions: object, type: string | TypeDef, imports
|
||||
if (typeof type === 'string') {
|
||||
// If type is "unorthodox" (i.e. `{ something: any }` for an Enum input or `[a | b | c, d | e | f]` for a Tuple's similar types),
|
||||
// we return it as-is
|
||||
if (/(^{.+:.+})|^\([^,]+\)|^\(.+\)\[\]|^\[([^;]+|)?[^;]+\]/.exec(type)) {
|
||||
if (/(^{.+:.+})|^\([^,]+\)|^\(.+\)\[\]|^\[.+\]/.exec(type) && !/\[\w+;\w+\]/.exec(type)) {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user