[JS IR] Access with exported properties by its name, not accessor
[JS IR] Export properties to ts as getter/setter, not fields [JS IR] Fix typescript tests Merge-request: KT-MR-5074
This commit is contained in:
@@ -56,6 +56,7 @@ class ExportedProperty(
|
||||
val isStatic: Boolean = false,
|
||||
val isAbstract: Boolean,
|
||||
val isProtected: Boolean,
|
||||
val isField: Boolean,
|
||||
val irGetter: IrFunction?,
|
||||
val irSetter: IrFunction?,
|
||||
val exportedObject: ExportedClass? = null,
|
||||
|
||||
+5
-1
@@ -144,6 +144,7 @@ class ExportModelGenerator(
|
||||
isStatic = false,
|
||||
isAbstract = parentClass?.isInterface == false && property.modality == Modality.ABSTRACT,
|
||||
isProtected = property.visibility == DescriptorVisibilities.PROTECTED,
|
||||
isField = parentClass?.isInterface == true,
|
||||
irGetter = property.getter,
|
||||
irSetter = property.setter
|
||||
)
|
||||
@@ -169,6 +170,7 @@ class ExportModelGenerator(
|
||||
isProtected = false,
|
||||
irGetter = null,
|
||||
irSetter = null,
|
||||
isField = false,
|
||||
)
|
||||
|
||||
val nameProperty = fakeProperty(
|
||||
@@ -195,7 +197,8 @@ class ExportModelGenerator(
|
||||
isProtected = parentClass.visibility == DescriptorVisibilities.PROTECTED,
|
||||
irGetter = context.mapping.enumEntryToGetInstanceFun[irEnumEntry]
|
||||
?: error("Unable to find get instance fun for ${field.fqNameWhenAvailable}"),
|
||||
irSetter = null
|
||||
irSetter = null,
|
||||
isField = false,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -385,6 +388,7 @@ class ExportModelGenerator(
|
||||
irGetter = context.mapping.objectToGetInstanceFunction[klass]!!,
|
||||
irSetter = null,
|
||||
exportedObject = exportedClass,
|
||||
isField = false,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+17
-4
@@ -24,7 +24,7 @@ fun wrapTypeScript(name: String, moduleKind: ModuleKind, dts: String): String {
|
||||
else -> "declare "
|
||||
}
|
||||
val types = """
|
||||
type Nullable<T> = T | null | undefined
|
||||
type Nullable<T> = T | null | undefined
|
||||
${declareKeyword}const __doNotImplementIt: unique symbol
|
||||
type __doNotImplementIt = typeof __doNotImplementIt
|
||||
""".trimIndent().prependIndent(moduleKind.indent) + "\n"
|
||||
@@ -105,7 +105,7 @@ fun ExportedDeclaration.toTypeScript(indent: String, prefix: String = ""): Strin
|
||||
is ExportedProperty -> {
|
||||
val visibility = if (isProtected) "protected " else ""
|
||||
val keyword = when {
|
||||
isMember -> (if (isAbstract) "abstract " else "") + (if (!mutable) "readonly " else "")
|
||||
isMember -> (if (isAbstract) "abstract " else "")
|
||||
else -> if (mutable) "let " else "const "
|
||||
}
|
||||
val possibleStatic = if (isMember && isStatic) "static " else ""
|
||||
@@ -114,7 +114,18 @@ fun ExportedDeclaration.toTypeScript(indent: String, prefix: String = ""): Strin
|
||||
isMember && containsUnresolvedChar -> "\"$name\""
|
||||
else -> name
|
||||
}
|
||||
if (!isMember && containsUnresolvedChar) "" else "$prefix$visibility$possibleStatic$keyword$memberName: ${type.toTypeScript(indent)};"
|
||||
val typeToTypeScript = type.toTypeScript(indent)
|
||||
if (isMember && !isField) {
|
||||
val getter = "$prefix$visibility$possibleStatic${keyword}get $memberName(): $typeToTypeScript;"
|
||||
if (!mutable) getter
|
||||
else getter + "\n" + "$indent$prefix$visibility$possibleStatic${keyword}set $memberName(value: $typeToTypeScript);"
|
||||
} else {
|
||||
if (!isMember && containsUnresolvedChar) ""
|
||||
else {
|
||||
val readonly = if (isMember && !mutable) "readonly " else ""
|
||||
"$prefix$visibility$possibleStatic$keyword$readonly$memberName: $typeToTypeScript;"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
is ExportedClass -> {
|
||||
@@ -202,8 +213,9 @@ fun List<ExportedDeclaration>.withMagicProperty(): List<ExportedDeclaration> {
|
||||
isStatic = false,
|
||||
isAbstract = false,
|
||||
isProtected = false,
|
||||
isField = true,
|
||||
irGetter = null,
|
||||
irSetter = null
|
||||
irSetter = null,
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -250,6 +262,7 @@ fun ExportedClass.toReadonlyProperty(): ExportedProperty {
|
||||
isStatic = false,
|
||||
isAbstract = false,
|
||||
isProtected = false,
|
||||
isField = false,
|
||||
irGetter = null,
|
||||
irSetter = null
|
||||
)
|
||||
|
||||
+1
-1
@@ -124,7 +124,7 @@ fun translateCall(
|
||||
val property = function.correspondingPropertySymbol?.owner
|
||||
if (
|
||||
property != null &&
|
||||
(property.isEffectivelyExternal() || property.isExportedInterfaceMember())
|
||||
(property.isEffectivelyExternal() || property.isExportedMember())
|
||||
) {
|
||||
val propertyName = context.getNameForProperty(property)
|
||||
val nameRef = when (jsDispatchReceiver) {
|
||||
|
||||
@@ -11,6 +11,9 @@ import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||
import org.jetbrains.kotlin.ir.util.parentClassOrNull
|
||||
|
||||
fun IrDeclaration.isExportedMember() =
|
||||
parentClassOrNull.let { it is IrClass && it.isJsExport() }
|
||||
|
||||
fun IrDeclaration?.isExportedClass() =
|
||||
this is IrClass && kind.isClass && isJsExport()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user