Added support for returning value from safe qualified expression.

This commit is contained in:
Pavel Talanov
2011-11-17 15:03:23 +04:00
parent ce39645ccd
commit 628a2334f5
3 changed files with 19 additions and 3 deletions
@@ -379,10 +379,11 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
public JsNode visitSafeQualifiedExpression(@NotNull JetSafeQualifiedExpression expression,
@NotNull TranslationContext context) {
JsExpression receiver = translateReceiver(expression, context);
JsNullLiteral nullLiteral = context.program().getNullLiteral();
JsBinaryOperation nullCheck = new JsBinaryOperation
(JsBinaryOperator.NEQ, receiver, context.program().getNullLiteral());
JsStatement thenStatement = AstUtil.convertToStatement(translateQualifiedExpression(expression, context));
return new JsIf(nullCheck, thenStatement, null);
(JsBinaryOperator.NEQ, receiver, nullLiteral);
JsExpression thenExpression = AstUtil.convertToExpression(translateQualifiedExpression(expression, context));
return new JsConditional(nullCheck, thenExpression, nullLiteral);
}
@@ -44,6 +44,11 @@ public final class PropertyAccessorTest extends AbstractClassTest {
testFooBoxIsTrue("safeCall.kt");
}
@Test
public void safeCallReturnsNullIfFails() throws Exception {
testFooBoxIsTrue("safeCallReturnsNullIfFails.kt");
}
//TODO test
// @Test
// public void namespaceCustomAccessors() throws Exception {
@@ -0,0 +1,10 @@
namespace foo
class A() {
val x = 4
}
fun box() : Boolean {
var a : A? = null;
return (a?.x == null);
}