1
+ package org.jetbrains.dukat.tsLowerings
2
+
3
+ import org.jetbrains.dukat.astCommon.IdentifierEntity
4
+ import org.jetbrains.dukat.ownerContext.NodeOwner
5
+ import org.jetbrains.dukat.tsmodel.ClassLikeDeclaration
6
+ import org.jetbrains.dukat.tsmodel.ConstructSignatureDeclaration
7
+ import org.jetbrains.dukat.tsmodel.HeritageClauseDeclaration
8
+ import org.jetbrains.dukat.tsmodel.InterfaceDeclaration
9
+ import org.jetbrains.dukat.tsmodel.ModuleDeclaration
10
+ import org.jetbrains.dukat.tsmodel.ParameterOwnerDeclaration
11
+ import org.jetbrains.dukat.tsmodel.SourceSetDeclaration
12
+ import org.jetbrains.dukat.tsmodel.VariableDeclaration
13
+ import org.jetbrains.dukat.tsmodel.types.ParameterValueDeclaration
14
+ import org.jetbrains.dukat.tsmodel.types.TypeDeclaration
15
+
16
+ private class ReplaceTypesLowering (private val typesToReplace : Map <String , ParameterValueDeclaration >, private val depth : Int ) : DeclarationLowering {
17
+ private val newTypesToReplace = mutableMapOf<String , ParameterValueDeclaration >()
18
+
19
+ private fun findNewType (type : ParameterValueDeclaration ): ParameterValueDeclaration ? {
20
+ if (type is TypeDeclaration && type.typeReference != null ) {
21
+ val uid = type.typeReference!! .uid
22
+ if (typesToReplace.containsKey(uid)) {
23
+ return typesToReplace.getValue(uid)
24
+ }
25
+ }
26
+ return null
27
+ }
28
+
29
+ override fun lowerParameterValue (
30
+ declaration : ParameterValueDeclaration ,
31
+ owner : NodeOwner <ParameterOwnerDeclaration >?
32
+ ): ParameterValueDeclaration {
33
+ return findNewType(declaration) ? : declaration
34
+ }
35
+
36
+ override fun lowerHeritageClause (
37
+ heritageClause : HeritageClauseDeclaration ,
38
+ owner : NodeOwner <ClassLikeDeclaration >?
39
+ ): HeritageClauseDeclaration {
40
+ val reference = heritageClause.typeReference
41
+ if (reference != null && typesToReplace.containsKey(reference.uid)) {
42
+ val newType = typesToReplace.getValue(reference.uid)
43
+ if (newType is TypeDeclaration ) {
44
+ return heritageClause.copy(name = newType.value, typeArguments = newType.params, typeReference = newType.typeReference)
45
+ }
46
+ }
47
+ return heritageClause
48
+ }
49
+
50
+ override fun lowerVariableDeclaration (declaration : VariableDeclaration , owner : NodeOwner <ModuleDeclaration >? ): VariableDeclaration {
51
+ val newType = findNewType(declaration.type)
52
+ if (newType != null ) {
53
+ newTypesToReplace[declaration.uid] = newType
54
+ return declaration.copy(type = newType)
55
+ }
56
+ return declaration
57
+ }
58
+
59
+ override fun lower (source : SourceSetDeclaration ): SourceSetDeclaration {
60
+ val newSource = super .lower(source)
61
+ return if (depth == 0 ) {
62
+ ReplaceTypesLowering (newTypesToReplace, depth + 1 ).lower(newSource)
63
+ } else {
64
+ newSource
65
+ }
66
+ }
67
+ }
68
+
69
+ private class ProcessConstructorInterfacesLowering : DeclarationLowering {
70
+
71
+ private val typesToReplace = mutableMapOf<String , ParameterValueDeclaration >()
72
+
73
+ private fun determineCommonReturnType (constructors : List <ConstructSignatureDeclaration >): ParameterValueDeclaration ? {
74
+ return constructors[0 ].type
75
+ }
76
+
77
+ override fun lowerInterfaceDeclaration (
78
+ declaration : InterfaceDeclaration ,
79
+ owner : NodeOwner <ModuleDeclaration >?
80
+ ): InterfaceDeclaration {
81
+ val constructSignatures = declaration.members.filterIsInstance<ConstructSignatureDeclaration >()
82
+ if (constructSignatures.isEmpty()) {
83
+ return declaration
84
+ }
85
+ val commonReturnType = determineCommonReturnType(constructSignatures)
86
+ if (commonReturnType is TypeDeclaration && commonReturnType.value == IdentifierEntity (" Array" )) {
87
+ typesToReplace[declaration.uid] = commonReturnType
88
+ }
89
+ return declaration
90
+ }
91
+
92
+ override fun lower (source : SourceSetDeclaration ): SourceSetDeclaration {
93
+ val newSource = super .lower(source)
94
+ return ReplaceTypesLowering (typesToReplace, 0 ).lower(newSource)
95
+ }
96
+ }
97
+
98
+ class ProcessConstructorInterfaces : TsLowering {
99
+ override fun lower (source : SourceSetDeclaration ): SourceSetDeclaration {
100
+ return ProcessConstructorInterfacesLowering ().lower(source)
101
+ }
102
+ }
0 commit comments