Skip to content

Commit

Permalink
Added support for selects on type casts.
Browse files Browse the repository at this point in the history
fixes #85
  • Loading branch information
traceyyoshima committed Apr 7, 2023
1 parent 19ea45b commit e7aa8d1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2121,10 +2121,17 @@ public J visitTypeAlias(FirTypeAlias typeAlias, ExecutionContext ctx) {
public J visitTypeOperatorCall(FirTypeOperatorCall typeOperatorCall, ExecutionContext ctx) {
Space prefix = whitespace();
FirExpression expression = typeOperatorCall.getArgumentList().getArguments().get(0);
Markers markers = Markers.EMPTY;
boolean includeParentheses = false;
if (typeOperatorCall.getOperation() == FirOperation.AS && source.startsWith("(", cursor)) {
skip("(");
includeParentheses = true;
}

Expression element;
// A when subject expression does not have a target because it's implicit
if (expression instanceof FirWhenSubjectExpression) {
element = new J.Empty(randomId(), EMPTY, Markers.EMPTY);
element = new J.Empty(randomId(), EMPTY, Markers.EMPTY);
} else {
FirElement target = expression instanceof FirSmartCastExpression ?
((FirSmartCastExpression) expression).getOriginalExpression() :
Expand All @@ -2133,7 +2140,6 @@ public J visitTypeOperatorCall(FirTypeOperatorCall typeOperatorCall, ExecutionCo
}

Space after;
Markers markers = Markers.EMPTY;
switch (typeOperatorCall.getOperation()) {
case IS:
after = sourceBefore("is");
Expand All @@ -2154,6 +2160,9 @@ public J visitTypeOperatorCall(FirTypeOperatorCall typeOperatorCall, ExecutionCo
}

if (typeOperatorCall.getOperation() == FirOperation.AS || typeOperatorCall.getOperation() == FirOperation.SAFE_AS) {
if (!includeParentheses) {
markers = markers.addIfAbsent(new OmitParentheses(randomId()));
}
return new J.TypeCast(
randomId(),
prefix,
Expand All @@ -2162,7 +2171,8 @@ public J visitTypeOperatorCall(FirTypeOperatorCall typeOperatorCall, ExecutionCo
randomId(),
after,
Markers.EMPTY,
JRightPadded.build((TypeTree) visitElement(typeOperatorCall.getConversionTypeRef(), ctx))),
JRightPadded.build((TypeTree) visitElement(typeOperatorCall.getConversionTypeRef(), ctx))
.withAfter(includeParentheses ? sourceBefore(")") : EMPTY)),
element);
} else {
JRightPadded<Expression> expr = JRightPadded.build(element).withAfter(after);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,10 @@ public J visitTernary(J.Ternary ternary, PrintOutputCapture<P> p) {
@Override
public J visitTypeCast(J.TypeCast typeCast, PrintOutputCapture<P> p) {
beforeSyntax(typeCast, Space.Location.TYPE_CAST_PREFIX, p);
boolean printParens = !typeCast.getMarkers().findFirst(OmitParentheses.class).isPresent();
if (printParens) {
p.append('(');
}
visit(typeCast.getExpression(), p);

J.ControlParentheses<TypeTree> controlParens = typeCast.getClazz();
Expand All @@ -651,7 +655,9 @@ public J visitTypeCast(J.TypeCast typeCast, PrintOutputCapture<P> p) {
p.append(as);

visit(controlParens.getTree(), p);

if (printParens) {
p.append(')');
}
afterSyntax(typeCast, p);
return typeCast;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,25 @@ void unresolvedNameInLightSource() {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/84")
@Test
void varargArgumentExpression() {
rewriteRun(
kotlin(
"""
class StringValue {
val value: String = ""
}
"""
),
kotlin(
"""
fun method(input : Any) {
val split = (input as StringValue).value.split("-").toTypedArray()
}
"""
)
);
}
}

0 comments on commit e7aa8d1

Please sign in to comment.