Filter unused enum fields for v14 (#4071)

This commit is contained in:
Jaco
2021-10-17 15:05:29 +03:00
committed by GitHub
parent 567504329d
commit d7ab3bfbc1
10 changed files with 194 additions and 467 deletions
+31 -29
View File
@@ -52,38 +52,40 @@ const tsWrapperOpaque = tsExport;
function tsEnum (registry: Registry, definitions: Record<string, ModuleTypes>, { lookupIndex, name: enumName, sub }: TypeDef, imports: TypeImports): string {
setImports(definitions, imports, ['Enum']);
const keys = (sub as TypeDef[]).map((def, index): string => {
const { info, lookupName, name = `unknown${index}`, type } = def;
const getter = stringUpperFirst(stringCamelCase(name.replace(' ', '_')));
const isComplex = [TypeDefInfo.Option, TypeDefInfo.Result, TypeDefInfo.Struct, TypeDefInfo.Tuple, TypeDefInfo.Vec, TypeDefInfo.VecFixed].includes(info);
const asGetter = type === 'Null' || info === TypeDefInfo.DoNotConstruct
? ''
: createGetter(definitions, `as${getter}`, lookupName || (isComplex ? formatType(registry, definitions, info === TypeDefInfo.Struct ? def : type, imports, false) : type), imports);
const isGetter = info === TypeDefInfo.DoNotConstruct
? ''
: createGetter(definitions, `is${getter}`, 'boolean', imports);
const keys = (sub as TypeDef[])
.filter(({ name }) => !!name && !name.startsWith('__Unused'))
.map((def, index): string => {
const { info, lookupName, name = `unknown${index}`, type } = def;
const getter = stringUpperFirst(stringCamelCase(name.replace(' ', '_')));
const isComplex = [TypeDefInfo.Option, TypeDefInfo.Result, TypeDefInfo.Struct, TypeDefInfo.Tuple, TypeDefInfo.Vec, TypeDefInfo.VecFixed].includes(info);
const asGetter = type === 'Null' || info === TypeDefInfo.DoNotConstruct
? ''
: createGetter(definitions, `as${getter}`, lookupName || (isComplex ? formatType(registry, definitions, info === TypeDefInfo.Struct ? def : type, imports, false) : type), imports);
const isGetter = info === TypeDefInfo.DoNotConstruct
? ''
: createGetter(definitions, `is${getter}`, 'boolean', imports);
switch (info) {
case TypeDefInfo.Compact:
case TypeDefInfo.Plain:
case TypeDefInfo.Result:
case TypeDefInfo.Si:
case TypeDefInfo.Struct:
case TypeDefInfo.Tuple:
case TypeDefInfo.Vec:
case TypeDefInfo.Option:
case TypeDefInfo.VecFixed:
case TypeDefInfo.WrapperOpaque:
return `${isGetter}${asGetter}`;
switch (info) {
case TypeDefInfo.Compact:
case TypeDefInfo.Plain:
case TypeDefInfo.Result:
case TypeDefInfo.Si:
case TypeDefInfo.Struct:
case TypeDefInfo.Tuple:
case TypeDefInfo.Vec:
case TypeDefInfo.Option:
case TypeDefInfo.VecFixed:
case TypeDefInfo.WrapperOpaque:
return `${isGetter}${asGetter}`;
case TypeDefInfo.DoNotConstruct:
case TypeDefInfo.Null:
return `${isGetter}`;
case TypeDefInfo.DoNotConstruct:
case TypeDefInfo.Null:
return `${isGetter}`;
default:
throw new Error(`Enum: ${enumName || 'undefined'}: Unhandled type ${TypeDefInfo[info]}, ${stringify(def)}`);
}
});
default:
throw new Error(`Enum: ${enumName || 'undefined'}: Unhandled type ${TypeDefInfo[info]}, ${stringify(def)}`);
}
});
return exportInterface(lookupIndex, enumName, 'Enum', keys.join(''));
}
+5 -4
View File
@@ -66,13 +66,14 @@ export function getSimilarTypes (registry: Registry, definitions: Record<string,
}
}
} else if (isChildClass(Enum, Clazz)) {
const e = new (Clazz as Constructor)(registry) as Enum;
const { defKeys, isBasic } = new (Clazz as Constructor)(registry) as Enum;
const keys = defKeys.filter((v) => !v.startsWith('__Unused'));
if (e.isBasic) {
possibleTypes.push(arrayToStrType(e.defKeys), 'number');
if (isBasic) {
possibleTypes.push(arrayToStrType(keys), 'number');
} else {
// TODO We don't really want any here, these should be expanded
possibleTypes.push(...e.defKeys.map((key): string => `{ ${key}: any }`), 'string');
possibleTypes.push(...keys.map((k) => `{ ${k}: any }`), 'string');
}
possibleTypes.push('Uint8Array');