KT-12976: add code to generated JS modules that detects wrong module order and produces human-friendly error message. Fix #KT-12976

This commit is contained in:
Alexey Andreev
2016-09-13 12:44:31 +03:00
parent d665193c20
commit 0a240b2a3a
7 changed files with 120 additions and 19 deletions
@@ -112,16 +112,6 @@ public final class Namer {
public static final String ENUM_NAME_FIELD = "name$";
public static final String ENUM_ORDINAL_FIELD = "ordinal$";
public static boolean isUndefined(@NotNull JsExpression expr) {
if (expr instanceof JsPrefixOperation) {
JsUnaryOperator op = ((JsPrefixOperation) expr).getOperator();
return op == JsUnaryOperator.VOID;
}
return false;
}
@NotNull
public static String getFunctionTag(@NotNull CallableDescriptor functionDescriptor) {
String moduleName = getModuleName(functionDescriptor);
@@ -63,9 +63,14 @@ object ModuleWrapperTranslation {
else {
JsNameRef(scope.declareName(moduleId), rootName.makeRef())
}
val plainExpr = JsAstUtils.assignment(lhs, plainInvocation)
val selector = JsAstUtils.newJsIf(amdTest, amdBody, JsAstUtils.newJsIf(commonJsTest, commonJsBody, plainExpr.makeStmt()))
val plainBlock = JsBlock()
for (importedModule in importedModules) {
plainBlock.statements += addModuleValidation(moduleId, program, importedModule)
}
plainBlock.statements += JsAstUtils.assignment(lhs, plainInvocation).makeStmt()
val selector = JsAstUtils.newJsIf(amdTest, amdBody, JsAstUtils.newJsIf(commonJsTest, commonJsBody, plainBlock))
adapterBody.statements += selector
return listOf(JsInvocation(adapter, JsLiteral.THIS, function).makeStmt())
@@ -102,15 +107,34 @@ object ModuleWrapperTranslation {
importedModules: List<String>, program: JsProgram
): List<JsStatement> {
val invocation = makePlainInvocation(moduleId, function, importedModules, program)
val statements = mutableListOf<JsStatement>()
val statement = if (Namer.requiresEscaping(moduleId)) {
for (importedModule in importedModules) {
statements += addModuleValidation(moduleId, program, importedModule)
}
statements += if (Namer.requiresEscaping(moduleId)) {
JsAstUtils.assignment(makePlainModuleRef(moduleId, program), invocation).makeStmt()
}
else {
JsAstUtils.newVar(program.rootScope.declareName(moduleId), invocation)
}
return listOf(statement)
return statements
}
private fun addModuleValidation(
currentModuleId: String,
program: JsProgram,
moduleName: String
): JsStatement {
val moduleRef = makePlainModuleRef(moduleName, program)
val moduleExistsCond = JsAstUtils.typeOfIs(moduleRef, program.getStringLiteral("undefined"))
val moduleNotFoundMessage = program.getStringLiteral(
"Error loading module '" + currentModuleId + "'. Its dependency '" + moduleName + "' was not found. " +
"Please, check whether '" + moduleName + "' is loaded prior to '" + currentModuleId + "'.")
val moduleNotFoundThrow = JsThrow(JsNew(JsNameRef("Error"), listOf<JsExpression>(moduleNotFoundMessage)))
return JsIf(moduleExistsCond, JsBlock(moduleNotFoundThrow))
}
private fun makePlainInvocation(
@@ -0,0 +1,14 @@
// MODULE: lib
// FILE: lib.kt
package lib
fun bar() = "OK"
// MODULE: main(lib)
// FILE: main.kt
package foo
import lib.bar
fun box() = bar()
+16
View File
@@ -0,0 +1,16 @@
// MODULE: lib
// FILE: lib.kt
// MODULE_KIND: UMD
package lib
fun bar() = "OK"
// MODULE: main(lib)
// FILE: main.kt
// MODULE_KIND: UMD
package foo
import lib.bar
fun box() = bar()