11package dev.adamko.kxstsgen
22
3+ import dev.adamko.kxstsgen.TsProperty.Optional
4+ import dev.adamko.kxstsgen.TsProperty.Required
35import kotlin.jvm.JvmInline
6+ import kotlinx.serialization.descriptors.SerialDescriptor
47
58
9+ /* *
10+ * A unique identifier for a [TsElement].
11+ *
12+ * This is usually generated from [SerialDescriptor.serialName].
13+ */
14+ // Note: A value class probably isn't the best choice here. The manual String manipulation is
15+ // restrictive and clunky, and makes nested references very difficult to decipher.
616@JvmInline
717value class TsElementId (private val id : String ) {
818 val namespace: String
@@ -13,14 +23,25 @@ value class TsElementId(private val id: String) {
1323 override fun toString (): String = id
1424}
1525
16-
26+ /* *
27+ * Some TypeScript source code element. Either a [TsLiteral] or a [TsDeclaration].
28+ */
1729sealed interface TsElement
1830
1931
32+ /* *
33+ * Declarations are named elements that developers create in TypeScript source code.
34+ *
35+ * For example, an [interface][TsDeclaration.TsInterface] is declared. In contrast, the interface
36+ * may have a [string][TsLiteral.Primitive.TsString] property, which is represented as a
37+ * [TsLiteral].
38+ */
2039sealed interface TsDeclaration : TsElement {
2140 val id: TsElementId
2241
23- data class TsType (
42+
43+ /* * A named reference to one or more other types. */
44+ data class TsTypeAlias (
2445 override val id : TsElementId ,
2546 val typeRefs : Set <TsTypeRef >,
2647 ) : TsDeclaration {
@@ -50,6 +71,7 @@ sealed interface TsDeclaration : TsElement {
5071}
5172
5273
74+ /* * Literal built-in TypeScript elements. */
5375sealed interface TsLiteral : TsElement {
5476
5577 sealed interface Primitive : TsLiteral {
@@ -61,17 +83,23 @@ sealed interface TsLiteral : TsElement {
6183 object TsObject : Primitive
6284
6385 object TsAny : Primitive
86+
87+ // the remaining primitives are defined, but unused
6488 object TsNever : Primitive
6589 object TsNull : Primitive
6690 object TsUndefined : Primitive
6791 object TsUnknown : Primitive
6892 object TsVoid : Primitive
6993 }
7094
95+
96+ /* * A list with elements of type [valueTypeRef]. */
7197 data class TsList (
7298 val valueTypeRef : TsTypeRef ,
7399 ) : TsLiteral
74100
101+
102+ /* * A key-value map. */
75103 data class TsMap (
76104 val keyTypeRef : TsTypeRef ,
77105 val valueTypeRef : TsTypeRef ,
@@ -87,11 +115,19 @@ sealed interface TsLiteral : TsElement {
87115}
88116
89117
118+ /* *
119+ * A reference to some [TsElement]. The reference may be [nullable].
120+ *
121+ * A reference does not require the target to be generated.
122+ * This helps prevent circular dependencies causing a lock.
123+ */
90124sealed interface TsTypeRef {
91125 val nullable: Boolean
92126
127+
93128 data class Literal (val element : TsLiteral , override val nullable : Boolean ) : TsTypeRef
94129
130+
95131 data class Declaration (
96132 val id : TsElementId ,
97133 val parent : Declaration ? ,
@@ -101,40 +137,49 @@ sealed interface TsTypeRef {
101137}
102138
103139
104- // source: kxs
140+ /* *
141+ * A property within an [interface][TsDeclaration.TsInterface]
142+ *
143+ * In property may be [Required] or [Optional]. See the TypeScript docs:
144+ * ['Optional Properties'](https://www.typescriptlang.org/docs/handbook/2/objects.html#optional-properties)
145+ */
105146sealed interface TsProperty {
106147 val name: String
107148 val typeRef: TsTypeRef
108149
150+
109151 data class Required (
110152 override val name : String ,
111153 override val typeRef : TsTypeRef ,
112154 ) : TsProperty
113155
156+
114157 data class Optional (
115158 override val name : String ,
116159 override val typeRef : TsTypeRef ,
117160 ) : TsProperty
118161}
119162
120163
164+ /* *
165+ * Meta-data about the polymorphism of a [TsDeclaration.TsInterface].
166+ */
121167sealed interface TsPolymorphism {
122168
169+ /* * The name of the field used to discriminate between [subclasses]. */
123170 val discriminatorName: String
124171 val subclasses: Set <TsDeclaration .TsInterface >
125172
173+
126174 data class Sealed (
127175 override val discriminatorName : String ,
128176 override val subclasses : Set <TsDeclaration .TsInterface >,
129177 ) : TsPolymorphism
130178
179+
180+ /* * Note: [Open] is not implemented correctly */
131181 data class Open (
132182 override val discriminatorName : String ,
133183 override val subclasses : Set <TsDeclaration .TsInterface >,
134184 ) : TsPolymorphism
135-
136- // object None : TsPolymorphism {
137- // override val discriminatorName: Nothing = error("not implemented")
138- // override val subclasses: Nothing = error("not implemented")
139- // }
140185}
0 commit comments