Skip to content

Commit

Permalink
Merge branch 'main' into 571
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Feb 11, 2024
2 parents 747f3c2 + b74f43b commit abc2e45
Show file tree
Hide file tree
Showing 68 changed files with 1,409 additions and 886 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/comment-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: comment-pr
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#using-data-from-the-triggering-workflow
on:
workflow_run:
workflows: ["receive-pr"]
types:
- completed
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
# Since this pull request has write permissions on the target repo, we should **NOT** execute any untrusted code.
jobs:
post-suggestions:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
uses: openrewrite/gh-automation/.github/workflows/comment-pr.yml@main
secrets:
GH_PAT_ACTIONS_READ: ${{ secrets.GH_PAT_ACTIONS_READ }}
11 changes: 11 additions & 0 deletions .github/workflows/receive-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: receive-pr
on:
pull_request:
types: [opened, synchronize]
branches:
- main
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
# Since this pull request receives untrusted code, we should **NOT** have any secrets in the environment.
jobs:
upload-patch:
uses: openrewrite/gh-automation/.github/workflows/receive-pr.yml@main
1 change: 0 additions & 1 deletion .java-version

This file was deleted.

4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("org.openrewrite.build.recipe-library") version "latest.release"
kotlin("jvm") version "1.9.0"
kotlin("jvm") version "1.9.22"
}
group = "org.openrewrite"
description = "Rewrite Kotlin"
Expand All @@ -13,7 +13,7 @@ val latest = if (project.hasProperty("releasing")) {
"latest.integration"
}

val kotlinVersion = "1.9.0"
val kotlinVersion = "1.9.22"

dependencies {
annotationProcessor("org.projectlombok:lombok:latest.release")
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/openrewrite/kotlin/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@


import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.openrewrite.*;
import org.openrewrite.internal.ThrowingConsumer;
import org.openrewrite.internal.lang.Nullable;
Expand Down Expand Up @@ -248,7 +247,6 @@ public Space visitSpace(Space space, KSpace.Location loc, Integer integer) {
return next(space);
}

@NotNull
private Space next(Space space) {
if (!space.getComments().isEmpty()) {
return space;
Expand All @@ -264,8 +262,8 @@ public Space visitSpace(Space space, Space.Location loc, Integer integer) {
} else if (loc == Space.Location.IDENTIFIER_PREFIX && parentCursor.getValue() instanceof J.Break &&
((J.Break) parentCursor.getValue()).getLabel() == getCursor().getValue()) {
return space;
} else if (loc == Space.Location.IDENTIFIER_PREFIX && parentCursor.getValue() instanceof K.KReturn &&
((K.KReturn) parentCursor.getValue()).getLabel() == getCursor().getValue()) {
} else if (loc == Space.Location.IDENTIFIER_PREFIX && parentCursor.getValue() instanceof K.Return &&
((K.Return) parentCursor.getValue()).getLabel() == getCursor().getValue()) {
return space;
} else if (loc == Space.Location.LABEL_SUFFIX) {
return space;
Expand Down Expand Up @@ -561,7 +559,7 @@ private boolean isAnnotationField(J.Identifier ident) {
private boolean isValidated(J.Identifier i) {
J j = getCursor().dropParentUntil(it -> it instanceof J).getValue();
// TODO: replace with AnnotationUseSite tree.
return !(j instanceof K.KReturn);
return !(j instanceof K.Return);
}

private boolean isValidated(J.MethodInvocation mi) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.openrewrite.text.PlainText;

@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class FindKotlinSources extends Recipe {
transient KotlinSourceFile kotlinSourceFile = new KotlinSourceFile(this);

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/openrewrite/kotlin/KotlinIsoVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@ public K.FunctionType.Parameter visitFunctionTypeParameter(K.FunctionType.Parame
}

@Override
public K.KReturn visitKReturn(K.KReturn kReturn, P p) {
return (K.KReturn) super.visitKReturn(kReturn, p);
public K.Return visitReturn(K.Return return_, P p) {
return (K.Return) super.visitReturn(return_, p);
}

@Override
public K.KString visitKString(K.KString kString, P p) {
return (K.KString) super.visitKString(kString, p);
public K.StringTemplate visitStringTemplate(K.StringTemplate stringTemplate, P p) {
return (K.StringTemplate) super.visitStringTemplate(stringTemplate, p);
}

@Override
public K.KString.Value visitKStringValue(K.KString.Value value, P p) {
return (K.KString.Value) super.visitKStringValue(value, p);
public K.StringTemplate.Expression visitStringTemplateExpression(K.StringTemplate.Expression expression, P p) {
return (K.StringTemplate.Expression) super.visitStringTemplateExpression(expression, p);
}

@Override
public K.KThis visitKThis(K.KThis kThis, P p) {
return (K.KThis) super.visitKThis(kThis, p);
public K.This visitThis(K.This aThis, P p) {
return (K.This) super.visitThis(aThis, p);
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/openrewrite/kotlin/KotlinParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ public CompiledSource parse(List<Parser.Input> sources, Disposable disposable, E
libraryScope,
compilerConfiguration.get(LOOKUP_TRACKER),
compilerConfiguration.get(ENUM_WHEN_TRACKER),
compilerConfiguration.get(IMPORT_TRACKER),
null, // Do not incrementally compile
emptyList(), // Add extension registrars when needed here.
true,
Expand Down
120 changes: 60 additions & 60 deletions src/main/java/org/openrewrite/kotlin/KotlinVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,65 +178,6 @@ public J visitFunctionTypeParameter(K.FunctionType.Parameter parameter, P p) {
return pa;
}

public J visitKReturn(K.KReturn kReturn, P p) {
K.KReturn r = kReturn;
r = r.withPrefix(visitSpace(r.getPrefix(), KSpace.Location.KRETURN_PREFIX, p));
r = r.withMarkers(visitMarkers(r.getMarkers(), p));
Statement temp = (Statement) visitStatement(r, p);
if (!(temp instanceof K.KReturn)) {
return temp;
} else {
r = (K.KReturn) temp;
}
Expression temp2 = (Expression) visitExpression(r, p);
if (!(temp2 instanceof K.KReturn)) {
return temp2;
} else {
r = (K.KReturn) temp2;
}
r = r.withExpression(visitAndCast(r.getExpression(), p));
r = r.withLabel(visitAndCast(r.getLabel(), p));
return r;
}

public J visitKString(K.KString kString, P p) {
K.KString k = kString;
k = k.withPrefix(visitSpace(k.getPrefix(), KSpace.Location.KSTRING_PREFIX, p));
k = k.withMarkers(visitMarkers(k.getMarkers(), p));
Expression temp = (Expression) visitExpression(k, p);
if (!(temp instanceof K.KString)) {
return temp;
} else {
k = (K.KString) temp;
}
k = k.withStrings(ListUtils.map(k.getStrings(), s -> visit(s, p)));
k = k.withType(visitType(k.getType(), p));
return k;
}

public J visitKThis(K.KThis kThis, P p) {
K.KThis k = kThis;
k = k.withPrefix(visitSpace(k.getPrefix(), KSpace.Location.KTHIS_PREFIX, p));
k = k.withMarkers(visitMarkers(k.getMarkers(), p));
Expression temp = (Expression) visitExpression(k, p);
if (!(temp instanceof K.KThis)) {
return temp;
} else {
k = (K.KThis) temp;
}
k = k.withType(visitType(k.getType(), p));
return k;
}

public J visitKStringValue(K.KString.Value value, P p) {
K.KString.Value v = value;
v = v.withPrefix(visitSpace(v.getPrefix(), KSpace.Location.KSTRING_VALUE_PREFIX, p));
v = v.withMarkers(visitMarkers(v.getMarkers(), p));
v = v.withTree(visit(v.getTree(), p));
v = v.withAfter(visitSpace(v.getAfter(), KSpace.Location.KSTRING_VALUE_AFTER, p));
return v;
}

public J visitListLiteral(K.ListLiteral listLiteral, P p) {
K.ListLiteral l = listLiteral;
l = l.withPrefix(visitSpace(l.getPrefix(), KSpace.Location.LIST_LITERAL_PREFIX, p));
Expand Down Expand Up @@ -289,12 +230,33 @@ public J visitProperty(K.Property property, P p) {
pr = (K.Property) temp;
}

pr = pr.withVariableDeclarations(visitAndCast(pr.getVariableDeclarations(), p));
pr = pr.getPadding().withVariableDeclarations(visitRightPadded(pr.getPadding().getVariableDeclarations(), p));
pr = pr.getPadding().withReceiver(visitRightPadded(pr.getPadding().getReceiver(), p));
pr = pr.withAccessors(visitContainer(pr.getAccessors(), p));
return pr;
}

public J visitReturn(K.Return return_, P p) {
K.Return r = return_;
r = r.withPrefix(visitSpace(r.getPrefix(), KSpace.Location.RETURN_PREFIX, p));
r = r.withMarkers(visitMarkers(r.getMarkers(), p));
Statement temp = (Statement) visitStatement(r, p);
if (!(temp instanceof K.Return)) {
return temp;
} else {
r = (K.Return) temp;
}
Expression temp2 = (Expression) visitExpression(r, p);
if (!(temp2 instanceof K.Return)) {
return temp2;
} else {
r = (K.Return) temp2;
}
r = r.withExpression(visitAndCast(r.getExpression(), p));
r = r.withLabel(visitAndCast(r.getLabel(), p));
return r;
}

public J visitSpreadArgument(K.SpreadArgument spreadArgument, P p) {
K.SpreadArgument s = spreadArgument;
s = s.withPrefix(visitSpace(s.getPrefix(), KSpace.Location.SPREAD_ARGUMENT_PREFIX, p));
Expand All @@ -309,6 +271,44 @@ public J visitSpreadArgument(K.SpreadArgument spreadArgument, P p) {
return s;
}

public J visitStringTemplate(K.StringTemplate stringTemplate, P p) {
K.StringTemplate k = stringTemplate;
k = k.withPrefix(visitSpace(k.getPrefix(), KSpace.Location.STRING_TEMPLATE_PREFIX, p));
k = k.withMarkers(visitMarkers(k.getMarkers(), p));
Expression temp = (Expression) visitExpression(k, p);
if (!(temp instanceof K.StringTemplate)) {
return temp;
} else {
k = (K.StringTemplate) temp;
}
k = k.withStrings(ListUtils.map(k.getStrings(), s -> visit(s, p)));
k = k.withType(visitType(k.getType(), p));
return k;
}

public J visitStringTemplateExpression(K.StringTemplate.Expression expression, P p) {
K.StringTemplate.Expression v = expression;
v = v.withPrefix(visitSpace(v.getPrefix(), KSpace.Location.STRING_TEMPLATE_EXPRESSION_PREFIX, p));
v = v.withMarkers(visitMarkers(v.getMarkers(), p));
v = v.withTree(visit(v.getTree(), p));
v = v.withAfter(visitSpace(v.getAfter(), KSpace.Location.STRING_TEMPLATE_EXPRESSION_AFTER, p));
return v;
}

public J visitThis(K.This aThis, P p) {
K.This k = aThis;
k = k.withPrefix(visitSpace(k.getPrefix(), KSpace.Location.THIS_PREFIX, p));
k = k.withMarkers(visitMarkers(k.getMarkers(), p));
Expression temp = (Expression) visitExpression(k, p);
if (!(temp instanceof K.This)) {
return temp;
} else {
k = (K.This) temp;
}
k = k.withType(visitType(k.getType(), p));
return k;
}

public J visitTypeAlias(K.TypeAlias typeAlias, P p) {
K.TypeAlias t = typeAlias;
t = t.withPrefix(visitSpace(t.getPrefix(), KSpace.Location.TYPE_ALIAS_PREFIX, p));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/openrewrite/kotlin/RenameTypeAlias.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.openrewrite.kotlin.tree.K;

@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class RenameTypeAlias extends Recipe {

@Option(displayName = "Old alias name",
Expand Down Expand Up @@ -56,7 +56,7 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new KotlinIsoVisitor<ExecutionContext>() {
@Override
public K.TypeAlias visitTypeAlias(K.TypeAlias typeAlias, ExecutionContext executionContext) {
public K.TypeAlias visitTypeAlias(K.TypeAlias typeAlias, ExecutionContext ctx) {
if (!aliasName.equals(typeAlias.getSimpleName()) || !TypeUtils.isOfClassType(typeAlias.getType(), fullyQualifiedAliasedType)) {
return typeAlias;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.*;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
Expand All @@ -34,22 +32,24 @@
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

import static org.openrewrite.Tree.randomId;

@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class EqualsMethodUsage extends Recipe {
@Nullable
private static J.Binary equalsBinaryTemplate;

@Override
public String getDisplayName() {
return "Structural equality tests should use \"==\" or \"!=\"";
return "Structural equality tests should use `==` or `!=`";
}

@Override
public String getDescription() {
return "In Kotlin, `==` means structural equality and `!=` structural inequality and both map to the left-side " +
"term’s `equals()` function. It is, therefore, redundant to call `equals()` as a function. Also, `==` and `!=`" +
" are more general than `equals()` and `!equals()` because it allows either of both operands to be null.\n" +
" are more general than `equals()` and `!equals()` because it allows either of both operands to be `null`.\n" +
"Developers using `equals()` instead of `==` or `!=` is often the result of adapting styles from other " +
"languages like Java, where `==` means reference equality and `!=` means reference inequality.\n" +
"The `==` and `!=` operators are a more concise and elegant way to test structural equality than calling a function.";
Expand Down Expand Up @@ -103,8 +103,10 @@ public J visitMethodInvocation(J.MethodInvocation method,
) {
Expression lhs = method.getSelect();
Expression rhs = method.getArguments().get(0);
getCursor().getParentTreeCursor().putMessage("replaced", true);
return buildEqualsBinary(lhs, rhs);
Cursor parentCursor = getCursor().getParentTreeCursor();
parentCursor.putMessage("replaced", true);
J.Binary binary = buildEqualsBinary(lhs, rhs);
return parentCursor.getValue() instanceof J.Block ? new K.ExpressionStatement(randomId(), binary) : binary;
}
return method;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@


@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class ImplicitParameterInLambda extends Recipe {
@Override
public String getDisplayName() {
return "\"it\" shouldn't be used as a lambda parameter name";
return "`it` shouldn't be used as a lambda parameter name";
}

@Override
public String getDescription() {
return "\"it\" is a special identifier that allows you to refer to the current parameter being passed to a " +
return "`it` is a special identifier that allows you to refer to the current parameter being passed to a " +
"lambda expression without explicitly naming the parameter." +
" Lambda expressions are a concise way of writing anonymous functions. Many lambda expressions have " +
"only one parameter, when this is true the compiler can determine the parameter type by context. Thus " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.openrewrite.kotlin.format.TrailingCommaVisitor;

@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class RemoveTrailingComma extends Recipe {
@Override
public String getDisplayName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@


@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class RemoveTrailingSemicolon extends Recipe {
@Override
public String getDisplayName() {
Expand Down Expand Up @@ -72,7 +72,7 @@ public <M extends Marker> M visitMarker(Marker marker, ExecutionContext ctx) {
}

@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
private static class CollectSemicolonRemovableElements extends KotlinPrinter<Set<Marker>> {
Pattern WS = Pattern.compile("^\\s+");

Expand Down
Loading

0 comments on commit abc2e45

Please sign in to comment.