JS backend: report diagonstic message for unsupported element instead of throw exception.
#KT-6507 Fixed
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
$TESTDATA_DIR$/diagnosticForUnhandledElements.kt
|
||||
-no-stdlib
|
||||
-output
|
||||
$TEMP_DIR$/out.js
|
||||
@@ -0,0 +1,18 @@
|
||||
package foo
|
||||
|
||||
annotation class fancy
|
||||
|
||||
[fancy]
|
||||
class Foo {
|
||||
[fancy]
|
||||
fun baz([fancy] foo : Int) : Int {
|
||||
return [fancy] 1
|
||||
}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
|
||||
class A {
|
||||
fun foo(){}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
WARNING: compiler/testData/cli/js/diagnosticForUnhandledElements.kt: (8, 21) Parameter 'foo' is never used
|
||||
ERROR: compiler/testData/cli/js/diagnosticForUnhandledElements.kt: (9, 16) Cannot translate (not supported yet): '[fancy] 1'
|
||||
ERROR: compiler/testData/cli/js/diagnosticForUnhandledElements.kt: (15, 5) Cannot translate (not supported yet): 'class A {...'
|
||||
COMPILATION_ERROR
|
||||
@@ -183,6 +183,12 @@ public class KotlincExecutableTestGenerated extends AbstractKotlincExecutableTes
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/cli/js"), Pattern.compile("^(.+)\\.args$"), false);
|
||||
}
|
||||
|
||||
@TestMetadata("diagnosticForUnhandledElements.args")
|
||||
public void testDiagnosticForUnhandledElements() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cli/js/diagnosticForUnhandledElements.args");
|
||||
doJsTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("jsExtraHelp.args")
|
||||
public void testJsExtraHelp() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cli/js/jsExtraHelp.args");
|
||||
|
||||
+2
@@ -34,6 +34,8 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by Delegates.lazy {
|
||||
put(ErrorsJs.JSCODE_ERROR, "JavaScript: {0}", JsCallDataTextRenderer)
|
||||
put(ErrorsJs.JSCODE_WARNING, "JavaScript: {0}", JsCallDataTextRenderer)
|
||||
put(ErrorsJs.JSCODE_ARGUMENT_SHOULD_BE_CONSTANT, "Argument must be string constant")
|
||||
put(ErrorsJs.NOT_SUPPORTED, "Cannot translate (not supported yet): ''{0}''", RenderFirstLineOfElementText)
|
||||
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ public interface ErrorsJs {
|
||||
DiagnosticFactory1<JetExpression, JsCallData> JSCODE_ERROR = DiagnosticFactory1.create(ERROR, JsCodePositioningStrategy.INSTANCE$);
|
||||
DiagnosticFactory1<JetExpression, JsCallData> JSCODE_WARNING = DiagnosticFactory1.create(WARNING, JsCodePositioningStrategy.INSTANCE$);
|
||||
DiagnosticFactory0<JetExpression> JSCODE_ARGUMENT_SHOULD_BE_CONSTANT = DiagnosticFactory0.create(ERROR, DEFAULT);
|
||||
DiagnosticFactory1<JetElement, JetElement> NOT_SUPPORTED = DiagnosticFactory1.create(ERROR, DEFAULT);
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
Object _initializer = new Object() {
|
||||
|
||||
@@ -16,10 +16,17 @@
|
||||
|
||||
package org.jetbrains.kotlin.js.resolve.diagnostics
|
||||
|
||||
import org.jetbrains.kotlin.renderer.Renderer
|
||||
import org.jetbrains.kotlin.js.resolve.diagnostics.JsCallData
|
||||
import org.jetbrains.kotlin.js.resolve.diagnostics.JsCallDataWithCode
|
||||
import com.google.gwt.dev.js.rhino.Utils.isEndOfLine
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.renderer.Renderer
|
||||
|
||||
object RenderFirstLineOfElementText : Renderer<PsiElement> {
|
||||
override fun render(element: PsiElement): String {
|
||||
val text = element.getText()
|
||||
val index = text.indexOf('\n')
|
||||
return if (index == -1) text else text.substring(0, index) + "..."
|
||||
}
|
||||
}
|
||||
|
||||
abstract class JsCallDataRenderer : Renderer<JsCallData> {
|
||||
protected abstract fun format(data: JsCallDataWithCode): String
|
||||
|
||||
+5
@@ -63,6 +63,11 @@ public class DeclarationBodyVisitor extends TranslatorVisitor<Void> {
|
||||
return enumEntryList;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void emptyResult(@NotNull TranslationContext context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitClass(@NotNull JetClass expression, TranslationContext context) {
|
||||
return null;
|
||||
|
||||
+5
@@ -60,6 +60,11 @@ import static org.jetbrains.kotlin.js.translate.utils.TranslationUtils.translate
|
||||
import static org.jetbrains.kotlin.resolve.BindingContextUtils.isVarCapturedInClosure;
|
||||
|
||||
public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
|
||||
@Override
|
||||
protected JsNode emptyResult(@NotNull TranslationContext context) {
|
||||
return context.getEmptyExpression();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public JsNode visitConstantExpression(@NotNull JetConstantExpression expression, @NotNull TranslationContext context) {
|
||||
|
||||
+6
-3
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.kotlin.js.translate.general;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.js.resolve.diagnostics.ErrorsJs;
|
||||
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
|
||||
import org.jetbrains.kotlin.psi.JetDeclaration;
|
||||
import org.jetbrains.kotlin.psi.JetDeclarationContainer;
|
||||
@@ -26,12 +27,14 @@ import org.jetbrains.kotlin.psi.JetVisitor;
|
||||
/**
|
||||
* This class is a base class for all visitors.
|
||||
*/
|
||||
public class TranslatorVisitor<T> extends JetVisitor<T, TranslationContext> {
|
||||
public abstract class TranslatorVisitor<T> extends JetVisitor<T, TranslationContext> {
|
||||
|
||||
protected abstract T emptyResult(@NotNull TranslationContext context);
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public T visitJetElement(@NotNull JetElement expression, TranslationContext context) {
|
||||
throw new UnsupportedOperationException("Unsupported expression encountered:" + expression.toString());
|
||||
context.bindingTrace().report(ErrorsJs.NOT_SUPPORTED.on(expression, expression));
|
||||
return emptyResult(context);
|
||||
}
|
||||
|
||||
public final void traverseContainer(@NotNull JetDeclarationContainer jetClass,
|
||||
|
||||
+5
@@ -37,6 +37,11 @@ public final class InitializerVisitor extends TranslatorVisitor<Void> {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void emptyResult(@NotNull TranslationContext context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Void visitProperty(@NotNull JetProperty property, @NotNull TranslationContext context) {
|
||||
JetExpression initializer = property.getInitializer();
|
||||
|
||||
Reference in New Issue
Block a user