diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/jsCode/noJavaScriptProduced.kt b/compiler/testData/diagnostics/testsWithJsStdLib/jsCode/noJavaScriptProduced.kt new file mode 100644 index 00000000000..117a24fdb7d --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/jsCode/noJavaScriptProduced.kt @@ -0,0 +1,16 @@ +fun test() { + js("") + js(" ") + js(""" + """) + + val empty = "" + js(empty) + + val whitespace = " " + js(whitespace) + + val multiline = """ + """ + js(multiline) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/jsCode/noJavaScriptProduced.txt b/compiler/testData/diagnostics/testsWithJsStdLib/jsCode/noJavaScriptProduced.txt new file mode 100644 index 00000000000..b8def46715c --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/jsCode/noJavaScriptProduced.txt @@ -0,0 +1,3 @@ +package + +internal fun test(): kotlin.Unit diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestWithJsStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestWithJsStdLibGenerated.java index 65be4aec383..2bb9e353e12 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestWithJsStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestWithJsStdLibGenerated.java @@ -257,6 +257,12 @@ public class JetDiagnosticsTestWithJsStdLibGenerated extends AbstractJetDiagnost doTest(fileName); } + @TestMetadata("noJavaScriptProduced.kt") + public void testNoJavaScriptProduced() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/jsCode/noJavaScriptProduced.kt"); + doTest(fileName); + } + @TestMetadata("warning.kt") public void testWarning() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/jsCode/warning.kt"); diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/DefaultErrorMessagesJs.kt b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/DefaultErrorMessagesJs.kt index 434a1252a6c..1bbb748505b 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/DefaultErrorMessagesJs.kt +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/DefaultErrorMessagesJs.kt @@ -38,6 +38,7 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by Delegates.lazy { put(ErrorsJs.REFERENCE_TO_BUILTIN_MEMBERS_NOT_SUPPORTED, "Callable references for builtin members are not supported yet: ''{0}''", RenderFirstLineOfElementText) put(ErrorsJs.NON_TOPLEVEL_CLASS_DECLARATION, "Non-toplevel {0} declarations not supported yet", Renderers.STRING) put(ErrorsJs.SECONDARY_CONSTRUCTOR, "Secondary constructors not supported yet") + put(ErrorsJs.JSCODE_NO_JAVASCRIPT_PRODUCED, "Argument must be non-empty JavaScript code") put(ErrorsJs.INLINE_CALL_CYCLE, "The call is a part of inline cycle") this diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/ErrorsJs.java b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/ErrorsJs.java index ca1f59c70b8..28bd0420e92 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/ErrorsJs.java +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/ErrorsJs.java @@ -45,6 +45,7 @@ public interface ErrorsJs { DiagnosticFactory1 REFERENCE_TO_BUILTIN_MEMBERS_NOT_SUPPORTED = DiagnosticFactory1.create(ERROR, DEFAULT); DiagnosticFactory1 NON_TOPLEVEL_CLASS_DECLARATION = DiagnosticFactory1.create(ERROR, DECLARATION_NAME); DiagnosticFactory0 SECONDARY_CONSTRUCTOR = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE_OR_DEFAULT); + DiagnosticFactory0 JSCODE_NO_JAVASCRIPT_PRODUCED = DiagnosticFactory0.create(ERROR, DEFAULT); DiagnosticFactory0 INLINE_CALL_CYCLE = DiagnosticFactory0.create(ERROR, DEFAULT); @SuppressWarnings("UnusedDeclaration") diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsCallChecker.kt b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsCallChecker.kt index 3cfc0adce75..1dde587e6a8 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsCallChecker.kt +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsCallChecker.kt @@ -43,6 +43,7 @@ import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluat import org.jetbrains.kotlin.types.JetType import com.intellij.openapi.util.TextRange +import org.jetbrains.kotlin.js.parser.parse import java.io.StringReader import kotlin.platform.platformStatic @@ -81,18 +82,16 @@ public class JsCallChecker : CallChecker { } val code = evaluationResult.getValue() as String - val reader = StringReader(code) val errorReporter = JsCodeErrorReporter(argument, code, context.trace) - Context.enter().setErrorReporter(errorReporter) try { - val ts = TokenStream(reader, "js", 0) - val parser = Parser(IRFactory(ts), /* insideFunction = */ true) - parser.parse(ts) + val statements = parse(code, errorReporter, insideFunction = true) + + if (statements.size() == 0) { + context.trace.report(ErrorsJs.JSCODE_NO_JAVASCRIPT_PRODUCED.on(argument)) + } } catch (e: AbortParsingException) { // ignore - } finally { - Context.exit() } } }