JS: prevent compiler from importing module more than once if not necessary. See KT-15260
This commit is contained in:
committed by
Alexey Andreev
parent
1ecd957981
commit
9b4b7960d3
@@ -5083,6 +5083,18 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/jsModule/externalProperty.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/jsModule/externalProperty.kt");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("importCountCommonJS.kt")
|
||||||
|
public void testImportCountCommonJS() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/jsModule/importCountCommonJS.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("importCountUmd.kt")
|
||||||
|
public void testImportCountUmd() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/jsModule/importCountUmd.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("js/js.translator/testData/box/jsName")
|
@TestMetadata("js/js.translator/testData/box/jsName")
|
||||||
|
|||||||
@@ -92,6 +92,17 @@ public class DirectiveTestUtils {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static final DirectiveHandler FUNCTION_CALLED_TIMES = new DirectiveHandler("FUNCTION_CALLED_TIMES") {
|
||||||
|
@Override
|
||||||
|
void processEntry(@NotNull JsNode ast, @NotNull ArgumentsHelper arguments) throws Exception {
|
||||||
|
int expectedCount = Integer.parseInt(arguments.getNamedArgument("count"));
|
||||||
|
String functionName = arguments.getFirst();
|
||||||
|
CallCounter counter = CallCounter.countCalls(ast);
|
||||||
|
int actualCount = counter.getUnqualifiedCallsCount(functionName);
|
||||||
|
assertEquals("Function " + functionName, expectedCount, actualCount);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private static boolean parseBooleanArgument(@NotNull ArgumentsHelper arguments, @NotNull String name, boolean defaultValue) {
|
private static boolean parseBooleanArgument(@NotNull ArgumentsHelper arguments, @NotNull String name, boolean defaultValue) {
|
||||||
String value = arguments.findNamedArgument(name);
|
String value = arguments.findNamedArgument(name);
|
||||||
return value != null ? Boolean.parseBoolean(value) : defaultValue;
|
return value != null ? Boolean.parseBoolean(value) : defaultValue;
|
||||||
@@ -253,6 +264,7 @@ public class DirectiveTestUtils {
|
|||||||
private static final List<DirectiveHandler> DIRECTIVE_HANDLERS = Arrays.asList(
|
private static final List<DirectiveHandler> DIRECTIVE_HANDLERS = Arrays.asList(
|
||||||
FUNCTION_CONTAINS_NO_CALLS,
|
FUNCTION_CONTAINS_NO_CALLS,
|
||||||
FUNCTION_NOT_CALLED,
|
FUNCTION_NOT_CALLED,
|
||||||
|
FUNCTION_CALLED_TIMES,
|
||||||
PROPERTY_NOT_USED,
|
PROPERTY_NOT_USED,
|
||||||
PROPERTY_NOT_READ_FROM,
|
PROPERTY_NOT_READ_FROM,
|
||||||
PROPERTY_NOT_WRITTEN_TO,
|
PROPERTY_NOT_WRITTEN_TO,
|
||||||
|
|||||||
@@ -661,12 +661,13 @@ public final class StaticContext {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private ImportedModule getImportedModule(@NotNull String baseName, @Nullable DeclarationDescriptor descriptor) {
|
private ImportedModule getImportedModule(@NotNull String baseName, @Nullable DeclarationDescriptor descriptor) {
|
||||||
ImportedModuleKey key = new ImportedModuleKey(baseName, descriptor);
|
JsName plainName = descriptor != null && config.getModuleKind() == ModuleKind.UMD ?
|
||||||
|
rootScope.declareName(getPlainId(descriptor)) : null;
|
||||||
|
ImportedModuleKey key = new ImportedModuleKey(baseName, plainName);
|
||||||
|
|
||||||
ImportedModule module = importedModules.get(key);
|
ImportedModule module = importedModules.get(key);
|
||||||
if (module == null) {
|
if (module == null) {
|
||||||
JsName internalName = rootScope.declareTemporaryName(Namer.LOCAL_MODULE_PREFIX + Namer.suggestedModuleName(baseName));
|
JsName internalName = rootScope.declareTemporaryName(Namer.LOCAL_MODULE_PREFIX + Namer.suggestedModuleName(baseName));
|
||||||
JsName plainName = descriptor != null ? rootScope.declareName(getPlainId(descriptor)) : null;
|
|
||||||
module = new ImportedModule(baseName, internalName, plainName != null ? pureFqn(plainName, null) : null);
|
module = new ImportedModule(baseName, internalName, plainName != null ? pureFqn(plainName, null) : null);
|
||||||
importedModules.put(key, module);
|
importedModules.put(key, module);
|
||||||
}
|
}
|
||||||
@@ -835,11 +836,11 @@ public final class StaticContext {
|
|||||||
private final String baseName;
|
private final String baseName;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private final DeclarationDescriptor declaration;
|
private final JsName plainName;
|
||||||
|
|
||||||
public ImportedModuleKey(@NotNull String baseName, @Nullable DeclarationDescriptor declaration) {
|
public ImportedModuleKey(@NotNull String baseName, @Nullable JsName plainName) {
|
||||||
this.baseName = baseName;
|
this.baseName = baseName;
|
||||||
this.declaration = declaration;
|
this.plainName = plainName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -850,7 +851,7 @@ public final class StaticContext {
|
|||||||
ImportedModuleKey key = (ImportedModuleKey) o;
|
ImportedModuleKey key = (ImportedModuleKey) o;
|
||||||
|
|
||||||
if (!baseName.equals(key.baseName)) return false;
|
if (!baseName.equals(key.baseName)) return false;
|
||||||
if (declaration != null ? !declaration.equals(key.declaration) : key.declaration != null) return false;
|
if (plainName != null ? !plainName.equals(key.plainName) : key.plainName != null) return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -858,7 +859,7 @@ public final class StaticContext {
|
|||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int result = baseName.hashCode();
|
int result = baseName.hashCode();
|
||||||
result = 31 * result + (declaration != null ? declaration.hashCode() : 0);
|
result = 31 * result + (plainName != null ? plainName.hashCode() : 0);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
__beginModule__();
|
||||||
|
module.exports = function(param) {
|
||||||
|
switch (typeof param) {
|
||||||
|
case "number":
|
||||||
|
return "a";
|
||||||
|
case "string":
|
||||||
|
return "b";
|
||||||
|
default:
|
||||||
|
return "c";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
__endModule__("lib");
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
// MODULE_KIND: COMMON_JS
|
||||||
|
// FUNCTION_CALLED_TIMES: require count=2
|
||||||
|
|
||||||
|
@JsModule("lib")
|
||||||
|
external fun f(x: Int): String
|
||||||
|
|
||||||
|
@JsModule("lib")
|
||||||
|
external fun f(x: String): String
|
||||||
|
|
||||||
|
@JsModule("lib")
|
||||||
|
external fun g(x: Boolean): String
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
val result = f(23) + f("foo") + g(true)
|
||||||
|
if (result != "abc") return "fail: $result"
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
var f = function(param) {
|
||||||
|
switch (typeof param) {
|
||||||
|
case "number":
|
||||||
|
return "a";
|
||||||
|
case "string":
|
||||||
|
return "b";
|
||||||
|
default:
|
||||||
|
return "c";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var g = f;
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
// MODULE_KIND: UMD
|
||||||
|
// NO_JS_MODULE_SYSTEM
|
||||||
|
// FUNCTION_CALLED_TIMES: require count=3
|
||||||
|
|
||||||
|
@JsModule("lib")
|
||||||
|
@JsNonModule
|
||||||
|
external fun f(x: Int): String
|
||||||
|
|
||||||
|
@JsModule("lib")
|
||||||
|
@JsNonModule
|
||||||
|
external fun f(x: String): String
|
||||||
|
|
||||||
|
@JsModule("lib")
|
||||||
|
@JsNonModule
|
||||||
|
external fun g(x: Boolean): String
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
val result = f(23) + f("foo") + g(true)
|
||||||
|
if (result != "abc") return "fail: $result"
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user