Skip to content

Commit 55ccdd2

Browse files
authored
Add support for Range/RangeInclusive typegen (#4802)
1 parent 0449dd7 commit 55ccdd2

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# CHANGELOG
22

3+
## master
4+
5+
Changes:
6+
7+
- Added support for typegen over `Range/RangeInclusive` types
8+
9+
310
## 8.3.2 May 8, 2022
411

512
Contributed:

packages/typegen/src/generate/tsDef.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ function tsExport (registry: Registry, definitions: Record<string, ModuleTypes>,
4242
return exportInterface(def.lookupIndex, def.name, formatType(registry, definitions, def, imports, false));
4343
}
4444

45-
const tsBTreeMap = tsExport;
46-
const tsBTreeSet = tsExport;
47-
const tsCompact = tsExport;
48-
const tsDoNotConstruct = tsExport;
49-
const tsHashMap = tsExport;
50-
const tsOption = tsExport;
51-
const tsPlain = tsExport;
52-
const tsTuple = tsExport;
53-
const tsWrapperKeepOpaque = tsExport;
54-
const tsWrapperOpaque = tsExport;
55-
5645
/** @internal */
5746
function tsEnum (registry: Registry, definitions: Record<string, ModuleTypes>, { lookupIndex, name: enumName, sub }: TypeDef, imports: TypeImports): string {
5847
setImports(definitions, imports, ['Enum']);
@@ -227,29 +216,29 @@ function tsVec (registry: Registry, definitions: Record<string, ModuleTypes>, de
227216
// `generators[typedef.info](...)` TS will show any unhandled types. Rather
228217
// we are being explicit in having no handlers where we do not support (yet)
229218
export const typeEncoders: Record<TypeDefInfo, (registry: Registry, definitions: Record<string, ModuleTypes>, def: TypeDef, imports: TypeImports) => string> = {
230-
[TypeDefInfo.BTreeMap]: tsBTreeMap,
231-
[TypeDefInfo.BTreeSet]: tsBTreeSet,
232-
[TypeDefInfo.Compact]: tsCompact,
233-
[TypeDefInfo.DoNotConstruct]: tsDoNotConstruct,
219+
[TypeDefInfo.BTreeMap]: tsExport,
220+
[TypeDefInfo.BTreeSet]: tsExport,
221+
[TypeDefInfo.Compact]: tsExport,
222+
[TypeDefInfo.DoNotConstruct]: tsExport,
234223
[TypeDefInfo.Enum]: tsEnum,
235-
[TypeDefInfo.HashMap]: tsHashMap,
224+
[TypeDefInfo.HashMap]: tsExport,
236225
[TypeDefInfo.Int]: tsInt,
237226
[TypeDefInfo.Linkage]: errorUnhandled,
238227
[TypeDefInfo.Null]: tsNull,
239-
[TypeDefInfo.Option]: tsOption,
240-
[TypeDefInfo.Plain]: tsPlain,
241-
[TypeDefInfo.Range]: errorUnhandled,
242-
[TypeDefInfo.RangeInclusive]: errorUnhandled,
228+
[TypeDefInfo.Option]: tsExport,
229+
[TypeDefInfo.Plain]: tsExport,
230+
[TypeDefInfo.Range]: tsExport,
231+
[TypeDefInfo.RangeInclusive]: tsExport,
243232
[TypeDefInfo.Result]: tsResult,
244233
[TypeDefInfo.Set]: tsSet,
245234
[TypeDefInfo.Si]: tsSi,
246235
[TypeDefInfo.Struct]: tsStruct,
247-
[TypeDefInfo.Tuple]: tsTuple,
236+
[TypeDefInfo.Tuple]: tsExport,
248237
[TypeDefInfo.UInt]: tsUInt,
249238
[TypeDefInfo.Vec]: tsVec,
250239
[TypeDefInfo.VecFixed]: tsVec,
251-
[TypeDefInfo.WrapperKeepOpaque]: tsWrapperKeepOpaque,
252-
[TypeDefInfo.WrapperOpaque]: tsWrapperOpaque
240+
[TypeDefInfo.WrapperKeepOpaque]: tsExport,
241+
[TypeDefInfo.WrapperOpaque]: tsExport
253242
};
254243

255244
/** @internal */

packages/typegen/src/util/formatting.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ const formatters: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef, def
175175
},
176176

177177
[TypeDefInfo.Range]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
178-
throw new Error(`TypeDefInfo.Range: Not implemented on ${stringify(typeDef)}`);
178+
return singleParamNotation(registry, 'Range', typeDef, definitions, imports, withShortcut);
179179
},
180180

181181
[TypeDefInfo.RangeInclusive]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
182-
throw new Error(`TypeDefInfo.RangeInclusive: Not implemented on ${stringify(typeDef)}`);
182+
return singleParamNotation(registry, 'RangeInclusive', typeDef, definitions, imports, withShortcut);
183183
},
184184

185185
[TypeDefInfo.Set]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {

packages/types/src/metadata/PortableRegistry/PortableRegistry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const BITVEC_NS_MSB = ['bitvec::order::Msb0', 'BitOrderMsb0'];
5454
const BITVEC_NS = [...BITVEC_NS_LSB, ...BITVEC_NS_MSB];
5555

5656
// These we never use these as top-level names, they are wrappers
57-
const WRAPPERS = ['BoundedBTreeMap', 'BoundedBTreeSet', 'BoundedVec', 'Box', 'BTreeMap', 'BTreeSet', 'Cow', 'Result', 'Option', 'WeakBoundedVec', 'WrapperKeepOpaque', 'WrapperOpaque'];
57+
const WRAPPERS = ['BoundedBTreeMap', 'BoundedBTreeSet', 'BoundedVec', 'Box', 'BTreeMap', 'BTreeSet', 'Cow', 'Option', 'Range', 'RangeInclusive', 'Result', 'WeakBoundedVec', 'WrapperKeepOpaque', 'WrapperOpaque'];
5858

5959
// These are reserved and/or conflicts with built-in Codec or JS definitions
6060
const RESERVED = ['entries', 'hash', 'keys', 'new', 'size'];

0 commit comments

Comments
 (0)