Skip to content

Commit 5331cd4

Browse files
committed
rename TsType to TsTypeAlias, and update docs in tsElements.kt
1 parent ee2f3d1 commit 5331cd4

File tree

3 files changed

+72
-23
lines changed

3 files changed

+72
-23
lines changed

modules/kxs-ts-gen-core/src/commonMain/kotlin/dev.adamko.kxstsgen/KxsTsSourceCodeGenerator.kt

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,19 @@ abstract class KxsTsSourceCodeGenerator(
1616
is TsDeclaration.TsEnum -> generateEnum(element)
1717
is TsDeclaration.TsInterface -> generateInterface(element)
1818
is TsDeclaration.TsNamespace -> generateNamespace(element)
19-
is TsDeclaration.TsType -> generateType(element)
19+
is TsDeclaration.TsTypeAlias -> generateType(element)
2020
}
2121
}
2222

2323
abstract fun generateEnum(enum: TsDeclaration.TsEnum): String
2424
abstract fun generateInterface(element: TsDeclaration.TsInterface): String
2525
abstract fun generateNamespace(namespace: TsDeclaration.TsNamespace): String
26-
abstract fun generateType(element: TsDeclaration.TsType): String
26+
abstract fun generateType(element: TsDeclaration.TsTypeAlias): String
2727

28-
abstract fun generateMapTypeReference(
29-
// requestorId: TsElementId?,
30-
tsMap: TsLiteral.TsMap,
31-
): String
28+
abstract fun generateMapTypeReference(tsMap: TsLiteral.TsMap): String
3229

3330
abstract fun generatePrimitive(primitive: TsLiteral.Primitive): String
34-
abstract fun generateTypeReference(
35-
// rootId: TsElementId?,
36-
typeRef: TsTypeRef,
37-
): String
31+
abstract fun generateTypeReference(typeRef: TsTypeRef): String
3832

3933
open class Default(
4034
config: KxsTsConfig,
@@ -118,6 +112,8 @@ abstract class KxsTsSourceCodeGenerator(
118112
}
119113
}
120114

115+
116+
// note: this isn't used because at present poly-open descriptors are converted to 'any'
121117
private fun generatePolyOpen(
122118
element: TsDeclaration.TsInterface,
123119
polymorphism: TsPolymorphism.Open,
@@ -144,7 +140,7 @@ abstract class KxsTsSourceCodeGenerator(
144140
subInterfaces,
145141
)
146142

147-
val subInterfaceTypeUnion = TsDeclaration.TsType(
143+
val subInterfaceTypeUnion = TsDeclaration.TsTypeAlias(
148144
element.id,
149145
subInterfaceRefs
150146
)
@@ -154,6 +150,14 @@ abstract class KxsTsSourceCodeGenerator(
154150
}
155151
}
156152

153+
/**
154+
* Generate a 'sealed class' equivalent.
155+
*
156+
* * type union of all subclasses
157+
* * a namespace with contains
158+
* * a 'Type' enum
159+
* * subclasses, each with an additional 'Type' property that has a literal 'Type' enum value
160+
*/
157161
private fun generatePolyClosed(
158162
element: TsDeclaration.TsInterface,
159163
polymorphism: TsPolymorphism.Sealed,
@@ -189,7 +193,7 @@ abstract class KxsTsSourceCodeGenerator(
189193
subclass.copy(properties = setOf(typeProp) + subclass.properties)
190194
}
191195

192-
val subInterfaceTypeUnion = TsDeclaration.TsType(
196+
val subInterfaceTypeUnion = TsDeclaration.TsTypeAlias(
193197
element.id,
194198
subInterfaceRefs.keys
195199
)
@@ -208,7 +212,7 @@ abstract class KxsTsSourceCodeGenerator(
208212
}
209213

210214

211-
override fun generateType(element: TsDeclaration.TsType): String {
215+
override fun generateType(element: TsDeclaration.TsTypeAlias): String {
212216
val aliases =
213217
element.typeRefs
214218
.map { generateTypeReference(it) }

modules/kxs-ts-gen-core/src/commonMain/kotlin/dev.adamko.kxstsgen/TsElementConverter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fun interface TsElementConverter {
7575
PolymorphicKind.OPEN -> {
7676
val resultId = context.elementId(descriptor)
7777
val fieldTypeRef = TsTypeRef.Literal(TsLiteral.Primitive.TsAny, false)
78-
TsDeclaration.TsType(resultId, fieldTypeRef)
78+
TsDeclaration.TsTypeAlias(resultId, fieldTypeRef)
7979
}
8080
}
8181
}
@@ -117,7 +117,7 @@ fun interface TsElementConverter {
117117
val resultId = context.elementId(structDescriptor)
118118
val fieldDescriptor = structDescriptor.elementDescriptors.first()
119119
val fieldTypeRef = context.typeRef(fieldDescriptor)
120-
return TsDeclaration.TsType(resultId, fieldTypeRef)
120+
return TsDeclaration.TsTypeAlias(resultId, fieldTypeRef)
121121
}
122122

123123

modules/kxs-ts-gen-core/src/commonMain/kotlin/dev.adamko.kxstsgen/tsElements.kt

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
package dev.adamko.kxstsgen
22

3+
import dev.adamko.kxstsgen.TsProperty.Optional
4+
import dev.adamko.kxstsgen.TsProperty.Required
35
import 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
717
value 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+
*/
1729
sealed 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+
*/
2039
sealed 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. */
5375
sealed 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+
*/
90124
sealed 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+
*/
105146
sealed 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+
*/
121167
sealed 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

Comments
 (0)