diff --git a/CHANGELOG.md b/CHANGELOG.md index e87842660c..5548b633b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 1.5.0-beta.x - Add proper support for type generation with an Enum containing an Tuple (Thanks to https://github.com/monitz87) +- Type generation will now handle the same sub-module name across packages, i.e. `@polkadot/types/interfaces/runtime` & `@mine/interfaces/runtime`) - Add `.range([from, to]: [Hash, Hash?], ...args: any[]): [Hash, Codec][]` on all storage entries - Allow `BTreeMap` to be initialized with a `Record` object (in addition to `Map`) - Allow for `HashMap` definitions diff --git a/package.json b/package.json index 8f8031a142..92ca19604f 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "@babel/core": "^7.8.4", "@babel/register": "^7.8.3", "@babel/runtime": "^7.8.4", - "@polkadot/dev": "^0.50.15", + "@polkadot/dev": "^0.50.16", "@polkadot/ts": "^0.3.6", "@polkadot/typegen": "workspace:packages/typegen", "copyfiles": "^2.2.0" diff --git a/packages/typegen/src/generate/consts.ts b/packages/typegen/src/generate/consts.ts index 458684a0f6..1fb0db7b4b 100644 --- a/packages/typegen/src/generate/consts.ts +++ b/packages/typegen/src/generate/consts.ts @@ -39,16 +39,16 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: Record { const allTypes: Record }>> = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes }; const imports = createImports(allTypes); - const allDefs = Object.entries(allTypes).reduce((defs, [, obj]) => { - return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [key]: value }), defs); + const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => { + return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs); }, {}); const body = meta.asLatest.modules.reduce((acc, mod): string[] => { return acc.concat(generateModule(allDefs, mod, imports, isStrict)); }, [] as string[]); const header = createImportCode(HEADER('chain'), imports, [ - ...Object.keys(imports.localTypes).sort().map((moduleName): { file: string; types: string[] } => ({ - file: `${imports.moduleToPackage[moduleName]}/${moduleName}`, - types: Object.keys(imports.localTypes[moduleName]) + ...Object.keys(imports.localTypes).sort().map((packagePath): { file: string; types: string[] } => ({ + file: packagePath, + types: Object.keys(imports.localTypes[packagePath]) })) ]); const interfaceStart = [ diff --git a/packages/typegen/src/generate/interfaceRegistry.ts b/packages/typegen/src/generate/interfaceRegistry.ts index 468225a2dc..6e0fc1259c 100644 --- a/packages/typegen/src/generate/interfaceRegistry.ts +++ b/packages/typegen/src/generate/interfaceRegistry.ts @@ -45,9 +45,9 @@ export function generateInterfaceTypes (importDefinitions: { [importPath: string }, ''); const header = createImportCode(HEADER('defs'), imports, [ - ...Object.keys(imports.localTypes).sort().map((moduleName): { file: string; types: string[] } => ({ - file: `${imports.moduleToPackage[moduleName]}/${moduleName}`, - types: Object.keys(imports.localTypes[moduleName]) + ...Object.keys(imports.localTypes).sort().map((packagePath): { file: string; types: string[] } => ({ + file: packagePath, + types: Object.keys(imports.localTypes[packagePath]) })) ]); diff --git a/packages/typegen/src/generate/query.ts b/packages/typegen/src/generate/query.ts index 333addf21f..c75d81dea1 100644 --- a/packages/typegen/src/generate/query.ts +++ b/packages/typegen/src/generate/query.ts @@ -96,16 +96,16 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr writeFile(dest, (): string => { const allTypes: Record }>> = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes }; const imports = createImports(allTypes); - const allDefs = Object.entries(allTypes).reduce((defs, [, obj]) => { - return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [key]: value }), defs); + const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => { + return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs); }, {}); const body = meta.asLatest.modules.reduce((acc: string[], mod): string[] => { return acc.concat(generateModule(allDefs, registry, mod, imports, isStrict)); }, []); const header = createImportCode(HEADER('chain'), imports, [ - ...Object.keys(imports.localTypes).sort().map((moduleName): { file: string; types: string[] } => ({ - file: `${imports.moduleToPackage[moduleName]}/${moduleName}`, - types: Object.keys(imports.localTypes[moduleName]) + ...Object.keys(imports.localTypes).sort().map((packagePath): { file: string; types: string[] } => ({ + file: packagePath, + types: Object.keys(imports.localTypes[packagePath]) })), { file: 'rxjs', diff --git a/packages/typegen/src/generate/rpc.ts b/packages/typegen/src/generate/rpc.ts index b3b671acd2..936f19e8b3 100644 --- a/packages/typegen/src/generate/rpc.ts +++ b/packages/typegen/src/generate/rpc.ts @@ -12,17 +12,21 @@ import { FOOTER, HEADER, createDocComments, createImportCode, createImports, get export default function generateRpcTypes (dest = 'packages/api/src/augment/rpc.ts'): void { writeFile(dest, (): string => { const registry = new TypeRegistry(); - const imports = createImports({ '@polkadot/types/interfaces': definitions }); + const allTypes = { '@polkadot/types/interfaces': definitions }; + const imports = createImports(allTypes); + const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => { + return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs); + }, {}); const body = Object.keys(interfaces).sort().reduce((allSections, section): string[] => { const allMethods = Object.keys(interfaces[section].methods).sort().map((key): string => { const method = interfaces[section].methods[key]; - setImports(definitions, imports, [method.type]); + setImports(allDefs, imports, [method.type]); // FIXME These 2 are too hard to type, I give up if (section === 'state') { if (method.method === 'getStorage') { - setImports(definitions, imports, ['Codec']); + setImports(allDefs, imports, ['Codec']); return ' getStorage(key: any, block?: Hash | Uint8Array | string): Observable;'; } else if (method.method === 'subscribeStorage') { return ' subscribeStorage(keys: any[]): Observable;'; @@ -31,7 +35,8 @@ export default function generateRpcTypes (dest = 'packages/api/src/augment/rpc.t const args = method.params.map((param): string => { const similarTypes = getSimilarTypes(definitions, registry, param.type, imports); - setImports(definitions, imports, [param.type, ...similarTypes]); + + setImports(allDefs, imports, [param.type, ...similarTypes]); return `${param.name}${param.isOptional ? '?' : ''}: ${similarTypes.join(' | ')}`; }); @@ -49,9 +54,9 @@ export default function generateRpcTypes (dest = 'packages/api/src/augment/rpc.t }, []).join('\n'); const header = createImportCode(HEADER('chain'), imports, [ - ...Object.keys(imports.localTypes).sort().map((moduleName): { file: string; types: string[] } => ({ - file: `@polkadot/types/interfaces/${moduleName}`, - types: Object.keys(imports.localTypes[moduleName]) + ...Object.keys(imports.localTypes).sort().map((packagePath): { file: string; types: string[] } => ({ + file: packagePath, + types: Object.keys(imports.localTypes[packagePath]) })), { file: 'rxjs', diff --git a/packages/typegen/src/generate/tsDef.ts b/packages/typegen/src/generate/tsDef.ts index 4faea01526..5c8b2fd981 100644 --- a/packages/typegen/src/generate/tsDef.ts +++ b/packages/typegen/src/generate/tsDef.ts @@ -188,9 +188,9 @@ function generateTsDefFor (importDefinitions: { [importPath: string]: object }, const sortedDefs = interfaces.sort((a, b): number => a[0].localeCompare(b[0])).map(([, definition]): string => definition).join('\n\n'); const header = createImportCode(HEADER('defs'), imports, [ - ...Object.keys(imports.localTypes).sort().map((moduleName): { file: string; types: string[] } => ({ - file: `${imports.moduleToPackage[moduleName]}/${moduleName}`, - types: Object.keys(imports.localTypes[moduleName]) + ...Object.keys(imports.localTypes).sort().map((packagePath): { file: string; types: string[] } => ({ + file: packagePath, + types: Object.keys(imports.localTypes[packagePath]) })) ]); diff --git a/packages/typegen/src/generate/tx.ts b/packages/typegen/src/generate/tx.ts index a03a895a23..e9f66f3ca2 100644 --- a/packages/typegen/src/generate/tx.ts +++ b/packages/typegen/src/generate/tx.ts @@ -63,16 +63,16 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr writeFile(dest, (): string => { const allTypes: Record }>> = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes }; const imports = createImports(allTypes); - const allDefs = Object.entries(allTypes).reduce((defs, [, obj]) => { - return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [key]: value }), defs); + const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => { + return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs); }, {}); const body = meta.asLatest.modules.reduce((acc, mod): string[] => { return acc.concat(generateModule(registry, allDefs, mod, imports, isStrict)); }, [] as string[]); const header = createImportCode(HEADER('chain'), imports, [ - ...Object.keys(imports.localTypes).sort().map((moduleName): { file: string; types: string[] } => ({ - file: `${imports.moduleToPackage[moduleName]}/${moduleName}`, - types: Object.keys(imports.localTypes[moduleName]) + ...Object.keys(imports.localTypes).sort().map((packagePath): { file: string; types: string[] } => ({ + file: packagePath, + types: Object.keys(imports.localTypes[packagePath]) })), { file: '@polkadot/api/types', diff --git a/packages/typegen/src/util/imports.ts b/packages/typegen/src/util/imports.ts index fd7485b4f3..cbfc0e2833 100644 --- a/packages/typegen/src/util/imports.ts +++ b/packages/typegen/src/util/imports.ts @@ -28,7 +28,6 @@ export interface TypeImports { typesTypes: TypeExist; // `import {} from '@polkadot/types/types` definitions: object; // all definitions typeToModule: Record; - moduleToPackage: Record; } // Maps the types as found to the source location. This is used to generate the @@ -83,34 +82,35 @@ export function setImports (allDefs: object, imports: TypeImports, types: string }); } -// Create an Imports object, can be prefilled with `ignoredTypes` +// Create an Imports object, can be pre-filled with `ignoredTypes` /** @internal */ export function createImports (importDefinitions: Record, { types }: { types: Record } = { types: {} }): TypeImports { const definitions = {} as Record; const typeToModule = {} as Record; - const moduleToPackage = {} as Record; Object.entries(importDefinitions).forEach(([packagePath, packageDef]): void => { Object.entries(packageDef).forEach(([name, moduleDef]): void => { - if (definitions[name]) { - throw new Error(`Duplicated module: ${name}. Packages: ${packagePath}, ${moduleToPackage[name]}`); + const fullName = `${packagePath}/${name}`; + + if (definitions[fullName]) { + throw new Error(`Duplicated module imports ${fullName}`); } - definitions[name] = moduleDef; - moduleToPackage[name] = packagePath; + definitions[fullName] = moduleDef; Object.keys(moduleDef.types).forEach((type): void => { if (typeToModule[type]) { - throw new Error(`Duplicated type: ${type}, found in: ${name}, ${typeToModule[type]}`); + throw new Error(`Duplicated type: ${type}, found in: ${fullName}, ${typeToModule[type]}`); } - typeToModule[type] = name; + typeToModule[type] = fullName; }); }); }); return { codecTypes: {}, + definitions, extrinsicTypes: {}, genericTypes: {}, ignoredTypes: Object.keys(types), @@ -121,8 +121,6 @@ export function createImports (importDefinitions: Record, { type }, {}), primitiveTypes: {}, typesTypes: {}, - definitions, - typeToModule, - moduleToPackage + typeToModule }; } diff --git a/yarn.lock b/yarn.lock index 3f7a2b9e60..3d3b2e81ce 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2802,9 +2802,9 @@ __metadata: languageName: unknown linkType: soft -"@polkadot/dev@npm:^0.50.15": - version: 0.50.15 - resolution: "@polkadot/dev@npm:0.50.15" +"@polkadot/dev@npm:^0.50.16": + version: 0.50.16 + resolution: "@polkadot/dev@npm:0.50.16" dependencies: "@babel/cli": ^7.8.4 "@babel/core": ^7.8.6 @@ -2865,7 +2865,7 @@ __metadata: vuepress: ^1.3.1 webpack: ^4.41.6 webpack-cli: ^3.3.11 - yargs: ^15.1.0 + yargs: ^15.2.0 bin: polkadot-ci-ghact-build: scripts/polkadot-ci-ghact-build.js polkadot-ci-ghact-build.sh: scripts/polkadot-ci-ghact-build.sh @@ -2886,7 +2886,7 @@ __metadata: polkadot-exec-typedoc: scripts/polkadot-exec-typedoc.js polkadot-exec-vuepress: scripts/polkadot-exec-vuepress.js polkadot-exec-webpack: scripts/polkadot-exe-webpackc.js - checksum: 2/f13ba1c552b05fd4815399c6e64c4ca2ce9fc1d603f820b21cb78b4e94e13259315e793dea99bfc249fbc512d6f2f07914826671397dc32c95da6a82216a9b43 + checksum: 2/eb1e710835c352f18036c9979dd3d1d823a7a0c63644400b6dd09e09dbb0c2d55acd2614bf616eec4ec8ec2f4e7b962f2228bef279bdb852da931dcb996d962e languageName: node linkType: hard @@ -15524,7 +15524,7 @@ resolve@1.1.7: "@babel/core": ^7.8.4 "@babel/register": ^7.8.3 "@babel/runtime": ^7.8.4 - "@polkadot/dev": ^0.50.15 + "@polkadot/dev": ^0.50.16 "@polkadot/ts": ^0.3.6 "@polkadot/typegen": "workspace:packages/typegen" copyfiles: ^2.2.0 @@ -18600,6 +18600,16 @@ resolve@1.1.7: languageName: node linkType: hard +"yargs-parser@npm:^17.1.0": + version: 17.1.0 + resolution: "yargs-parser@npm:17.1.0" + dependencies: + camelcase: ^5.0.0 + decamelize: ^1.2.0 + checksum: 2/8e5cc4b16b9558b2cba63b45e574dd72680f44c064623bacfc1fa9805d4f7b9a2008cd261de6b8d78f6cedb1597e5d81728828714e3b5ec897eb1854e31eb0b8 + languageName: node + linkType: hard + "yargs@npm:12.0.5": version: 12.0.5 resolution: "yargs@npm:12.0.5" @@ -18695,6 +18705,25 @@ resolve@1.1.7: languageName: node linkType: hard +"yargs@npm:^15.2.0": + version: 15.2.0 + resolution: "yargs@npm:15.2.0" + dependencies: + cliui: ^6.0.0 + decamelize: ^1.2.0 + find-up: ^4.1.0 + get-caller-file: ^2.0.1 + require-directory: ^2.1.1 + require-main-filename: ^2.0.0 + set-blocking: ^2.0.0 + string-width: ^4.2.0 + which-module: ^2.0.0 + y18n: ^4.0.0 + yargs-parser: ^17.1.0 + checksum: 2/809333561040bc4e7b752289098b652a0deb101c3903000d5cf198d6699c350bd4934baba440e707a784dcfaf0e72342c5bf28440e72626fe033b6b9efb03778 + languageName: node + linkType: hard + "zepto@npm:^1.2.0": version: 1.2.0 resolution: "zepto@npm:1.2.0"