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

Add BufferedWriterCreation recipe & SimplifyConstantTernaryExecution #258

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

JLLeitschuh
Copy link
Contributor

  • BufferedWriterCreation
  • SimplifyConstantTernaryExecution

Signed-off-by: Jonathan Leitschuh Jonathan.Leitschuh@gmail.com

What's your motivation?

More recipes, more better?

Anything in particular you'd like reviewers to focus on?

There seems to be a bug in autoFormat where the J.Literal isn't having it's prefix padding removed.

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@JLLeitschuh
Copy link
Contributor Author

This PR is currently failing. It seems that autoFormat isn't working correctly in the SimplifyConstantTernaryExecution, and I have no idea what I'm doing wrong. Any insights here would be greatly appreciated.

@JLLeitschuh
Copy link
Contributor Author

Also, waiting on openrewrite/rewrite#4015 to land too for this PR

Comment on lines 49 to 53
public J visitTernary(J.Ternary ternary, ExecutionContext executionContext) {
J.Ternary t = (J.Ternary) super.visitTernary(ternary, executionContext);
Expression condition =
SimplifyConstantIfBranchExecution.cleanupBooleanExpression(
t.getCondition(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public J visitTernary(J.Ternary ternary, ExecutionContext executionContext) {
J.Ternary t = (J.Ternary) super.visitTernary(ternary, executionContext);
Expression condition =
SimplifyConstantIfBranchExecution.cleanupBooleanExpression(
t.getCondition(),
public J visitTernary(J.Ternary ternary, ExecutionContext ctx) {
J.Ternary t = (J.Ternary) super.visitTernary(ternary, ctx);
ctx
return autoFormat(t.getTruePart(), ctx);
return autoFormat(t.getFalsePart(), ctx);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely wrong @timtebeek

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well that's disappointing; thanks for calling it out!
The underlying patch file looks ok-ish to me at first glance

diff --git a/src/main/java/org/openrewrite/staticanalysis/SimplifyConstantTernaryExecution.java b/src/main/java/org/openrewrite/staticanalysis/SimplifyConstantTernaryExecution.java
index d98e472..41983c2 100644
--- a/src/main/java/org/openrewrite/staticanalysis/SimplifyConstantTernaryExecution.java
+++ b/src/main/java/org/openrewrite/staticanalysis/SimplifyConstantTernaryExecution.java
@@ -46,18 +46,18 @@ public class SimplifyConstantTernaryExecution extends Recipe {
     public TreeVisitor<?, ExecutionContext> getVisitor() {
         JavaVisitor<ExecutionContext> v = new JavaVisitor<ExecutionContext>() {
             @Override
-            public J visitTernary(J.Ternary ternary, ExecutionContext executionContext) {
-                J.Ternary t = (J.Ternary) super.visitTernary(ternary, executionContext);
+            public J visitTernary(J.Ternary ternary, ExecutionContext ctx) {
+                J.Ternary t = (J.Ternary) super.visitTernary(ternary, ctx);
                 Expression condition =
                         SimplifyConstantIfBranchExecution.cleanupBooleanExpression(
                                 t.getCondition(),
                                 getCursor(),
-                                executionContext
+                                ctx
                         );
                 if (J.Literal.isLiteralValue(condition, true)) {
-                    return autoFormat(t.getTruePart(), executionContext);
+                    return autoFormat(t.getTruePart(), ctx);
                 } else if (J.Literal.isLiteralValue(condition, false)) {
-                    return autoFormat(t.getFalsePart(), executionContext);
+                    return autoFormat(t.getFalsePart(), ctx);
                 }
                 return t;
             }

So then it must be https://github.com/googleapis/code-suggester that's somehow thrown off. Unless you spot anything else in the diff above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks otherwise fine. Sounds like a fun bug to fix


@Override
public String getDescription() {
return "Checks for ternary expressions that are always `true` or `false` and simplifies them.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add this to the SimplifyTernary Refaster recipe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not if I want to include the call to cleanupBooleanExpression

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we were to make sure the existing SimplifyBooleanExpressionVisitor can handle this case, then we should be fine, as we have the dedicated SIMPLIFY_BOOLEANS embedding option for that.

 - BufferedWriterCreation
 - SimplifyConstantTernaryExecution

Signed-off-by: Jonathan Leitschuh <Jonathan.Leitschuh@gmail.com>
@JLLeitschuh JLLeitschuh force-pushed the feat/JLL/buffered-writer-creation-and-simplify-ternary branch from 1ef0e73 to 56a8440 Compare February 17, 2024 17:34
"""
class Test {
void test() {
System.out.println("foo");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@knutwannheden this test is failing and is currently generating a change that looks like this:

System.out.println( "foo");

instead of the expected:

System.out.println("foo");

Same issue with the test above. Not sure what's going on here. Is this something broken with the AutoFormat visitor?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be. I can investigate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "problem" here is that what gets auto-formatted is the true or false branch of the ternary only. In an of itself it isn't incorrectly formatted and removing the whitespace prefix would require checking what it is contained in.

While it might be possible to change how auto-formatting works (although that currently seems quite difficult, since our cursor doesn't know which "feature" of the containing element is currently being visited), I think we should here instead just format the containing element. I've updated the PR accordingly.

@timtebeek timtebeek changed the title Add Two Recipes Add BufferedWriterCreation recipe & SimplifyConstantTernaryExecution Jul 28, 2024
@timtebeek timtebeek self-requested a review July 28, 2024 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Ready to Review
Development

Successfully merging this pull request may close these issues.

3 participants