feat: add implicit export support for Kotlin/JS

This commit is contained in:
Artem Kobzar
2021-11-16 16:59:18 +00:00
committed by Space
parent f05436b939
commit af924fd3d1
10 changed files with 161 additions and 7 deletions
@@ -120,6 +120,14 @@ sealed class ExportedType {
class IntersectionType(val lhs: ExportedType, val rhs: ExportedType) : ExportedType()
fun withNullability(nullable: Boolean) =
class ImplicitlyExportedType(val type: ExportedType) : ExportedType() {
override fun withNullability(nullable: Boolean) =
ImplicitlyExportedType(type.withNullability(nullable))
}
open fun withNullability(nullable: Boolean) =
if (nullable) Nullable(this) else this
fun withImplicitlyExported(implicitlyExportedType: Boolean) =
if (implicitlyExportedType) ImplicitlyExportedType(this) else this
}
@@ -468,6 +468,7 @@ class ExportModelGenerator(
classifier is IrClassSymbol -> {
val klass = classifier.owner
val isImplicitlyExported = !klass.isExported(context)
val name = if (generateNamespacesForPackages) klass.fqNameWhenAvailable!!.asString() else klass.name.asString()
when (klass.kind) {
@@ -484,7 +485,7 @@ class ExportModelGenerator(
name,
type.arguments.map { exportTypeArgument(it) }
)
}
}.withImplicitlyExported(isImplicitlyExported)
}
else -> error("Unexpected classifier $classifier")
@@ -114,10 +114,8 @@ fun ExportedDeclaration.toTypeScript(indent: String, prefix: String = ""): Strin
val keyword = if (isInterface) "interface" else "class"
val superInterfacesKeyword = if (isInterface) "extends" else "implements"
val superClassClause = superClass?.let { " extends ${it.toTypeScript(indent)}" } ?: ""
val superInterfacesClause = if (superInterfaces.isNotEmpty()) {
" $superInterfacesKeyword " + superInterfaces.joinToString(", ") { it.toTypeScript(indent) }
} else ""
val superClassClause = superClass?.let { it.toExtendsClause(indent) } ?: ""
val superInterfacesClause = superInterfaces.toImplementsClause(superInterfacesKeyword, indent)
val members = members.map {
if (!ir.isInner || it !is ExportedFunction || !it.isStatic) {
@@ -151,12 +149,36 @@ fun ExportedDeclaration.toTypeScript(indent: String, prefix: String = ""): Strin
val nestedClasses = nonInnerClasses + innerClasses.map { it.withProtectedConstructors() }
val klassExport = "$prefix$modifiers$keyword $name$renderedTypeParameters$superClassClause$superInterfacesClause {\n$bodyString}"
val staticsExport = if (nestedClasses.isNotEmpty()) "\n" + ExportedNamespace(name, nestedClasses).toTypeScript(indent, prefix) else ""
val staticsExport =
if (nestedClasses.isNotEmpty()) "\n" + ExportedNamespace(name, nestedClasses).toTypeScript(indent, prefix) else ""
if (name.isValidES5Identifier()) klassExport + staticsExport else ""
}
}
fun ExportedType.toExtendsClause(indent: String): String {
return when (this) {
is ExportedType.ImplicitlyExportedType -> " /*${type.toExtendsClause(indent)} */"
else -> " extends ${toTypeScript(indent)}"
}
}
fun List<ExportedType>.toImplementsClause(superInterfacesKeyword: String, indent: String): String {
val (exportedInterfaces, nonExportedInterfaces) = partition { it !is ExportedType.ImplicitlyExportedType }
val listOfNonExportedInterfaces = nonExportedInterfaces.joinToString(", ") {
(it as ExportedType.ImplicitlyExportedType).type.toTypeScript(indent)
}
return when {
exportedInterfaces.isEmpty() && nonExportedInterfaces.isNotEmpty() ->
" /* $superInterfacesKeyword $listOfNonExportedInterfaces */"
exportedInterfaces.isNotEmpty() -> {
val nonExportedInterfacesTsString = if (nonExportedInterfaces.isNotEmpty()) "/*, $listOfNonExportedInterfaces */" else ""
" $superInterfacesKeyword " + exportedInterfaces.joinToString(", ") { it.toTypeScript(indent) } + nonExportedInterfacesTsString
}
else -> ""
}
}
fun IrClass.asNestedClassAccess(): String {
val name = getJsNameOrKotlinName().identifier
if (parent !is IrClass) return name
@@ -235,4 +257,7 @@ fun ExportedType.toTypeScript(indent: String): String = when (this) {
}
is ExportedType.LiteralType.StringLiteralType -> "\"$value\""
is ExportedType.LiteralType.NumberLiteralType -> value.toString()
is ExportedType.ImplicitlyExportedType -> {
ExportedType.Primitive.Any.toTypeScript(indent) + "/* ${type.toTypeScript("")} */"
}
}