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

Commit 7889ac5

Browse files
epabstSchahen
authored andcommitted
Don't fully qualify reference to types when not needed (#98)
1 parent 16ef64d commit 7889ac5

File tree

6 files changed

+28
-4
lines changed

6 files changed

+28
-4
lines changed

src/main/kotlin/converter/typeUtils.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ private fun ObjectTypeToKotlinTypeMapper.buildFqn(symbol: Symbol): KtQualifiedNa
219219
// TODO: make something better for the case when we have more than one declaration for this symbol.
220220
// For example see how it work for testData/typeAlias/typeParams.d.ts after renaming `MyHeaders` to `Headers`
221221
val declaration = symbol.declarations?.singleOrNull()
222-
return declaration?.let { buildFqn(it) } ?: KtQualifiedName(typeChecker.getFullyQualifiedName(symbol))
222+
return declaration?.let { buildFqn(it) } ?: KtQualifiedName(symbolToString(symbol))
223+
}
224+
225+
private fun ObjectTypeToKotlinTypeMapper.symbolToString(symbol: Symbol): String {
226+
return enclosingDeclaration?.let { typeChecker.symbolToString(symbol, it) } ?: typeChecker.symbolToString(symbol)
223227
}
224228

225229
private fun ObjectTypeToKotlinTypeMapper.buildFqn(declaration: Node): KtQualifiedName? {

src/main/kotlin/ts2kt/TypeScriptToKotlin.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ class TypeScriptToKotlin(
229229
declarations = innerPackagePartBuilder.members,
230230
defaultAnnotations = additionalAnnotations,
231231
typeChecker = typeChecker,
232-
currentPackage = newQualifier.joinToString(".")
232+
currentPackage = newQualifier.joinToString("."),
233+
enclosingDeclaration = node
233234
)
234235
val tr = TypeScriptToKotlin(
235236
currentPackagePartBuilder = innerPackagePartBuilder,

src/main/kotlin/ts2kt/mappingObjectTypeToKotlinType.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface ObjectTypeToKotlinTypeMapper {
2424
fun getKotlinTypeForObjectType(objectType: TypeLiteralNode): KtType
2525
fun withTypeParameters(typeParameters: NodeArray<TypeParameterDeclaration>?): ObjectTypeToKotlinTypeMapper
2626
val currentPackage: String
27+
val enclosingDeclaration: Node?
2728
val typeChecker: TypeChecker
2829
// TODO: support recursive type aliases, see recursiveType.d.ts
2930
val typesInMappingProcess: MutableSet<Type>
@@ -34,6 +35,7 @@ data class ObjectTypeToKotlinTypeMapperImpl(
3435
val defaultAnnotations: List<KtAnnotation>,
3536
val declarations: MutableList<KtMember>,
3637
override val currentPackage: String,
38+
override val enclosingDeclaration: Node? = null,
3739
val typeParameterDeclarations: List<TypeParameterDeclaration> = listOf(),
3840
val cache: MutableMap<String, KtType> = hashMapOf()
3941
) : ObjectTypeToKotlinTypeMapper {

testData/objectType/asTypeAliasInNamespaces.d.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package asTypeAliasInNamespaces.a
44
external interface I {
55
fun foo(): String
66
}
7-
external fun foo(): a.I = definedExternally
7+
external fun foo(): I = definedExternally
88
external fun bar(): b.I = definedExternally
99

1010
// ------------------------------------------------------------------------------------------
@@ -14,5 +14,5 @@ package asTypeAliasInNamespaces.b
1414
external interface I {
1515
fun foo(): String
1616
}
17-
external fun foo(): b.I = definedExternally
17+
external fun foo(): I = definedExternally
1818
external fun bar(): a.I = definedExternally
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package useExportedType
2+
3+
external interface StructureType {
4+
var name: String
5+
var details: String? get() = definedExternally; set(value) = definedExternally
6+
}
7+
external interface Registry {
8+
fun register(type: StructureType)
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type StructureType = {
2+
name: string;
3+
details?: string;
4+
};
5+
6+
export interface Registry {
7+
register(type: StructureType)
8+
}

0 commit comments

Comments
 (0)