From 14e4febb0567015dcc0f05e1db05931559847b95 Mon Sep 17 00:00:00 2001 From: Artem Kobzar Date: Mon, 24 Oct 2022 10:12:30 +0000 Subject: [PATCH] [K/JS] Exclude `default` from the regular reserved keywords to give ability to export declarations with default export ^KT-54480 Fixed fix(KT-54480): exclude default from the regular reserved keywords to give ability to export intities with default export. Merge-request: KT-MR-7459 Merged-by: Artem Kobzar --- .../testsWithJsStdLib/export/nonConsumableIdentifiers.kt | 4 ++++ .../testsWithJsStdLib/export/nonConsumableIdentifiers.txt | 7 +++++++ .../src/org/jetbrains/kotlin/js/common/IdentifierPolicy.kt | 6 ++++-- .../js/resolve/diagnostics/JsExportDeclarationChecker.kt | 3 ++- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/export/nonConsumableIdentifiers.kt b/compiler/testData/diagnostics/testsWithJsStdLib/export/nonConsumableIdentifiers.kt index cf8f37399d5..7a5499e4117 100644 --- a/compiler/testData/diagnostics/testsWithJsStdLib/export/nonConsumableIdentifiers.kt +++ b/compiler/testData/diagnostics/testsWithJsStdLib/export/nonConsumableIdentifiers.kt @@ -24,6 +24,10 @@ val bar = 4 @JsName("super") class Baz +@JsExport +@JsName("default") +class DefDef + @JsExport class Test { fun instanceof() {} diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/export/nonConsumableIdentifiers.txt b/compiler/testData/diagnostics/testsWithJsStdLib/export/nonConsumableIdentifiers.txt index ca65c76b220..599a21f9ab2 100644 --- a/compiler/testData/diagnostics/testsWithJsStdLib/export/nonConsumableIdentifiers.txt +++ b/compiler/testData/diagnostics/testsWithJsStdLib/export/nonConsumableIdentifiers.txt @@ -13,6 +13,13 @@ package foo { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } + @kotlin.js.JsExport @kotlin.js.JsName(name = "default") public final class DefDef { + public constructor DefDef() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + @kotlin.js.JsExport public object NaN { private constructor NaN() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean diff --git a/js/js.ast/src/org/jetbrains/kotlin/js/common/IdentifierPolicy.kt b/js/js.ast/src/org/jetbrains/kotlin/js/common/IdentifierPolicy.kt index f16afd30b89..f2e2ed2d990 100644 --- a/js/js.ast/src/org/jetbrains/kotlin/js/common/IdentifierPolicy.kt +++ b/js/js.ast/src/org/jetbrains/kotlin/js/common/IdentifierPolicy.kt @@ -51,9 +51,11 @@ fun String.isValidES5Identifier(): Boolean { return true } -val RESERVED_KEYWORDS: Set = setOf( +val SPECIAL_KEYWORDS: Set = setOf("default") + +val RESERVED_KEYWORDS: Set = SPECIAL_KEYWORDS + setOf( // keywords - "await", "break", "case", "catch", "continue", "debugger", "default", "delete", "do", "else", "finally", "for", "function", "if", + "await", "break", "case", "catch", "continue", "debugger", "delete", "do", "else", "finally", "for", "function", "if", "in", "instanceof", "new", "return", "switch", "this", "throw", "try", "typeof", "var", "void", "while", "with", // future reserved words diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsExportDeclarationChecker.kt b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsExportDeclarationChecker.kt index 06d957b0d36..188208e46ce 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsExportDeclarationChecker.kt +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsExportDeclarationChecker.kt @@ -11,6 +11,7 @@ import org.jetbrains.kotlin.builtins.isFunctionType import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.ClassKind.* import org.jetbrains.kotlin.js.common.RESERVED_KEYWORDS +import org.jetbrains.kotlin.js.common.SPECIAL_KEYWORDS import org.jetbrains.kotlin.js.naming.NameSuggestion import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils import org.jetbrains.kotlin.psi.KtAnnotationEntry @@ -212,7 +213,7 @@ object JsExportDeclarationChecker : DeclarationChecker { val name = declarationDescriptor.getKotlinOrJsName() - if (name !in RESERVED_KEYWORDS && NameSuggestion.sanitizeName(name) == name) return + if (name in SPECIAL_KEYWORDS || (name !in RESERVED_KEYWORDS && NameSuggestion.sanitizeName(name) == name)) return val reportTarget = declarationDescriptor.getJsNameArgument() ?: declaration.getIdentifier()