From de8fd128b9516e1ff89d1f42b843e5c819971618 Mon Sep 17 00:00:00 2001 From: Shagen Ogandzhanian Date: Sat, 20 Jun 2020 00:50:02 +0200 Subject: [PATCH] Narrow types for setters wherever it makes sense this supposed to fix boths https://github.com/Kotlin/dukat/issues/311 and https://github.com/Kotlin/dukat/issues/310 --- .../data/typescript/escaping/escaping.d.kt | 6 ++- .../data/typescript/escaping/escaping.d.ts | 1 - .../typescript/escaping/unsupportedNames.d.kt | 4 +- .../removeUnsupportedJsNames.kt | 54 ++++++++++--------- 4 files changed, 37 insertions(+), 28 deletions(-) diff --git a/compiler/test/data/typescript/escaping/escaping.d.kt b/compiler/test/data/typescript/escaping/escaping.d.kt index f11e18ac8..9160abbfc 100644 --- a/compiler/test/data/typescript/escaping/escaping.d.kt +++ b/compiler/test/data/typescript/escaping/escaping.d.kt @@ -27,7 +27,11 @@ external fun `fun`() @Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE") external interface This { operator fun get(key: String): Any? - operator fun set(key: String, value: Any?) + operator fun set(key: String, value: Boolean) + operator fun set(key: String, value: String) + operator fun set(key: String, value: String?) + operator fun set(key: String, value: Any) + operator fun set(key: String, value: Number?) var this_one_shouldnt_be_escaped: Boolean var `when`: String var `typealias`: Number diff --git a/compiler/test/data/typescript/escaping/escaping.d.ts b/compiler/test/data/typescript/escaping/escaping.d.ts index 4dec80fe3..0e88cd8a0 100644 --- a/compiler/test/data/typescript/escaping/escaping.d.ts +++ b/compiler/test/data/typescript/escaping/escaping.d.ts @@ -1,5 +1,4 @@ declare var val: any; -// declare var var: boolean; declare var $foo: boolean; declare function bar$(ba$z: number); declare function fun(); diff --git a/compiler/test/data/typescript/escaping/unsupportedNames.d.kt b/compiler/test/data/typescript/escaping/unsupportedNames.d.kt index 7aa3731a5..66169c487 100644 --- a/compiler/test/data/typescript/escaping/unsupportedNames.d.kt +++ b/compiler/test/data/typescript/escaping/unsupportedNames.d.kt @@ -30,7 +30,9 @@ external interface ExtendeConfigurationBoolean { external interface HeterogenousConfig { operator fun get(key: String): Any? - operator fun set(key: String, value: Any?) + operator fun set(key: String, value: Boolean) + operator fun set(key: String, value: String) + operator fun set(key: String, value: Array) var valid_field: Boolean var other_valid_field: String } \ No newline at end of file diff --git a/model-lowerings/src/org/jetbrains/dukat/commonLowerings/removeUnsupportedJsNames.kt b/model-lowerings/src/org/jetbrains/dukat/commonLowerings/removeUnsupportedJsNames.kt index 5ed066dce..1083d6b0f 100644 --- a/model-lowerings/src/org/jetbrains/dukat/commonLowerings/removeUnsupportedJsNames.kt +++ b/model-lowerings/src/org/jetbrains/dukat/commonLowerings/removeUnsupportedJsNames.kt @@ -99,32 +99,36 @@ private class UnsupportedJsNamesLowering : ModelWithOwnerTypeLowering { body = null ) - val unsupportedSetter = MethodModel( - name = IdentifierEntity("set"), - parameters = listOf(ParameterModel( - name = "key", - type = TypeValueModel(IdentifierEntity("String"), emptyList(), null, null), - initializer = null, - vararg = false, - modifier = null - ), ParameterModel( - name = "value", - type = commonType, - initializer = null, - vararg = false, - modifier = null - )), - type = TypeValueModel(IdentifierEntity("Unit"), emptyList(), null, null), - typeParameters = emptyList(), - static = false, - override = null, - operator = true, - annotations = emptyList(), - open = false, - body = null - ) + val unrolledTypes = unsupportedMembers.mapNotNull { it.getType() } + + val unsupportedSetters = unrolledTypes.map {setterType -> + MethodModel( + name = IdentifierEntity("set"), + parameters = listOf(ParameterModel( + name = "key", + type = TypeValueModel(IdentifierEntity("String"), emptyList(), null, null), + initializer = null, + vararg = false, + modifier = null + ), ParameterModel( + name = "value", + type = setterType, + initializer = null, + vararg = false, + modifier = null + )), + type = TypeValueModel(IdentifierEntity("Unit"), emptyList(), null, null), + typeParameters = emptyList(), + static = false, + override = null, + operator = true, + annotations = emptyList(), + open = false, + body = null + ) + } - listOf(unsupportedGetter, unsupportedSetter) + supportedMembers + listOf(unsupportedGetter) + unsupportedSetters + supportedMembers } else { supportedMembers }