Relax transformation assertions

This commit is contained in:
Alexey Sedunov
2013-05-15 14:02:32 +04:00
parent ee9b96e943
commit b2b7bce99a
4 changed files with 52 additions and 100 deletions
@@ -1,10 +1,12 @@
package org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lexer.JetTokens;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils.*;
@@ -18,7 +20,7 @@ public class IfWhenUtils {
}
public static boolean checkIfToWhen(@NotNull JetIfExpression ifExpression) {
return ifExpression.getCondition() != null && ifExpression.getThen() != null && ifExpression.getElse() != null;
return ifExpression.getThen() != null && ifExpression.getElse() != null;
}
public static boolean checkWhenToIf(@NotNull JetWhenExpression whenExpression) {
@@ -29,7 +31,9 @@ public class IfWhenUtils {
assert expression != null : TRANSFORM_WITHOUT_CHECK;
}
private static List<JetExpression> splitExpressionToOrBranches(JetExpression expression) {
private static List<JetExpression> splitExpressionToOrBranches(@Nullable JetExpression expression) {
if (expression == null) return Collections.emptyList();
final List<JetExpression> branches = new ArrayList<JetExpression>();
expression.accept(
@@ -80,14 +84,19 @@ public class IfWhenUtils {
JetExpression thenBranch = currIfExpression.getThen();
JetExpression elseBranch = currIfExpression.getElse();
assertNotNull(condition);
assertNotNull(thenBranch);
assertNotNull(elseBranch);
List<JetExpression> orBranches = splitExpressionToOrBranches(condition);
for (JetExpression orBranch : orBranches) {
builder.condition(orBranch);
if (orBranches.isEmpty()) {
builder.condition("");
} else {
for (JetExpression orBranch : orBranches) {
builder.condition(orBranch);
}
}
//noinspection ConstantConditions
builder.branchExpression(thenBranch);
@@ -135,19 +144,12 @@ public class IfWhenUtils {
List<JetWhenEntry> entries = whenExpression.getEntries();
for (JetWhenEntry entry : entries) {
JetExpression branch = entry.getExpression();
assertNotNull(branch);
if (entry.isElse()) {
//noinspection ConstantConditions
builder.elseBranch(branch);
} else {
String branchConditionText = combineWhenConditions(entry.getConditions(), whenExpression.getSubjectExpression());
JetExpression branchExpression = entry.getExpression();
assertNotNull(branchExpression);
//noinspection ConstantConditions
builder.ifBranch(branchConditionText, branchExpression.getText());
builder.ifBranch(branchConditionText, branch != null ? branch.getText() : "");
}
}
@@ -120,10 +120,8 @@ public class WhenUtils {
for (JetWhenEntry entry : whenExpression.getEntries()) {
JetExpression branchExpression = entry.getExpression();
assertNotNull(branchExpression);
if (entry.isElse()) {
//noinspection ConstantConditions
builder.elseEntry(branchExpression);
continue;
}
@@ -135,30 +133,21 @@ public class WhenUtils {
if (conditionExpression instanceof JetIsExpression) {
JetIsExpression isExpression = (JetIsExpression) conditionExpression;
JetTypeReference typeReference = isExpression.getTypeRef();
assertNotNull(typeReference);
//noinspection ConstantConditions
builder.pattern(typeReference, isExpression.isNegated());
builder.pattern(isExpression.getTypeRef(), isExpression.isNegated());
}
else if (conditionExpression instanceof JetBinaryExpression) {
JetBinaryExpression binaryExpression = (JetBinaryExpression) conditionExpression;
JetExpression rhs = binaryExpression.getRight();
assertNotNull(rhs);
IElementType op = binaryExpression.getOperationToken();
if (op == JetTokens.IN_KEYWORD) {
//noinspection ConstantConditions
builder.range(rhs, false);
}
else if (op == JetTokens.NOT_IN) {
//noinspection ConstantConditions
builder.range(rhs, true);
}
else if (op == JetTokens.EQEQ) {
//noinspection ConstantConditions
builder.condition(rhs);
}
else assert false : TRANSFORM_WITHOUT_CHECK;
@@ -166,7 +155,6 @@ public class WhenUtils {
else assert false : TRANSFORM_WITHOUT_CHECK;
}
//noinspection ConstantConditions
builder.branchExpression(branchExpression);
}
@@ -176,37 +164,22 @@ public class WhenUtils {
static String whenConditionToExpressionText(@NotNull JetWhenCondition condition, JetExpression subject) {
if (condition instanceof JetWhenConditionIsPattern) {
JetWhenConditionIsPattern patternCondition = (JetWhenConditionIsPattern) condition;
JetTypeReference typeReference = patternCondition.getTypeRef();
assertNotNull(typeReference);
assertNotNull(subject);
//noinspection ConstantConditions
return toBinaryExpression(subject, (patternCondition.isNegated() ? "!is" : "is"), typeReference);
return toBinaryExpression(subject, (patternCondition.isNegated() ? "!is" : "is"), patternCondition.getTypeRef());
}
if (condition instanceof JetWhenConditionInRange) {
JetWhenConditionInRange rangeCondition = (JetWhenConditionInRange) condition;
JetExpression rangeExpression = rangeCondition.getRangeExpression();
assertNotNull(rangeExpression);
assertNotNull(subject);
//noinspection ConstantConditions
return toBinaryExpression(subject, rangeCondition.getOperationReference().getText(), rangeExpression);
return toBinaryExpression(subject, rangeCondition.getOperationReference().getText(), rangeCondition.getRangeExpression());
}
assert condition instanceof JetWhenConditionWithExpression : TRANSFORM_WITHOUT_CHECK;
JetExpression conditionExpression = ((JetWhenConditionWithExpression) condition).getExpression();
assertNotNull(conditionExpression);
//noinspection ConstantConditions
return subject != null ?
toBinaryExpression(parenthesizeIfNeeded(subject), "==", parenthesizeIfNeeded(conditionExpression))
: conditionExpression.getText();
if (subject != null) {
return toBinaryExpression(parenthesizeIfNeeded(subject), "==", parenthesizeIfNeeded(conditionExpression));
}
return conditionExpression != null ? conditionExpression.getText() : "";
}
public static void eliminateWhenSubject(@NotNull JetWhenExpression whenExpression) {
@@ -217,10 +190,8 @@ public class WhenUtils {
for (JetWhenEntry entry : whenExpression.getEntries()) {
JetExpression branchExpression = entry.getExpression();
assertNotNull(branchExpression);
if (entry.isElse()) {
//noinspection ConstantConditions
builder.elseEntry(branchExpression);
continue;
@@ -230,7 +201,6 @@ public class WhenUtils {
builder.condition(whenConditionToExpressionText(condition, subject));
}
//noinspection ConstantConditions
builder.branchExpression(branchExpression);
}