Skip to content

Commit 6c2d196

Browse files
authored
Name unions and fix ambiguous types (#1027) (#1033)
* Add "@codegen_names" jstag to identify union members * Extract unions to named types, and fix some type declarations * Add boolean/enum leniency to the TS generator
1 parent 445c7c6 commit 6c2d196

File tree

74 files changed

+3487
-4236
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+3487
-4236
lines changed

compiler/model/metamodel.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ export abstract class BaseType {
160160
kind: string
161161
/** Variant name for externally tagged variants */
162162
variantName?: string
163+
/**
164+
* Additional identifiers for use by code generators. Usage depends on the actual type:
165+
* - on unions (modeled as alias(union_of)), these are identifiers for the union members
166+
* - for additional properties, this is the name of the dict that holds these properties
167+
* - for additional property, this is the name of the key and value fields that hold the
168+
* additional property
169+
*/
170+
codegenNames?: string[]
163171
}
164172

165173
export type Variants = ExternalTag | InternalTag | Container

compiler/model/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ export function hoistTypeAnnotations (type: model.TypeDefinition, jsDocs: JSDoc[
627627
// We want to enforce a single jsDoc block.
628628
assert(jsDocs, jsDocs.length < 2, 'Use a single multiline jsDoc block instead of multiple single line blocks')
629629

630-
const validTags = ['class_serializer', 'doc_url', 'behavior', 'variants', 'variant', 'shortcut_property']
630+
const validTags = ['class_serializer', 'doc_url', 'behavior', 'variants', 'variant', 'shortcut_property', 'codegen_names']
631631
const tags = parseJsDocTags(jsDocs)
632632
if (jsDocs.length === 1) {
633633
const description = jsDocs[0].getDescription()
@@ -648,6 +648,12 @@ export function hoistTypeAnnotations (type: model.TypeDefinition, jsDocs: JSDoc[
648648
} else if (tag === 'doc_url') {
649649
assert(jsDocs, isValidUrl(value), '@doc_url is not a valid url')
650650
type.docUrl = value
651+
} else if (tag === 'codegen_names') {
652+
type.codegenNames = value.split(',').map(v => v.trim())
653+
assert(jsDocs,
654+
type.kind === 'type_alias' && type.type.kind === 'union_of' && type.type.items.length === type.codegenNames.length,
655+
'@codegen_names must have the number of items as the union definition'
656+
)
651657
} else {
652658
assert(jsDocs, false, `Unhandled tag: '${tag}' with value: '${value}' on type ${type.name.name}`)
653659
}

0 commit comments

Comments
 (0)