Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ts-generator supports:
* Parenthesis optimization: They are placed only when they are needed to disambiguate.
* Emitting either `null` or `undefined` for JVM nullable types.
* Functions
* Constructors

## No Proper Way To Install For Now

Expand Down
24 changes: 24 additions & 0 deletions src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,35 @@ class TypeScriptGenerator(


return "$typeKeyword ${klass.simpleName}$templateParameters$extendsString {\n" +
constructorsOf(klass) +
propertiesOf(klass) +
functionsOf(klass) +
"}"
}

private fun constructorsOf(klass: KClass<*>): String = try {
klass.constructors.joinToString("") { constructor ->
val parameters = constructor.parameters
.drop(1)
.joinToString(", ") { param ->
val paramType = pipeline.transformFunctionParameterType(param.type, param, constructor, klass)
"${param.name}: ${formatKType(paramType).formatWithoutParenthesis()}"

}
val visibility = when (constructor.visibility) {
KVisibility.PRIVATE -> "private "
KVisibility.PROTECTED -> "protected "
KVisibility.PUBLIC -> ""
KVisibility.INTERNAL -> ""
else -> ""
}
" ${visibility}constructor($parameters)\n"
}
} catch (exception: kotlin.reflect.jvm.internal.KotlinReflectionInternalError) {
print(exception.toString())
""
}

private fun functionsOf(klass: KClass<*>): String = try {
klass.declaredMemberFunctions
.let { functionsList ->
Expand Down
Loading