Avoid duplication of imports introduced during JS inlining

This commit is contained in:
Alexey Andreev
2017-07-14 16:46:04 +03:00
parent f2b2e20331
commit 8c256b24dc
19 changed files with 374 additions and 76 deletions
@@ -4838,6 +4838,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
doTest(fileName);
}
@TestMetadata("repeatedImport.kt")
public void testRepeatedImport() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/inlineMultiModule/repeatedImport.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/inlineMultiModule/simple.kt");
@@ -83,6 +83,13 @@ public class DirectiveTestUtils {
}
};
private static final DirectiveHandler PROPERTY_READ_COUNT = new DirectiveHandler("PROPERTY_READ_COUNT") {
@Override
void processEntry(@NotNull JsNode ast, @NotNull ArgumentsHelper arguments) throws Exception {
checkPropertyReadCount(ast, arguments.getNamedArgument("name"), Integer.parseInt(arguments.getNamedArgument("count")));
}
};
private static final DirectiveHandler FUNCTION_CALLED_IN_SCOPE = new DirectiveHandler("CHECK_CALLED_IN_SCOPE") {
@Override
void processEntry(@NotNull JsNode ast, @NotNull ArgumentsHelper arguments) throws Exception {
@@ -299,6 +306,7 @@ public class DirectiveTestUtils {
PROPERTY_NOT_USED,
PROPERTY_NOT_READ_FROM,
PROPERTY_NOT_WRITTEN_TO,
PROPERTY_READ_COUNT,
PROPERTY_WRITE_COUNT,
FUNCTION_CALLED_IN_SCOPE,
FUNCTION_NOT_CALLED_IN_SCOPE,
@@ -340,12 +348,16 @@ public class DirectiveTestUtils {
}
}
private static void checkPropertyReadCount(JsNode node, String propertyName, int expectedCount) throws Exception {
PropertyReferenceCollector counter = PropertyReferenceCollector.Companion.collect(node);
assertEquals("Property read count: " + propertyName, expectedCount, counter.unqualifiedReadCount(propertyName));
}
private static void checkPropertyWriteCount(JsNode node, String propertyName, int expectedCount) throws Exception {
PropertyReferenceCollector counter = PropertyReferenceCollector.Companion.collect(node);
assertEquals("Property write count: " + propertyName, expectedCount, counter.unqualifiedWriteCount(propertyName));
}
public static void checkFunctionNotCalled(@NotNull JsNode node, @NotNull String functionName, @Nullable String exceptFunction)
throws Exception {
Set<String> excludedScopes = exceptFunction != null ? Collections.singleton(exceptFunction) : Collections.emptySet();
@@ -24,17 +24,19 @@ import org.jetbrains.kotlin.js.translate.utils.JsAstUtils
class PropertyReferenceCollector : RecursiveJsVisitor() {
private val identReadSet = hashSetOf<String>()
private val identReadMap = hashMapOf<String, Int>()
private val identWriteMap = hashMapOf<String, Int>()
fun hasUnqualifiedReads(expectedIdent: String) = expectedIdent in identReadSet
fun hasUnqualifiedReads(expectedIdent: String) = expectedIdent in identReadMap
fun hasUnqualifiedWrites(expectedIdent: String) = expectedIdent in identWriteMap
fun unqualifiedWriteCount(expectedIdent: String): Int = identWriteMap[expectedIdent] ?: 0
fun unqualifiedReadCount(expectedIdent: String): Int = identReadMap[expectedIdent] ?: 0
override fun visitNameRef(nameRef: JsNameRef) {
super.visitNameRef(nameRef)
identReadSet.add(nameRef.ident)
identReadMap[nameRef.ident] = 1 + unqualifiedReadCount(nameRef.ident)
}
override fun visitBinaryExpression(x: JsBinaryOperation) {