Skip to content

Commit

Permalink
Remove Any from the inheritance chain
Browse files Browse the repository at this point in the history
this fixes #323
  • Loading branch information
Schahen committed Jul 3, 2020
1 parent d587f0f commit 8085959
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
4 changes: 3 additions & 1 deletion compiler/test/data/typescript/Object.d.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ import org.w3c.xhr.*

external var a: Any

external fun foo(o: Any): Any
external fun foo(o: Any): Any

external interface SomeClass
4 changes: 3 additions & 1 deletion compiler/test/data/typescript/Object.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
declare var a: Object;

declare function foo(o: Object): Object
declare function foo(o: Object): Object

interface SomeClass extends Object {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.jetbrains.dukat.commonLowerings

import org.jetbrains.dukat.astCommon.IdentifierEntity
import org.jetbrains.dukat.astCommon.QualifierEntity
import org.jetbrains.dukat.astModel.ClassLikeModel
import org.jetbrains.dukat.astModel.ClassModel
import org.jetbrains.dukat.astModel.HeritageModel
import org.jetbrains.dukat.astModel.InterfaceModel
import org.jetbrains.dukat.astModel.ModuleModel
import org.jetbrains.dukat.astModel.TypeValueModel
import org.jetbrains.dukat.model.commonLowerings.ModelLowering
import org.jetbrains.dukat.model.commonLowerings.TopLevelModelLowering
import org.jetbrains.dukat.ownerContext.NodeOwner
import org.jetbrains.dukat.stdlib.TSLIBROOT

private fun TypeValueModel.isAny() = fqName == QualifierEntity(TSLIBROOT, IdentifierEntity("Any"))

private fun ClassLikeModel.removeParentAny(): List<HeritageModel> {
return parentEntities.filter { !it.value.isAny() }
}

private class RemoveParentAnyLowering : TopLevelModelLowering {
override fun lowerClassModel(ownerContext: NodeOwner<ClassModel>, parentModule: ModuleModel): ClassModel? {
val node = ownerContext.node
return super.lowerClassModel(ownerContext.copy(node = node.copy(parentEntities = node.removeParentAny())), parentModule)
}

override fun lowerInterfaceModel(ownerContext: NodeOwner<InterfaceModel>, parentModule: ModuleModel): InterfaceModel? {
val node = ownerContext.node
return super.lowerInterfaceModel(ownerContext.copy(node = node.copy(parentEntities = node.removeParentAny())), parentModule)
}
}

class RemoveParentAny : ModelLowering {
override fun lower(module: ModuleModel): ModuleModel {
return RemoveParentAnyLowering().lowerRoot(module, NodeOwner(module, null))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ private enum class SubstitutedEntities(val value: IdentifierEntity) {
EXCLUDE(IdentifierEntity("Exclude")),
REQUIRED(IdentifierEntity("Required")),
READONLY_ARRAY(IdentifierEntity("ReadonlyArray")),
TEMPLATE_STRINGS_ARRAY(IdentifierEntity("TemplateStringsArray"))
TEMPLATE_STRINGS_ARRAY(IdentifierEntity("TemplateStringsArray")),
OBJECT(IdentifierEntity("Object"))
}

private fun TypeValueModel.resolveAsSubstitution(): TypeValueModel? {
val isLibReference = isLibReference()

return if (value == SubstitutedEntities.NON_NULLABLE.value || value == SubstitutedEntities.EXCLUDE.value || value == SubstitutedEntities.REQUIRED.value) {
return if (value == SubstitutedEntities.NON_NULLABLE.value || value == SubstitutedEntities.EXCLUDE.value || value == SubstitutedEntities.REQUIRED.value || value == SubstitutedEntities.OBJECT.value) {
if (isLibReference) {
createStdType("Any")
} else null
Expand Down Expand Up @@ -173,7 +174,7 @@ private class SubstituteLowering : ModelWithOwnerTypeLowering {
}
}

class SubstituteTsStdLibEntities() : ModelLowering {
class SubstituteTsStdLibEntities : ModelLowering {
override fun lower(module: ModuleModel): ModuleModel {
return SubstituteLowering().lowerRoot(module, NodeOwner(module, null))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ private fun mapPrimitiveValue(value: String): String {
"boolean" -> "Boolean"
"string" -> "String"
"number" -> "Number"
"Object" -> "Any"
else -> value
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.jetbrains.dukat.commonLowerings.AddExplicitGettersAndSetters
import org.jetbrains.dukat.commonLowerings.AddImports
import org.jetbrains.dukat.commonLowerings.AnyfyUnresolvedTypes
import org.jetbrains.dukat.commonLowerings.RemoveDuplicateMembers
import org.jetbrains.dukat.commonLowerings.RemoveParentAny
import org.jetbrains.dukat.commonLowerings.RemoveUnsupportedJsNames
import org.jetbrains.dukat.commonLowerings.SeparateNonExternalEntities
import org.jetbrains.dukat.commonLowerings.SubstituteTsStdLibEntities
Expand Down Expand Up @@ -99,6 +100,7 @@ open class TypescriptLowerer(
RemoveRedundantTypeParams(),
RemoveConflictingOverloads(),
SubstituteTsStdLibEntities(),
RemoveParentAny(),
EscapeIdentificators(),
RemoveUnsupportedJsNames(),
MergeClassLikesAndModuleDeclarations(),
Expand Down

0 comments on commit 8085959

Please sign in to comment.