Supported another case of when expression.
This commit is contained in:
@@ -269,7 +269,7 @@ public class AstUtil {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static JsPrefixOperation negation(JsExpression expression) {
|
||||
public static JsPrefixOperation negated(JsExpression expression) {
|
||||
return new JsPrefixOperation(JsUnaryOperator.NOT, expression);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public final class PatternTranslator extends AbstractTranslator {
|
||||
JetPattern pattern = getPattern(expression);
|
||||
JsExpression resultingExpression = translatePattern(pattern, left);
|
||||
if (expression.isNegated()) {
|
||||
return AstUtil.negation(resultingExpression);
|
||||
return AstUtil.negated(resultingExpression);
|
||||
}
|
||||
return resultingExpression;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ public class WhenTranslator extends AbstractTranslator {
|
||||
private final JsExpression expressionToMatch;
|
||||
@NotNull
|
||||
private final JsName dummyCounterName;
|
||||
private int currentEntryNumber = 0;
|
||||
|
||||
private WhenTranslator(@NotNull JetWhenExpression expression, @NotNull TranslationContext context) {
|
||||
super(context);
|
||||
@@ -35,16 +36,27 @@ public class WhenTranslator extends AbstractTranslator {
|
||||
|
||||
@NotNull
|
||||
JsNode translate() {
|
||||
return translateEntries();
|
||||
JsFor resultingFor = generateDummyFor();
|
||||
List<JsStatement> entries = translateEntries();
|
||||
resultingFor.setBody(AstUtil.newBlock(entries));
|
||||
return resultingFor;
|
||||
}
|
||||
|
||||
private JsBlock translateEntries() {
|
||||
//JsFor resultingFor = generateDummyFor();
|
||||
@NotNull
|
||||
private List<JsStatement> translateEntries() {
|
||||
List<JsStatement> entries = new ArrayList<JsStatement>();
|
||||
for (JetWhenEntry entry : whenExpression.getEntries()) {
|
||||
entries.add(translateEntry(entry));
|
||||
entries.add(surroundWithDummyIf(translateEntry(entry)));
|
||||
}
|
||||
return AstUtil.newBlock(entries);
|
||||
return entries;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsStatement surroundWithDummyIf(@NotNull JsStatement entryStatement) {
|
||||
JsExpression stepNumberEqualsCurrentEntryNumber = new JsBinaryOperation(JsBinaryOperator.EQ,
|
||||
dummyCounterName.makeRef(), translationContext().program().getNumberLiteral(currentEntryNumber));
|
||||
currentEntryNumber++;
|
||||
return new JsIf(stepNumberEqualsCurrentEntryNumber, entryStatement, null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -110,8 +122,12 @@ public class WhenTranslator extends AbstractTranslator {
|
||||
|
||||
@NotNull
|
||||
private JsExpression translatePatternCondition(@NotNull JetWhenConditionIsPattern condition) {
|
||||
return Translation.typeOperationTranslator(translationContext()).
|
||||
JsExpression patternMatchExpression = Translation.typeOperationTranslator(translationContext()).
|
||||
translatePattern(getPattern(condition), expressionToMatch);
|
||||
if (condition.isNegated()) {
|
||||
return AstUtil.negated(patternMatchExpression);
|
||||
}
|
||||
return patternMatchExpression;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -19,4 +19,9 @@ public final class PatternMatchingTest extends IncludeLibraryTest {
|
||||
testFooBoxIsTrue("whenType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotType() throws Exception {
|
||||
testFooBoxIsTrue("whenNotType.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ public final class RhinoFunctionResultChecker implements RhinoResultChecker {
|
||||
@Override
|
||||
public void runChecks(Context context, Scriptable scope) throws Exception {
|
||||
Object result = extractAndCallFunctionObject(namespaceName, functionName, context, scope);
|
||||
assertTrue("Result is not what expected! Exprected: " + expectedResult + " Evaluated : " + result,
|
||||
assertTrue("Result is not what expected! Expected: " + expectedResult + " Evaluated : " + result,
|
||||
result.equals(expectedResult));
|
||||
String report = namespaceName + "." + functionName + "() = " + Context.toString(result);
|
||||
System.out.println(report);
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace foo
|
||||
|
||||
class A() {
|
||||
|
||||
}
|
||||
|
||||
fun box() : Boolean {
|
||||
when(A()) {
|
||||
!is A => return false;
|
||||
else => return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user