Skip to content

Commit 8ab919b

Browse files
broadwaylambSpace Team
authored andcommitted
[JS] Allow exporting parameter names in function types in .d.ts files
Actually exporting those names will be implemented in the next commit.
1 parent 5decea6 commit 8ab919b

File tree

5 files changed

+47
-20
lines changed

5 files changed

+47
-20
lines changed

compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/tsexport/ExportModelGenerator.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,12 @@ class ExportModelGenerator(val context: JsIrBackendContext, val generateNamespac
648648
nonNullType.isArray() -> ExportedType.Array(exportTypeArgument(nonNullType.arguments[0], typeOwner))
649649
nonNullType.isSuspendFunction() -> ExportedType.ErrorType("Suspend functions are not supported")
650650
nonNullType.isFunction() -> ExportedType.Function(
651-
parameterTypes = nonNullType.arguments.dropLast(1).memoryOptimizedMap { exportTypeArgument(it, typeOwner) },
651+
parameters = nonNullType.arguments.dropLast(1).memoryOptimizedMap {
652+
ExportedParameter(
653+
name = null,
654+
type = exportTypeArgument(it, typeOwner),
655+
)
656+
},
652657
returnType = exportTypeArgument(nonNullType.arguments.last(), typeOwner)
653658
)
654659

compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/export/ExportModelGenerator.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,12 @@ class ExportModelGenerator(val context: WasmBackendContext) {
204204
nonNullType == jsRelatedSymbols.jsAnyType -> ExportedType.Primitive.Unknown
205205
nonNullType.isUnit() || nonNullType == context.wasmSymbols.voidType -> ExportedType.Primitive.Unit
206206
nonNullType.isFunction() -> ExportedType.Function(
207-
parameterTypes = nonNullType.arguments.dropLast(1).memoryOptimizedMap { exportTypeArgument(it) },
207+
parameters = nonNullType.arguments.dropLast(1).memoryOptimizedMap {
208+
ExportedParameter(
209+
name = null,
210+
type = exportTypeArgument(it),
211+
)
212+
},
208213
returnType = exportTypeArgument(nonNullType.arguments.last())
209214
)
210215
nonNullType.isNothing() -> ExportedType.Primitive.Nothing

js/typescript-export-model/src/org/jetbrains/kotlin/ir/backend/js/tsexport/ExportModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public data class ExportedObject(
119119
) : ExportedClass()
120120

121121
public class ExportedParameter(
122-
public val name: String,
122+
public val name: String?,
123123
public val type: ExportedType,
124124
public val hasDefaultValue: Boolean = false
125125
)
@@ -162,7 +162,7 @@ public sealed class ExportedType {
162162
}
163163

164164
public class Function(
165-
public val parameterTypes: List<ExportedType>,
165+
public val parameters: List<ExportedParameter>,
166166
public val returnType: ExportedType
167167
) : ExportedType()
168168

js/typescript-export-standalone/src/org/jetbrains/kotlin/js/tsexport/TypeExporter.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.analysis.api.symbols.KaClassKind
1616
import org.jetbrains.kotlin.analysis.api.symbols.KaNamedClassSymbol
1717
import org.jetbrains.kotlin.analysis.api.symbols.KaTypeAliasSymbol
1818
import org.jetbrains.kotlin.analysis.api.types.*
19+
import org.jetbrains.kotlin.ir.backend.js.tsexport.ExportedParameter
1920
import org.jetbrains.kotlin.ir.backend.js.tsexport.ExportedType
2021
import org.jetbrains.kotlin.ir.backend.js.tsexport.ExportedType.*
2122
import org.jetbrains.kotlin.ir.backend.js.tsexport.ExportedType.Array
@@ -97,10 +98,27 @@ internal class TypeExporter(private val config: TypeScriptExportConfig) {
9798
ErrorType("Suspend function types are not supported")
9899
} else {
99100
Function(
100-
parameterTypes = buildList {
101-
type.contextReceivers.mapTo(this) { exportType(it.type) }
102-
type.receiverType?.let { add(exportType(it)) }
103-
type.parameterTypes.mapTo(this) { exportType(it) }
101+
parameters = buildList {
102+
type.contextReceivers.mapTo(this) {
103+
ExportedParameter(
104+
name = null,
105+
type = exportType(it.type),
106+
)
107+
}
108+
type.receiverType?.let {
109+
add(
110+
ExportedParameter(
111+
name = null,
112+
type = exportType(it),
113+
)
114+
)
115+
}
116+
type.parameterTypes.mapTo(this) {
117+
ExportedParameter(
118+
name = null,
119+
type = exportType(it),
120+
)
121+
}
104122
},
105123
returnType = exportType(type.returnType),
106124
)

js/typescript-printer/src/org/jetbrains/kotlin/ir/backend/js/tsexport/ExportModelToTsDeclarations.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -469,15 +469,17 @@ public class ExportModelToTsDeclarations(private val moduleKind: ModuleKind) {
469469

470470
private fun List<ExportedParameter>.generateTypeScriptString(indent: String): String {
471471
var couldBeOptional = true
472-
val parameters = foldRight(mutableListOf<String>()) { it, acc ->
473-
if (!it.hasDefaultValue) couldBeOptional = false
474-
acc.apply { add(0, it.toTypeScript(indent, couldBeOptional)) }
475-
}
476-
return parameters.joinToString(", ")
472+
return asReversed()
473+
.mapIndexed { index, parameter ->
474+
if (!parameter.hasDefaultValue) couldBeOptional = false
475+
parameter.toTypeScript(indent, size - index - 1, couldBeOptional)
476+
}
477+
.asReversed()
478+
.joinToString()
477479
}
478480

479-
private fun ExportedParameter.toTypeScript(indent: String, couldBeOptional: Boolean): String {
480-
val name = makeValidES5Identifier(name, withHash = false)
481+
private fun ExportedParameter.toTypeScript(indent: String, index: Int, couldBeOptional: Boolean): String {
482+
val name = name?.let { makeValidES5Identifier(it, withHash = false) } ?: "p$index"
481483
val type = if (hasDefaultValue && !couldBeOptional) {
482484
ExportedType.UnionType(type, ExportedType.Primitive.Undefined)
483485
} else type
@@ -490,11 +492,8 @@ public class ExportModelToTsDeclarations(private val moduleKind: ModuleKind) {
490492
is ExportedType.Array -> "Array<${elementType.toTypeScript(indent, isInCommentContext)}>"
491493
is ExportedType.ObjectsParentType -> "$ObjectInheritanceIntrinsic<${constructor.toTypeScript(indent, isInCommentContext)}>()"
492494

493-
is ExportedType.Function -> "(" + parameterTypes
494-
.withIndex()
495-
.joinToString(", ") { (index, type) ->
496-
"p$index: ${type.toTypeScript(indent, isInCommentContext)}"
497-
} + ") => " + returnType.toTypeScript(indent, isInCommentContext)
495+
is ExportedType.Function ->
496+
"(" + parameters.generateTypeScriptString(indent) + ") => " + returnType.toTypeScript(indent, isInCommentContext)
498497

499498
is ExportedType.ConstructorType ->
500499
"abstract new " + (if (typeParameters.isNotEmpty()) "<${

0 commit comments

Comments
 (0)