diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/ClosureTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/ClosureTest.java index 95d99ee09bc..fe880217ae2 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/ClosureTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/ClosureTest.java @@ -72,4 +72,16 @@ public final class ClosureTest extends SingleFileTranslationTest { public void testClosureThisInConstructor() throws Exception { checkFooBoxIsOk(); } + + public void testClosureFunctionAsArgument() throws Exception { + checkFooBoxIsOk(); + } + + public void testClosureLocalFunction() throws Exception { + checkFooBoxIsOk(); + } + + public void testClosureLocalLiteralFunction() throws Exception { + checkFooBoxIsOk(); + } } diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/context/StaticContext.java b/js/js.translator/src/org/jetbrains/k2js/translate/context/StaticContext.java index 75f276a3b77..0667d9604cf 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/context/StaticContext.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/context/StaticContext.java @@ -164,7 +164,6 @@ public final class StaticContext { packageFqName.isRoot() ? null : getQualifierForParentPackage(packageFqName.parent())); } - // TODO: usage tracker @NotNull public JsName getNameForDescriptor(@NotNull DeclarationDescriptor descriptor) { JsName name = names.get(descriptor.getOriginal()); diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/context/TranslationContext.java b/js/js.translator/src/org/jetbrains/k2js/translate/context/TranslationContext.java index a091fdc7728..beed6e17621 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/context/TranslationContext.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/context/TranslationContext.java @@ -171,6 +171,7 @@ public class TranslationContext { @NotNull public JsName getNameForDescriptor(@NotNull DeclarationDescriptor descriptor) { + descriptorUsedInThisContext(descriptor); // TODO drop this return staticContext.getNameForDescriptor(descriptor); } @@ -186,6 +187,7 @@ public class TranslationContext { @NotNull public JsNameRef getQualifiedReference(@NotNull DeclarationDescriptor descriptor) { + descriptorUsedInThisContext(descriptor); // TODO drop this return staticContext.getQualifiedReference(descriptor); } @@ -261,9 +263,7 @@ public class TranslationContext { @Nullable public JsExpression getAliasForDescriptor(@NotNull DeclarationDescriptor descriptor) { - if (usageTracker != null) { - usageTracker.triggerUsed(descriptor); - } + descriptorUsedInThisContext(descriptor); return aliasingContext.getAliasForDescriptor(descriptor); } @@ -278,9 +278,7 @@ public class TranslationContext { effectiveDescriptor = descriptor; } - if (usageTracker != null) { - usageTracker.triggerUsed(effectiveDescriptor); - } + descriptorUsedInThisContext(effectiveDescriptor); JsExpression alias = aliasingContext.getAliasForDescriptor(effectiveDescriptor); return alias == null ? JsLiteral.THIS : alias; @@ -297,4 +295,10 @@ public class TranslationContext { public JsNameRef define(String name, JsExpression expression) { return getDefinitionPlace().define(name, expression); } + + private void descriptorUsedInThisContext(DeclarationDescriptor effectiveDescriptor) { + if (usageTracker != null) { + usageTracker.triggerUsed(effectiveDescriptor); + } + } } diff --git a/js/js.translator/testFiles/closure/cases/closureFunctionAsArgument.kt b/js/js.translator/testFiles/closure/cases/closureFunctionAsArgument.kt new file mode 100644 index 00000000000..ae8794d9730 --- /dev/null +++ b/js/js.translator/testFiles/closure/cases/closureFunctionAsArgument.kt @@ -0,0 +1,11 @@ +package foo + +fun test(f: () -> String): String { + val funLit = { f() } + return funLit() +} + + +fun box(): String { + return test { "OK" } +} \ No newline at end of file diff --git a/js/js.translator/testFiles/closure/cases/closureLocalFunction.kt b/js/js.translator/testFiles/closure/cases/closureLocalFunction.kt new file mode 100644 index 00000000000..945ca4d5255 --- /dev/null +++ b/js/js.translator/testFiles/closure/cases/closureLocalFunction.kt @@ -0,0 +1,12 @@ +package foo + +fun test(): String { + fun f(): String = "OK" + + val funLit = { f() } + return funLit() +} + +fun box(): String { + return test() +} \ No newline at end of file diff --git a/js/js.translator/testFiles/closure/cases/closureLocalLiteralFunction.kt b/js/js.translator/testFiles/closure/cases/closureLocalLiteralFunction.kt new file mode 100644 index 00000000000..e91273e68ec --- /dev/null +++ b/js/js.translator/testFiles/closure/cases/closureLocalLiteralFunction.kt @@ -0,0 +1,14 @@ +package foo + +val k = {"K"} + +fun test(): String { + val o = {"O"} + + val funLit = { o() + k() } + return funLit() +} + +fun box(): String { + return test() +} \ No newline at end of file