KT-852 Apply sure() call for expressions inside polyadic expression
This commit is contained in:
@@ -601,9 +601,9 @@ public class Converter {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static List<String> createConversions(@NotNull PsiCallExpression expression) {
|
public static List<String> createConversions(@NotNull PsiCallExpression expression) {
|
||||||
List<String> conversions = new LinkedList<String>();
|
|
||||||
PsiExpressionList argumentList = expression.getArgumentList();
|
PsiExpressionList argumentList = expression.getArgumentList();
|
||||||
PsiExpression[] arguments = argumentList != null ? argumentList.getExpressions() : new PsiExpression[]{};
|
PsiExpression[] arguments = argumentList != null ? argumentList.getExpressions() : new PsiExpression[]{};
|
||||||
|
List<String> conversions = new LinkedList<String>();
|
||||||
//noinspection UnusedDeclaration
|
//noinspection UnusedDeclaration
|
||||||
for (final PsiExpression a : arguments) {
|
for (final PsiExpression a : arguments) {
|
||||||
conversions.add("");
|
conversions.add("");
|
||||||
@@ -628,19 +628,41 @@ public class Converter {
|
|||||||
return conversions;
|
return conversions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static List<String> createConversions(@NotNull PsiPolyadicExpression expression, PsiType expectedType) {
|
||||||
|
PsiExpression[] arguments = expression.getOperands();
|
||||||
|
int length = arguments.length;
|
||||||
|
List<String> conversions = new LinkedList<String>();
|
||||||
|
|
||||||
|
List<PsiType> expectedTypes = Collections.nCopies(length, expectedType);
|
||||||
|
List<PsiType> actualTypes = new LinkedList<PsiType>();
|
||||||
|
|
||||||
|
for (PsiExpression e : arguments)
|
||||||
|
actualTypes.add(e.getType());
|
||||||
|
|
||||||
|
assert actualTypes.size() == expectedTypes.size() : "The type list must have the same length";
|
||||||
|
|
||||||
|
for (int i = 0; i < actualTypes.size(); i++)
|
||||||
|
conversions.add(i, createConversionForExpression(arguments[i], expectedTypes.get(i)));
|
||||||
|
|
||||||
|
return conversions;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static String createConversionForExpression(@Nullable PsiExpression expression, @NotNull PsiType expectedType) {
|
private static String createConversionForExpression(@Nullable PsiExpression expression, @NotNull PsiType expectedType) {
|
||||||
String conversion = "";
|
String conversion = "";
|
||||||
if (expression != null) {
|
if (expression != null) {
|
||||||
PsiType actualType = expression.getType();
|
PsiType actualType = expression.getType();
|
||||||
if (actualType != null) {
|
boolean isPrimitiveTypeOrNull = actualType == null || Node.PRIMITIVE_TYPES.contains(actualType.getCanonicalText());
|
||||||
if (Node.PRIMITIVE_TYPES.contains(actualType.getCanonicalText()) && (expression instanceof PsiReferenceExpression
|
boolean isRef = (expression instanceof PsiReferenceExpression && ((PsiReferenceExpression) expression).isQualified() || expression instanceof PsiMethodCallExpression);
|
||||||
&& ((PsiReferenceExpression) expression).isQualified() || expression instanceof PsiMethodCallExpression)
|
boolean containsQuestDot = expressionToExpression(expression).toKotlin().contains("?.");
|
||||||
&& expressionToExpression(expression).toKotlin().contains("?."))
|
|
||||||
conversion += ".sure()";
|
if (isPrimitiveTypeOrNull && isRef && containsQuestDot)
|
||||||
|
conversion += ".sure()";
|
||||||
|
|
||||||
|
if (actualType != null)
|
||||||
if (isConversionNeeded(actualType, expectedType))
|
if (isConversionNeeded(actualType, expectedType))
|
||||||
conversion += getPrimitiveTypeConversion(expectedType.getCanonicalText());
|
conversion += getPrimitiveTypeConversion(expectedType.getCanonicalText());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return conversion;
|
return conversion;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package org.jetbrains.jet.j2k.ast;
|
package org.jetbrains.jet.j2k.ast;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.j2k.util.AstUtil;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ignatov
|
* @author ignatov
|
||||||
@@ -9,16 +13,23 @@ public class BinaryExpression extends Expression {
|
|||||||
private final Expression myLeft;
|
private final Expression myLeft;
|
||||||
private final Expression myRight;
|
private final Expression myRight;
|
||||||
private final String myOp;
|
private final String myOp;
|
||||||
|
private final List<String> myConversions;
|
||||||
|
|
||||||
public BinaryExpression(Expression left, Expression right, String op) {
|
public BinaryExpression(Expression left, Expression right, String op) {
|
||||||
|
this(left, right, op, Arrays.asList("", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
public BinaryExpression(Expression left, Expression right, String op, List<String> conversions) {
|
||||||
myLeft = left;
|
myLeft = left;
|
||||||
myRight = right;
|
myRight = right;
|
||||||
myOp = op;
|
myOp = op;
|
||||||
|
myConversions = conversions;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String toKotlin() {
|
public String toKotlin() {
|
||||||
return "(" + myLeft.toKotlin() + SPACE + myOp + SPACE + myRight.toKotlin() + ")";
|
List<String> expressionsWithConversions = AstUtil.applyConversions(AstUtil.nodesToKotlin(Arrays.asList(myLeft, myRight)), myConversions);
|
||||||
|
return "(" + AstUtil.join(expressionsWithConversions, SPACE + myOp + SPACE) + ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,16 +11,19 @@ import java.util.List;
|
|||||||
public class PolyadicExpression extends Expression {
|
public class PolyadicExpression extends Expression {
|
||||||
private final List<Expression> myExpressions;
|
private final List<Expression> myExpressions;
|
||||||
private final String myToken;
|
private final String myToken;
|
||||||
|
private final List<String> myConversions;
|
||||||
|
|
||||||
public PolyadicExpression(List<Expression> expressions, String token) {
|
public PolyadicExpression(List<Expression> expressions, String token, List<String> conversions) {
|
||||||
super();
|
super();
|
||||||
myExpressions = expressions;
|
myExpressions = expressions;
|
||||||
myToken = token;
|
myToken = token;
|
||||||
|
myConversions = conversions;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String toKotlin() {
|
public String toKotlin() {
|
||||||
return "(" + AstUtil.joinNodes(myExpressions, SPACE + myToken + SPACE) + ")";
|
List<String> expressionsWithConversions = AstUtil.applyConversions(AstUtil.nodesToKotlin(myExpressions), myConversions);
|
||||||
|
return "(" + AstUtil.join(expressionsWithConversions, SPACE + myToken + SPACE) + ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package org.jetbrains.jet.j2k.util;
|
|||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.j2k.ast.Expression;
|
|
||||||
import org.jetbrains.jet.j2k.ast.INode;
|
import org.jetbrains.jet.j2k.ast.INode;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
@@ -57,10 +56,10 @@ public class AstUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static List<String> createListWithEmptyString(@NotNull final List<Expression> arguments) {
|
public static <T> List<String> createListWithEmptyString(@NotNull final List<T> arguments) {
|
||||||
final List<String> conversions = new LinkedList<String>();
|
final List<String> conversions = new LinkedList<String>();
|
||||||
//noinspection UnusedDeclaration
|
//noinspection UnusedDeclaration
|
||||||
for (Expression argument : arguments) conversions.add("");
|
for (T argument : arguments) conversions.add("");
|
||||||
return conversions;
|
return conversions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -125,7 +125,8 @@ public class ExpressionVisitor extends StatementVisitor {
|
|||||||
new BinaryExpression(
|
new BinaryExpression(
|
||||||
expressionToExpression(expression.getLOperand()),
|
expressionToExpression(expression.getLOperand()),
|
||||||
expressionToExpression(expression.getROperand()),
|
expressionToExpression(expression.getROperand()),
|
||||||
getOperatorString(expression.getOperationSign().getTokenType())
|
getOperatorString(expression.getOperationSign().getTokenType()),
|
||||||
|
createConversions(expression, PsiType.BOOLEAN)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -449,7 +450,8 @@ public class ExpressionVisitor extends StatementVisitor {
|
|||||||
super.visitPolyadicExpression(expression);
|
super.visitPolyadicExpression(expression);
|
||||||
myResult = new PolyadicExpression(
|
myResult = new PolyadicExpression(
|
||||||
expressionsToExpressionList(expression.getOperands()),
|
expressionsToExpressionList(expression.getOperands()),
|
||||||
getOperatorString(expression.getOperationTokenType())
|
getOperatorString(expression.getOperationTokenType()),
|
||||||
|
createConversions(expression, PsiType.BOOLEAN)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package demo;
|
package demo;
|
||||||
|
|
||||||
class Container {
|
class Container {
|
||||||
String myString = 1;
|
String myString = "1";
|
||||||
}
|
}
|
||||||
|
|
||||||
class One {
|
class One {
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package demo;
|
||||||
|
|
||||||
|
class Test {
|
||||||
|
String test() {
|
||||||
|
String s1 = "";
|
||||||
|
String s2 = "";
|
||||||
|
String s3 = "";
|
||||||
|
if (s1.isEmpty() && s2.isEmpty())
|
||||||
|
return "OK";
|
||||||
|
|
||||||
|
if (s1.isEmpty() && s2.isEmpty() && s3.isEmpty())
|
||||||
|
return "OOOK";
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package demo
|
||||||
|
open class Test() {
|
||||||
|
open fun test() : String? {
|
||||||
|
var s1 : String? = ""
|
||||||
|
var s2 : String? = ""
|
||||||
|
var s3 : String? = ""
|
||||||
|
if (((s1?.isEmpty()).sure() && (s2?.isEmpty()).sure()))
|
||||||
|
return "OK"
|
||||||
|
if (((s1?.isEmpty()).sure() && (s2?.isEmpty()).sure() && (s3?.isEmpty()).sure()))
|
||||||
|
return "OOOK"
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user