提交 9b4b7960 编写于 作者: A Alexey Andreev 提交者: Alexey Andreev

JS: prevent compiler from importing module more than once if not necessary. See KT-15260

上级 1ecd9579
......@@ -5083,6 +5083,18 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/jsModule/externalProperty.kt");
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")
......
......@@ -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) {
String value = arguments.findNamedArgument(name);
return value != null ? Boolean.parseBoolean(value) : defaultValue;
......@@ -253,6 +264,7 @@ public class DirectiveTestUtils {
private static final List<DirectiveHandler> DIRECTIVE_HANDLERS = Arrays.asList(
FUNCTION_CONTAINS_NO_CALLS,
FUNCTION_NOT_CALLED,
FUNCTION_CALLED_TIMES,
PROPERTY_NOT_USED,
PROPERTY_NOT_READ_FROM,
PROPERTY_NOT_WRITTEN_TO,
......
......@@ -661,12 +661,13 @@ public final class StaticContext {
@NotNull
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);
if (module == null) {
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);
importedModules.put(key, module);
}
......@@ -835,11 +836,11 @@ public final class StaticContext {
private final String baseName;
@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.declaration = declaration;
this.plainName = plainName;
}
@Override
......@@ -850,7 +851,7 @@ public final class StaticContext {
ImportedModuleKey key = (ImportedModuleKey) o;
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;
}
......@@ -858,7 +859,7 @@ public final class StaticContext {
@Override
public int hashCode() {
int result = baseName.hashCode();
result = 31 * result + (declaration != null ? declaration.hashCode() : 0);
result = 31 * result + (plainName != null ? plainName.hashCode() : 0);
return result;
}
}
......
__beginModule__();
module.exports = function(param) {
switch (typeof param) {
case "number":
return "a";
case "string":
return "b";
default:
return "c";
}
};
__endModule__("lib");
\ No newline at end of file
// 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"
}
\ No newline at end of file
var f = function(param) {
switch (typeof param) {
case "number":
return "a";
case "string":
return "b";
default:
return "c";
}
};
var g = f;
\ No newline at end of file
// 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"
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册