fix(d.ts for Default Argument Value): add question mark to exported funcitons with default parameter.

This commit is contained in:
Artem Kobzar
2022-01-24 13:31:13 +01:00
committed by Space
parent 4c5eb9ac32
commit 0cf1ebd686
5 changed files with 18 additions and 5 deletions
@@ -15,5 +15,6 @@ object JsLoweredDeclarationOrigin : IrDeclarationOrigin {
object BRIDGE_WITHOUT_STABLE_NAME : IrDeclarationOriginImpl("BRIDGE_WITHOUT_STABLE_NAME")
object OBJECT_GET_INSTANCE_FUNCTION : IrDeclarationOriginImpl("OBJECT_GET_INSTANCE_FUNCTION")
object JS_SHADOWED_EXPORT : IrDeclarationOriginImpl("JS_SHADOWED_EXPORT")
object JS_SHADOWED_DEFAULT_PARAMETER : IrDeclarationOriginImpl("JS_SHADOWED_DEFAULT_PARAMETER")
object ENUM_GET_INSTANCE_FUNCTION : IrDeclarationOriginImpl("ENUM_GET_INSTANCE_FUNCTION")
}
@@ -80,7 +80,8 @@ data class ExportedClass(
class ExportedParameter(
val name: String,
val type: ExportedType
val type: ExportedType,
val hasDefaultValue: Boolean = false
)
sealed class ExportedType {
@@ -110,7 +110,11 @@ class ExportModelGenerator(
if (parameterName in allReservedWords)
parameterName = "_$parameterName"
return ExportedParameter(parameterName, exportType(parameter.type))
return ExportedParameter(
parameterName,
exportType(parameter.type),
parameter.origin == JsLoweredDeclarationOrigin.JS_SHADOWED_DEFAULT_PARAMETER
)
}
private fun exportProperty(property: IrProperty): ExportedDeclaration? {
@@ -268,8 +268,12 @@ fun ExportedClass.toReadonlyProperty(): ExportedProperty {
)
}
fun ExportedParameter.toTypeScript(indent: String): String =
"${sanitizeName(name, withHash = false)}: ${type.toTypeScript(indent)}"
fun ExportedParameter.toTypeScript(indent: String): String {
val name = sanitizeName(name, withHash = false)
val type = type.toTypeScript(indent)
val questionMark = if (hasDefaultValue) "?" else ""
return "$name$questionMark: $type"
}
fun ExportedType.toTypeScript(indent: String): String = when (this) {
is ExportedType.Primitive -> typescript
@@ -112,7 +112,10 @@ class ExportedDefaultParameterStub(val context: JsIrBackendContext) : Declaratio
exportedDefaultStubFun.parent = declaration.parent
exportedDefaultStubFun.copyParameterDeclarationsFrom(declaration)
exportedDefaultStubFun.returnType = declaration.returnType.remapTypeParameters(declaration, exportedDefaultStubFun)
exportedDefaultStubFun.valueParameters.forEach { it.defaultValue = null }
exportedDefaultStubFun.valueParameters.forEach {
it.defaultValue = null
it.origin = JsLoweredDeclarationOrigin.JS_SHADOWED_DEFAULT_PARAMETER
}
declaration.origin = JsLoweredDeclarationOrigin.JS_SHADOWED_EXPORT