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:
@@ -10,6 +10,7 @@ import { EraIndex } from '@polkadot/types/interfaces/staking';
|
||||
|
||||
declare module '@polkadot/metadata/Decorated/consts/types' {
|
||||
export interface Constants {
|
||||
[index: string]: ModuleConstants;
|
||||
babe: {
|
||||
[index: string]: Codec;
|
||||
/**
|
||||
|
||||
@@ -27,6 +27,7 @@ import { Multiplier } from '@polkadot/types/interfaces/txpayment';
|
||||
import { Multisig } from '@polkadot/types/interfaces/utility';
|
||||
import { VestingInfo } from '@polkadot/types/interfaces/vesting';
|
||||
import { Observable } from 'rxjs';
|
||||
import { ApiTypes } from '@polkadot/api/types';
|
||||
|
||||
declare module '@polkadot/api/types/storage' {
|
||||
export interface AugmentedQueries<ApiType> {
|
||||
@@ -794,4 +795,8 @@ declare module '@polkadot/api/types/storage' {
|
||||
vesting: AugmentedQuery<ApiType, (arg: AccountId | string | Uint8Array) => Observable<Option<VestingInfo>>> & QueryableStorageEntry<ApiType>;
|
||||
};
|
||||
}
|
||||
|
||||
export interface QueryableStorage<ApiType extends ApiTypes> extends AugmentedQueries<ApiType> {
|
||||
[index: string]: QueryableModuleStorage<ApiType>;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import { EraIndex, RewardDestination, ValidatorPrefs } from '@polkadot/types/int
|
||||
import { Key } from '@polkadot/types/interfaces/system';
|
||||
import { Timepoint } from '@polkadot/types/interfaces/utility';
|
||||
import { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
|
||||
import { ApiTypes } from '@polkadot/api/types';
|
||||
|
||||
declare module '@polkadot/api/types/submittable' {
|
||||
export interface AugmentedSubmittables<ApiType> {
|
||||
@@ -1622,4 +1623,9 @@ declare module '@polkadot/api/types/submittable' {
|
||||
vestOther: AugmentedSubmittable<(target: LookupSource | Address | AccountId | AccountIndex | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
|
||||
};
|
||||
}
|
||||
|
||||
export interface SubmittableExtrinsics<ApiType extends ApiTypes> extends AugmentedSubmittables<ApiType> {
|
||||
(extrinsic: Uint8Array | string): SubmittableExtrinsic<ApiType>;
|
||||
[index: string]: SubmittableModuleExtrinsics<ApiType>;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,3 @@ export type QueryableStorageMulti<ApiType extends ApiTypes> =
|
||||
ApiType extends 'rxjs'
|
||||
? QueryableStorageMultiBase<ApiType>
|
||||
: QueryableStorageMultiPromise<ApiType>;
|
||||
|
||||
export interface QueryableStorage<ApiType extends ApiTypes> extends AugmentedQueries<ApiType> {
|
||||
[index: string]: QueryableModuleStorage<ApiType>;
|
||||
}
|
||||
|
||||
@@ -19,8 +19,3 @@ export interface SubmittableExtrinsicFunction<ApiType extends ApiTypes> extends
|
||||
export interface SubmittableModuleExtrinsics<ApiType extends ApiTypes> {
|
||||
[index: string]: SubmittableExtrinsicFunction<ApiType>;
|
||||
}
|
||||
|
||||
export interface SubmittableExtrinsics<ApiType extends ApiTypes> extends AugmentedSubmittables<ApiType> {
|
||||
(extrinsic: Uint8Array | string): SubmittableExtrinsic<ApiType>;
|
||||
[index: string]: SubmittableModuleExtrinsics<ApiType>;
|
||||
}
|
||||
|
||||
@@ -10,9 +10,8 @@ export type AugmentedConst<T extends Codec> = T & ConstantCodec;
|
||||
|
||||
export type ModuleConstants = Record<string, Codec>;
|
||||
|
||||
export interface Constants {
|
||||
[index: string]: ModuleConstants;
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface Constants {}
|
||||
|
||||
export interface ConstantCodec extends Codec {
|
||||
meta: ModuleConstantMetadataLatest;
|
||||
|
||||
@@ -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