Support when without expression.
#KT-1666 Fixed
This commit is contained in:
@@ -72,7 +72,16 @@ public final class PatternMatchingTest extends SingleFileTranslationTest {
|
||||
try {
|
||||
checkFooBoxIsTrue("whenAsExpressionWithThrow.kt");
|
||||
fail();
|
||||
} catch (JavaScriptException e) {
|
||||
}
|
||||
catch (JavaScriptException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testKT1665() throws Exception {
|
||||
checkOutput("kt1665.kt", "a", "");
|
||||
}
|
||||
|
||||
public void testWhenWithoutExpression() throws Exception {
|
||||
checkFooBoxIsTrue("whenWithoutExpression.kt");
|
||||
}
|
||||
}
|
||||
+17
-9
@@ -61,12 +61,16 @@ public final class PatternTranslator extends AbstractTranslator {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JsExpression translatePattern(@NotNull JetPattern pattern, @NotNull JsExpression expressionToMatch) {
|
||||
public JsExpression translatePattern(@NotNull JetPattern pattern, @Nullable JsExpression expressionToMatch) {
|
||||
if (expressionToMatch == null) {
|
||||
assert pattern instanceof JetExpressionPattern : "When using when without parameters we can have only expression patterns";
|
||||
return translateExpressionForExpressionPattern((JetExpressionPattern)pattern);
|
||||
}
|
||||
if (pattern instanceof JetTypePattern) {
|
||||
return translateTypePattern(expressionToMatch, (JetTypePattern) pattern);
|
||||
return translateTypePattern(expressionToMatch, (JetTypePattern)pattern);
|
||||
}
|
||||
if (pattern instanceof JetExpressionPattern) {
|
||||
return translateExpressionPattern(expressionToMatch, (JetExpressionPattern) pattern);
|
||||
return translateExpressionPattern(expressionToMatch, (JetExpressionPattern)pattern);
|
||||
}
|
||||
throw new AssertionError("Unsupported pattern type " + pattern.getClass());
|
||||
}
|
||||
@@ -124,16 +128,20 @@ public final class PatternTranslator extends AbstractTranslator {
|
||||
@NotNull
|
||||
private JsNameRef getClassNameReferenceForTypeReference(@NotNull JetTypeReference typeReference) {
|
||||
ClassDescriptor referencedClass = BindingUtils.getClassDescriptorForTypeReference
|
||||
(bindingContext(), typeReference);
|
||||
(bindingContext(), typeReference);
|
||||
return TranslationUtils.getQualifiedReference(context(), referencedClass);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsExpression translateExpressionPattern(JsExpression expressionToMatch, JetExpressionPattern pattern) {
|
||||
JetExpression patternExpression = pattern.getExpression();
|
||||
assert patternExpression != null : "Expression patter should have an expression.";
|
||||
JsExpression expressionToMatchAgainst =
|
||||
Translation.translateAsExpression(patternExpression, context());
|
||||
private JsExpression translateExpressionPattern(@NotNull JsExpression expressionToMatch, JetExpressionPattern pattern) {
|
||||
JsExpression expressionToMatchAgainst = translateExpressionForExpressionPattern(pattern);
|
||||
return equality(expressionToMatch, expressionToMatchAgainst);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsExpression translateExpressionForExpressionPattern(@NotNull JetExpressionPattern pattern) {
|
||||
JetExpression patternExpression = pattern.getExpression();
|
||||
assert patternExpression != null : "Expression patter should have an expression.";
|
||||
return Translation.translateAsExpression(patternExpression, context());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,17 +36,17 @@ import static org.jetbrains.k2js.translate.utils.JsAstUtils.*;
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
public class WhenTranslator extends AbstractTranslator {
|
||||
public final class WhenTranslator extends AbstractTranslator {
|
||||
|
||||
@NotNull
|
||||
static public JsNode translateWhenExpression(@NotNull JetWhenExpression expression, @NotNull TranslationContext context) {
|
||||
public static JsNode translateWhenExpression(@NotNull JetWhenExpression expression, @NotNull TranslationContext context) {
|
||||
WhenTranslator translator = new WhenTranslator(expression, context);
|
||||
return translator.translate();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private final JetWhenExpression whenExpression;
|
||||
@NotNull
|
||||
@Nullable
|
||||
private final JsExpression expressionToMatch;
|
||||
@NotNull
|
||||
private final TemporaryVariable dummyCounter;
|
||||
@@ -179,7 +179,7 @@ public class WhenTranslator extends AbstractTranslator {
|
||||
@NotNull
|
||||
private JsExpression translatePatternCondition(@NotNull JetWhenCondition condition) {
|
||||
JsExpression patternMatchExpression = Translation.patternTranslator(context()).
|
||||
translatePattern(getPattern(condition), expressionToMatch);
|
||||
translatePattern(getPattern(condition), expressionToMatch);
|
||||
if (isNegated(condition)) {
|
||||
return negated(patternMatchExpression);
|
||||
}
|
||||
@@ -188,7 +188,7 @@ public class WhenTranslator extends AbstractTranslator {
|
||||
|
||||
private static boolean isNegated(@NotNull JetWhenCondition condition) {
|
||||
if (condition instanceof JetWhenConditionIsPattern) {
|
||||
return ((JetWhenConditionIsPattern) condition).isNegated();
|
||||
return ((JetWhenConditionIsPattern)condition).isNegated();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -197,10 +197,10 @@ public class WhenTranslator extends AbstractTranslator {
|
||||
private static JetPattern getPattern(@NotNull JetWhenCondition condition) {
|
||||
JetPattern pattern;
|
||||
if (condition instanceof JetWhenConditionIsPattern) {
|
||||
pattern = ((JetWhenConditionIsPattern) condition).getPattern();
|
||||
pattern = ((JetWhenConditionIsPattern)condition).getPattern();
|
||||
}
|
||||
else if (condition instanceof JetWhenConditionWithExpression) {
|
||||
pattern = ((JetWhenConditionWithExpression) condition).getPattern();
|
||||
pattern = ((JetWhenConditionWithExpression)condition).getPattern();
|
||||
}
|
||||
else {
|
||||
throw new AssertionError("Wrong type of JetWhenCondition");
|
||||
@@ -209,10 +209,12 @@ public class WhenTranslator extends AbstractTranslator {
|
||||
return pattern;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Nullable
|
||||
private JsExpression translateExpressionToMatch(@NotNull JetWhenExpression expression) {
|
||||
JetExpression subject = expression.getSubjectExpression();
|
||||
assert subject != null : "Subject should not be null.";
|
||||
if (subject == null) {
|
||||
return null;
|
||||
}
|
||||
return Translation.translateAsExpression(subject, context());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
fun main(args : Array<String>) {
|
||||
val a = 10
|
||||
val b = 3
|
||||
when {
|
||||
a > b -> println("a")
|
||||
b > a -> println("b")
|
||||
else -> println("Unknown")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package foo
|
||||
|
||||
fun box() = when {
|
||||
1 > 3 -> false
|
||||
else -> true
|
||||
}
|
||||
Reference in New Issue
Block a user