Skip to content
This repository was archived by the owner on Nov 6, 2019. It is now read-only.

Commit 75e6529

Browse files
committed
Adjust for feedback
1 parent 252f1ba commit 75e6529

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/main/kotlin/converter/typeUtils.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,28 @@ private fun ObjectTypeToKotlinTypeMapper.mapUnionType(type: UnionOrIntersectionT
115115

116116
val mappedTypes = notNullTypes.map { mapType(it, null) }
117117
return KtTypeUnion(when {
118-
!nullable -> mappedTypes.merge()
118+
!nullable -> mappedTypes.mergeToPreventCompilationConflicts()
119119
notNullTypes.size == 1 -> mappedTypes.map { it.copy(isNullable = true) }
120120
else -> (mappedTypes + KtType(NOTHING, isNullable = true)).distinct()
121121
})
122122
}
123123

124-
private fun List<KtType>.merge(): List<KtType> {
125-
return groupBy { it.copy(comment = null) }.map { entry ->
126-
val comments = entry.value.map { it.comment }.filterNotNull()
124+
/**
125+
* Normalize to a KtType such that equals will be true if the KotlinJS compiler would consider them
126+
* conflicting if both were the only parameter to identically named functions.
127+
*/
128+
private fun KtType.normalizeToDetectCompilationConflicts(): KtType {
129+
return copy(comment = null, typeArgs = typeArgs.map { it.normalizeToDetectCompilationConflicts() })
130+
}
131+
132+
/**
133+
* Handle the case where a function is overloaded with the same Kotlin parameter types by merging them.
134+
* Comments are not taken into account for the comparison, but are preserved by concatenation using "|" since a "union".
135+
* This is especially useful for Typescript string literal unions.
136+
*/
137+
private fun List<KtType>.mergeToPreventCompilationConflicts(): List<KtType> {
138+
return groupBy { it.normalizeToDetectCompilationConflicts() }.map { entry ->
139+
val comments = entry.value.mapNotNull { it.comment }
127140
entry.key.copy(comment = if (comments.isEmpty()) null else comments.joinToString(" | "))
128141
}
129142
}

0 commit comments

Comments
 (0)