From 482060f1968d8be80b962fc1252ea8a64e5861bd Mon Sep 17 00:00:00 2001 From: Shagen Ogandzhanian Date: Thu, 11 Jun 2020 01:10:32 +0200 Subject: [PATCH] Use KLIBROOT prefix in fqName for any entities we resolve as "ours" this fixes https://github.com/Kotlin/dukat/issues/306 --- .../override/copyGeneratedMembers.d.kt | 2 +- .../unionType/literalWithBasicType.d.kt | 23 ++++++++++++++++ .../unionType/literalWithBasicType.d.ts | 9 +++++++ .../compiler/tests/core/CoreSetCliTests.kt | 1 - .../model/commonLowerings/lowerOverrides.kt | 6 ++--- .../dukat/commonLowerings/addImports.kt | 7 ----- .../dukat/stdlib/KotlinStdlibEntities.kt | 2 +- .../dukat/nodeIntroduction/introduceNodes.kt | 2 +- .../nodeLowering/lowerings/introduceModels.kt | 27 ++++++++++--------- 9 files changed, 52 insertions(+), 27 deletions(-) create mode 100644 compiler/test/data/typescript/unionType/literalWithBasicType.d.kt create mode 100644 compiler/test/data/typescript/unionType/literalWithBasicType.d.ts diff --git a/compiler/test/data/typescript/override/copyGeneratedMembers.d.kt b/compiler/test/data/typescript/override/copyGeneratedMembers.d.kt index 9ca8b14ee..b13d15052 100644 --- a/compiler/test/data/typescript/override/copyGeneratedMembers.d.kt +++ b/compiler/test/data/typescript/override/copyGeneratedMembers.d.kt @@ -25,9 +25,9 @@ external interface RS { external open class R : RS { open fun unshift(chunk: Any, encoding: String /* "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" */ = definedExternally) + override fun unshift(chunk: String, encoding: String /* "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" */) override fun shouldNotBeCopied(a: String): Array override fun shouldNotBeCopied(a: Array): Array - override fun unshift(chunk: String, encoding: String /* "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" */) override fun unshift(chunk: Uint8Array, encoding: String /* "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" */) override fun unshift(chunk: Array, encoding: String /* "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" */) } diff --git a/compiler/test/data/typescript/unionType/literalWithBasicType.d.kt b/compiler/test/data/typescript/unionType/literalWithBasicType.d.kt new file mode 100644 index 000000000..67fc8b0a1 --- /dev/null +++ b/compiler/test/data/typescript/unionType/literalWithBasicType.d.kt @@ -0,0 +1,23 @@ +@file:Suppress("INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS", "EXTERNAL_DELEGATION") + +import kotlin.js.* +import kotlin.js.Json +import org.khronos.webgl.* +import org.w3c.dom.* +import org.w3c.dom.events.* +import org.w3c.dom.parsing.* +import org.w3c.dom.svg.* +import org.w3c.dom.url.* +import org.w3c.fetch.* +import org.w3c.files.* +import org.w3c.notifications.* +import org.w3c.performance.* +import org.w3c.workers.* +import org.w3c.xhr.* + +external interface Locale { + fun ping(key: String /* 'sameDay' | 'nextDay' | 'lastDay' | 'nextWeek' | 'lastWeek' | 'sameElse' */ = definedExternally): String + fun pong(key: Number /* 1 | 2 | 3 */ = definedExternally): String + fun ping(): String + fun pong(): String +} \ No newline at end of file diff --git a/compiler/test/data/typescript/unionType/literalWithBasicType.d.ts b/compiler/test/data/typescript/unionType/literalWithBasicType.d.ts new file mode 100644 index 000000000..6f03f53f0 --- /dev/null +++ b/compiler/test/data/typescript/unionType/literalWithBasicType.d.ts @@ -0,0 +1,9 @@ +type AlphaKey = 'sameDay' | 'nextDay' | 'lastDay' | 'nextWeek' | 'lastWeek' | 'sameElse' | string; +type NumKey = 1 | 2 | 3 | number; + +interface Locale { + ping(key?: AlphaKey): string; + pong(key?: NumKey): string; +} + + diff --git a/compiler/test/src/org/jetbrains/dukat/compiler/tests/core/CoreSetCliTests.kt b/compiler/test/src/org/jetbrains/dukat/compiler/tests/core/CoreSetCliTests.kt index 786fa8644..e25f2ee75 100644 --- a/compiler/test/src/org/jetbrains/dukat/compiler/tests/core/CoreSetCliTests.kt +++ b/compiler/test/src/org/jetbrains/dukat/compiler/tests/core/CoreSetCliTests.kt @@ -39,7 +39,6 @@ open class CoreSetCliTests { @MethodSource("coreSet") open fun withValueSource(name: String, tsPath: String, ktPath: String, tsConfig: String) { assertContentEqualsBinary(name, tsPath, ktPath, if (tsConfig.isEmpty()) null else tsConfig) - assertContentEqualsBinary(name, tsPath, ktPath, if (tsConfig.isEmpty()) null else tsConfig) } open fun getTranslator(): CliTranslator = createStandardCliTranslator() diff --git a/model-lowerings-common/src/org/jetbrains/dukat/model/commonLowerings/lowerOverrides.kt b/model-lowerings-common/src/org/jetbrains/dukat/model/commonLowerings/lowerOverrides.kt index 3a10cfb36..98e92cd6a 100644 --- a/model-lowerings-common/src/org/jetbrains/dukat/model/commonLowerings/lowerOverrides.kt +++ b/model-lowerings-common/src/org/jetbrains/dukat/model/commonLowerings/lowerOverrides.kt @@ -3,7 +3,6 @@ package org.jetbrains.dukat.model.commonLowerings import org.jetbrains.dukat.astCommon.IdentifierEntity import org.jetbrains.dukat.astCommon.NameEntity import org.jetbrains.dukat.astCommon.QualifierEntity -import org.jetbrains.dukat.astCommon.appendLeft import org.jetbrains.dukat.astModel.CallableModel import org.jetbrains.dukat.astModel.CallableParameterModel import org.jetbrains.dukat.astModel.ClassLikeModel @@ -16,13 +15,12 @@ import org.jetbrains.dukat.astModel.MethodModel import org.jetbrains.dukat.astModel.ModuleModel import org.jetbrains.dukat.astModel.PropertyModel import org.jetbrains.dukat.astModel.SourceSetModel -import org.jetbrains.dukat.astModel.TopLevelModel import org.jetbrains.dukat.astModel.TypeModel import org.jetbrains.dukat.astModel.TypeParameterModel import org.jetbrains.dukat.astModel.TypeParameterReferenceModel import org.jetbrains.dukat.astModel.TypeValueModel import org.jetbrains.dukat.astModel.modifiers.InheritanceModifierModel -import org.jetbrains.dukat.stdlib.TSLIBROOT +import org.jetbrains.dukat.stdlib.KLIBROOT private fun TypeModel.isAny(): Boolean { return this is TypeValueModel && value == IdentifierEntity("Any") @@ -242,7 +240,7 @@ private class ClassLikeOverrideResolver(private val context: ModelContext, priva private fun NameEntity.withLibPrefix(): NameEntity { return when (this) { - is IdentifierEntity -> QualifierEntity(TSLIBROOT, this) + is IdentifierEntity -> QualifierEntity(KLIBROOT, this) is QualifierEntity -> this } } diff --git a/model-lowerings/src/org/jetbrains/dukat/commonLowerings/addImports.kt b/model-lowerings/src/org/jetbrains/dukat/commonLowerings/addImports.kt index ed9e91fbf..33cc63ff3 100644 --- a/model-lowerings/src/org/jetbrains/dukat/commonLowerings/addImports.kt +++ b/model-lowerings/src/org/jetbrains/dukat/commonLowerings/addImports.kt @@ -72,13 +72,6 @@ private class NameVisitor(private val name: NameEntity, private val importContex } override fun lowerIdentifierExpressionModel(ownerContext: NodeOwner): IdentifierExpressionModel { - - // TODO: enable it when we figure out correct approach to star imports - - /*val node = ownerContext.node - - return getNewName(node.identifier)?.let { node.copy(identifier = it) } ?: node*/ - return ownerContext.node } diff --git a/stdlib/src/org/jetbrains/dukat/stdlib/KotlinStdlibEntities.kt b/stdlib/src/org/jetbrains/dukat/stdlib/KotlinStdlibEntities.kt index b3c5c7d08..0dc2e9867 100644 --- a/stdlib/src/org/jetbrains/dukat/stdlib/KotlinStdlibEntities.kt +++ b/stdlib/src/org/jetbrains/dukat/stdlib/KotlinStdlibEntities.kt @@ -553,6 +553,6 @@ val KotlinBuiltInEntities = setOf( fun isStdLibEntity(fqName: NameEntity): Boolean { val leftMost = fqName.leftMost() - val isLib = leftMost == IdentifierEntity("") || fqName.isTsStdlibPrefixed() + val isLib = leftMost == IdentifierEntity("") || fqName.isTsStdlibPrefixed() || fqName.isKotlinStdlibPrefixed() return isLib && KotlinStdlibEntities.contains(fqName.rightMost()) } diff --git a/typescript/ts-node-introduction/src/org/jetbrains/dukat/nodeIntroduction/introduceNodes.kt b/typescript/ts-node-introduction/src/org/jetbrains/dukat/nodeIntroduction/introduceNodes.kt index 178d7a734..2ab67b415 100644 --- a/typescript/ts-node-introduction/src/org/jetbrains/dukat/nodeIntroduction/introduceNodes.kt +++ b/typescript/ts-node-introduction/src/org/jetbrains/dukat/nodeIntroduction/introduceNodes.kt @@ -360,7 +360,7 @@ private class LowerDeclarationsToNodes( ), MethodNode( "set", - convertParameters(declaration.indexTypes.toMutableList() + listOf(ParameterDeclaration("value", declaration.returnType.convertToNodeNullable() ?: declaration.returnType, null, false, false))), + convertParameters(declaration.indexTypes + listOf(ParameterDeclaration("value", declaration.returnType.convertToNodeNullable() ?: declaration.returnType, null, false, false))), TypeValueNode(IdentifierEntity("Unit"), emptyList()), emptyList(), false, diff --git a/typescript/ts-node-lowering/src/org/jetrbains/dukat/nodeLowering/lowerings/introduceModels.kt b/typescript/ts-node-lowering/src/org/jetrbains/dukat/nodeLowering/lowerings/introduceModels.kt index 1297bf029..1dd08206a 100644 --- a/typescript/ts-node-lowering/src/org/jetrbains/dukat/nodeLowering/lowerings/introduceModels.kt +++ b/typescript/ts-node-lowering/src/org/jetrbains/dukat/nodeLowering/lowerings/introduceModels.kt @@ -84,6 +84,7 @@ import org.jetbrains.dukat.astModel.statements.ReturnStatementModel import org.jetbrains.dukat.astModel.statements.StatementModel import org.jetbrains.dukat.logger.Logging import org.jetbrains.dukat.panic.raiseConcern +import org.jetbrains.dukat.stdlib.KLIBROOT import org.jetbrains.dukat.stdlib.KotlinStdlibEntities import org.jetbrains.dukat.stdlib.TSLIBROOT import org.jetbrains.dukat.translatorString.translate @@ -166,7 +167,7 @@ internal class DocumentConverter(private val moduleNode: ModuleNode, private val private fun TypeValueNode.getFqName(): NameEntity? { return typeReference?.getFqName() ?: if (KotlinStdlibEntities.contains(value)) { - TSLIBROOT.appendLeft(value) + KLIBROOT.appendLeft(value) } else null } @@ -216,16 +217,19 @@ internal class DocumentConverter(private val moduleNode: ModuleNode, private val fun TypeNode.process(context: TranslationContext = TranslationContext.IRRELEVANT): TypeModel { val dynamicName = IdentifierEntity("dynamic") return when (this) { - is LiteralUnionNode -> TypeValueModel( - when (kind) { - UnionLiteralKind.NUMBER -> IdentifierEntity("Number") - else -> IdentifierEntity("String") - }, - emptyList(), - params.joinToString(" | "), - null, - context == TranslationContext.PROPERTY(true) - ) + is LiteralUnionNode -> { + val value = when (kind) { + UnionLiteralKind.NUMBER -> IdentifierEntity("Number") + else -> IdentifierEntity("String") + } + TypeValueModel( + value, + emptyList(), + params.joinToString(" | "), + KLIBROOT.appendLeft(value), + context == TranslationContext.PROPERTY(true) + ) + } is UnionTypeNode -> TypeValueModel( dynamicName, emptyList(), @@ -684,7 +688,6 @@ internal class DocumentConverter(private val moduleNode: ModuleNode, private val } private fun MethodNode.process(override: NameEntity? = null): MethodModel { - // TODO: how ClassModel end up here? return MethodModel( name = IdentifierEntity(name), parameters = parameters.map { param -> param.process() },