Skip to content

Commit

Permalink
Narrow types for setters wherever it makes sense
Browse files Browse the repository at this point in the history
this supposed to fix boths #311
and #310
  • Loading branch information
Schahen committed Jun 19, 2020
1 parent 74e9066 commit de8fd12
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
6 changes: 5 additions & 1 deletion compiler/test/data/typescript/escaping/escaping.d.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion compiler/test/data/typescript/escaping/escaping.d.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
4 changes: 3 additions & 1 deletion compiler/test/data/typescript/escaping/unsupportedNames.d.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean>)
var valid_field: Boolean
var other_valid_field: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit de8fd12

Please sign in to comment.