Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visit multiple elements for diverse before templates #117

Merged
merged 4 commits into from
Nov 24, 2024

Conversation

timtebeek
Copy link
Contributor

@timtebeek timtebeek commented Nov 23, 2024

What's your motivation?

Before this change the order of before templates determined which visit method (singular) to generate, and searched for all before templates from that single visit method. This is not correct, and leads to some before templates not matching when they would require to visit a different element.

Have you considered any alternatives or workarounds?

Not compile refaster templates that would require us to visit different elements.

Any additional context

@timtebeek timtebeek marked this pull request as ready for review November 23, 2024 13:49
@timtebeek timtebeek self-assigned this Nov 23, 2024
@timtebeek timtebeek added the bug Something isn't working label Nov 23, 2024
@timtebeek timtebeek merged commit ab684fd into main Nov 24, 2024
1 check passed
@timtebeek timtebeek deleted the visit-multiple-elements branch November 24, 2024 10:58
@timtebeek
Copy link
Contributor Author

Confirmed that this recipe now makes the tests in rewrite-recipe-starter pass

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import org.openrewrite.java.template.RecipeDescriptor;

@RecipeDescriptor(
        name = "Standardize empty String checks",
        description = "Replace calls to `String.length() == 0` with `String.isEmpty()`."
)
public class StringIsEmpty {

    @BeforeTemplate
    boolean lengthIsZero(String s) {
        return s.length() == 0;
    }

    @BeforeTemplate
    boolean zeroIsLength(String s) {
        return 0 == s.length();
    }

    @BeforeTemplate
    boolean equalsEmptyString(String s) {
        return s.equals("");
    }

    @BeforeTemplate
    boolean emptyStringEquals(String s) {
        return "".equals(s);
    }

    @BeforeTemplate
    boolean lessThanOne(String s) {
        return s.length() < 1;
    }

    @BeforeTemplate
    boolean oneGreaterThanLength(String s) {
        return 1 > s.length();
    }

    @AfterTemplate
    boolean isEmpty(String s) {
        return s.isEmpty();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

StringIsEmpty with Refaster test fails
1 participant