diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/module/nestedProhibited.kt b/compiler/testData/diagnostics/testsWithJsStdLib/module/nestedProhibited.kt index 501471682a1..7e1a8ebb0b5 100644 --- a/compiler/testData/diagnostics/testsWithJsStdLib/module/nestedProhibited.kt +++ b/compiler/testData/diagnostics/testsWithJsStdLib/module/nestedProhibited.kt @@ -2,7 +2,9 @@ package foo @JsModule("A") -external class A +external class A { + class Nested +} @JsModule("B") external object B diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/module/nestedProhibited.txt b/compiler/testData/diagnostics/testsWithJsStdLib/module/nestedProhibited.txt index 11dee24c214..262752e9ba9 100644 --- a/compiler/testData/diagnostics/testsWithJsStdLib/module/nestedProhibited.txt +++ b/compiler/testData/diagnostics/testsWithJsStdLib/module/nestedProhibited.txt @@ -10,6 +10,13 @@ package foo { 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 + + public final class Nested { + public constructor Nested() + 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.JsModule(import = "B") public external object B { diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToModule.kt b/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToModule.kt index fc7ae91932c..2755a055c97 100644 --- a/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToModule.kt +++ b/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToModule.kt @@ -14,6 +14,8 @@ external object A { @JsModule("B") external open class B { fun foo(): Int + + class Nested } @JsModule("bar") @@ -37,6 +39,8 @@ fun box() { B() bar() baz() + + B.Nested() } external class DerivedB : B \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToModule.txt b/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToModule.txt index 9db3570a9f2..a776193f8de 100644 --- a/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToModule.txt +++ b/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToModule.txt @@ -19,6 +19,13 @@ package foo { public final fun foo(): kotlin.Int public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + public final class Nested { + public constructor Nested() + 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 + } } } diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToNonModule.kt b/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToNonModule.kt index 355c5f32598..4a3e7f3aeb4 100644 --- a/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToNonModule.kt +++ b/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToNonModule.kt @@ -14,6 +14,8 @@ external object A { @JsNonModule external open class B { fun foo(): Int + + class Nested } @JsNonModule @@ -31,6 +33,7 @@ fun box() { A.f()+A.g B() bar() + B.Nested() } external class DerivedB : B \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToNonModule.txt b/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToNonModule.txt index 697330909bc..0c451fcdb39 100644 --- a/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToNonModule.txt +++ b/compiler/testData/diagnostics/testsWithJsStdLib/module/wrongCallToNonModule.txt @@ -30,5 +30,12 @@ package foo { public final fun foo(): kotlin.Int public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + public final class Nested { + public constructor Nested() + 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 + } } } diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsModuleChecker.kt b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsModuleChecker.kt index de39a0025b1..1daa1106919 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsModuleChecker.kt +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsModuleChecker.kt @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.checkers.SimpleDeclarationChecker import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassNotAny @@ -45,10 +46,12 @@ object JsModuleChecker : SimpleDeclarationChecker { diagnosticHolder.report(ErrorsJs.JS_MODULE_PROHIBITED_ON_NON_NATIVE.on(declaration)) } - val isFileModuleOrNonModule = AnnotationsUtils.getFileModuleName(bindingContext, descriptor) != null || - AnnotationsUtils.isFromNonModuleFile(bindingContext, descriptor) - if (isFileModuleOrNonModule) { - diagnosticHolder.report(ErrorsJs.NESTED_JS_MODULE_PROHIBITED.on(declaration)) + if (DescriptorUtils.isTopLevelDeclaration(descriptor)) { + val isFileModuleOrNonModule = AnnotationsUtils.getFileModuleName(bindingContext, descriptor) != null || + AnnotationsUtils.isFromNonModuleFile(bindingContext, descriptor) + if (isFileModuleOrNonModule) { + diagnosticHolder.report(ErrorsJs.NESTED_JS_MODULE_PROHIBITED.on(declaration)) + } } } diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java index 31bdeeca544..27bb5840ef5 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java @@ -279,7 +279,7 @@ public final class StaticContext { expression = getQualifiedExpression(suggested.getScope()); } - if (isNativeObject(suggested.getDescriptor())) { + if (isNativeObject(suggested.getDescriptor()) && DescriptorUtils.isTopLevelDeclaration(suggested.getDescriptor())) { String fileModuleName = AnnotationsUtils.getFileModuleName(getBindingContext(), suggested.getDescriptor()); if (fileModuleName != null) { JsName moduleJsName = getImportedModule(fileModuleName, null).internalName; diff --git a/js/js.translator/testData/box/jsModule/externalPackage.js b/js/js.translator/testData/box/jsModule/externalPackage.js index f166d9d531e..369c328b641 100644 --- a/js/js.translator/testData/box/jsModule/externalPackage.js +++ b/js/js.translator/testData/box/jsModule/externalPackage.js @@ -6,6 +6,11 @@ define("lib", [], function() { return this.x + y; }; + function Nested() { + this.y = 55; + } + A.Nested = Nested; + B = { x: 123, foo: function(y) { diff --git a/js/js.translator/testData/box/jsModule/externalPackage.kt b/js/js.translator/testData/box/jsModule/externalPackage.kt index af67305206e..02b7f95a2e0 100644 --- a/js/js.translator/testData/box/jsModule/externalPackage.kt +++ b/js/js.translator/testData/box/jsModule/externalPackage.kt @@ -2,10 +2,14 @@ @file:JsModule("lib") package foo -external class A(@native x: Int = noImpl) { +external class A(x: Int) { val x: Int fun foo(y: Int): Int = noImpl + + class Nested { + val y: Int + } } external object B { @@ -25,6 +29,9 @@ fun box(): String { assertEquals(23, a.x) assertEquals(65, a.foo(42)) + val nested = A.Nested() + assertEquals(55, nested.y) + assertEquals(123, B.x) assertEquals(265, B.foo(142)) diff --git a/js/js.translator/testData/box/jsModule/externalPackagePlain.js b/js/js.translator/testData/box/jsModule/externalPackagePlain.js index 4437093dbac..4abca3beafb 100644 --- a/js/js.translator/testData/box/jsModule/externalPackagePlain.js +++ b/js/js.translator/testData/box/jsModule/externalPackagePlain.js @@ -6,6 +6,11 @@ var lib = function() { return this.x + y; }; + function Nested() { + this.y = 55; + } + A.Nested = Nested; + B = { x: 123, foo: function(y) { diff --git a/js/js.translator/testData/box/jsModule/externalPackagePlain.kt b/js/js.translator/testData/box/jsModule/externalPackagePlain.kt index c1480c5d48c..6203f715cfe 100644 --- a/js/js.translator/testData/box/jsModule/externalPackagePlain.kt +++ b/js/js.translator/testData/box/jsModule/externalPackagePlain.kt @@ -6,6 +6,10 @@ external class A(x: Int = noImpl) { val x: Int fun foo(y: Int): Int = noImpl + + class Nested { + val y: Int + } } external object B { @@ -25,6 +29,9 @@ fun box(): String { assertEquals(23, a.x) assertEquals(65, a.foo(42)) + val nested = A.Nested() + assertEquals(55, nested.y) + assertEquals(123, B.x) assertEquals(265, B.foo(142))