Handle import module name conflicts (#1983)

This commit is contained in:
Jaco Greeff
2020-03-02 00:07:34 +01:00
committed by GitHub
parent 3e702b8629
commit b588238836
10 changed files with 80 additions and 47 deletions
+5 -5
View File
@@ -39,16 +39,16 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: Record<strin
writeFile(dest, (): string => {
const allTypes: Record<string, Record<string, { types: Record<string, any> }>> = { '@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 = [
@@ -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])
}))
]);
+5 -5
View File
@@ -96,16 +96,16 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr
writeFile(dest, (): string => {
const allTypes: Record<string, Record<string, { types: Record<string, any> }>> = { '@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',
+12 -7
View File
@@ -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<string[]>((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<T = Codec>(key: any, block?: Hash | Uint8Array | string): Observable<T>;';
} else if (method.method === 'subscribeStorage') {
return ' subscribeStorage<T = Codec[]>(keys: any[]): Observable<T>;';
@@ -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',
+3 -3
View File
@@ -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])
}))
]);
+5 -5
View File
@@ -63,16 +63,16 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr
writeFile(dest, (): string => {
const allTypes: Record<string, Record<string, { types: Record<string, any> }>> = { '@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',
+10 -12
View File
@@ -28,7 +28,6 @@ export interface TypeImports {
typesTypes: TypeExist; // `import {} from '@polkadot/types/types`
definitions: object; // all definitions
typeToModule: Record<string, string>;
moduleToPackage: Record<string, string>;
}
// 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<string, object>, { types }: { types: Record<string, any> } = { types: {} }): TypeImports {
const definitions = {} as Record<string, object>;
const typeToModule = {} as Record<string, string>;
const moduleToPackage = {} as Record<string, string>;
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<string, object>, { type
}, {}),
primitiveTypes: {},
typesTypes: {},
definitions,
typeToModule,
moduleToPackage
typeToModule
};
}