Skip to content

Fix Comment handling in NeedsBraces #518

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

Merged
merged 6 commits into from
May 4, 2025

Conversation

Jammy-Louie
Copy link
Contributor

@Jammy-Louie Jammy-Louie commented Apr 16, 2025

What's changed?

Improve comment handling for NeedBraces recipe for if statements. Comments would be placed incorrectly or dropped if they trailed the if statement.

What's your motivation?

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

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

@Jammy-Louie Jammy-Louie self-assigned this Apr 16, 2025
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Apr 16, 2025
@Jammy-Louie Jammy-Louie added the bug Something isn't working label Apr 16, 2025
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/test/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethodsTest.java
    • lines 101-102

@Jammy-Louie Jammy-Louie force-pushed the issue-315-NeedsBraces-fix-comments branch from 01f25d9 to edfda4f Compare April 17, 2025 13:10
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/test/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethodsTest.java
    • lines 101-102

@Jammy-Louie Jammy-Louie force-pushed the issue-315-NeedsBraces-fix-comments branch from edfda4f to a4b7233 Compare April 17, 2025 13:18
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/test/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethodsTest.java
    • lines 101-102

@Jammy-Louie Jammy-Louie force-pushed the issue-315-NeedsBraces-fix-comments branch from 8955931 to 586cf50 Compare April 22, 2025 14:05
@Jammy-Louie Jammy-Louie force-pushed the issue-315-NeedsBraces-fix-comments branch from 586cf50 to e6f509e Compare April 22, 2025 14:17
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceof.java
    • lines 36-37

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceof.java
    • lines 36-37

Comment on lines +76 to +88
Cursor currentCursor = getCursor();
while (
currentCursor != null &&
currentCursor.getParent() != null &&
!(currentCursor.getParent().getValue() instanceof J.Block)
) {
currentCursor = currentCursor.getParent();
}

if (currentCursor != null && currentCursor.getValue() instanceof JRightPadded) {
JRightPadded<J> paddedIf = currentCursor.getValue();
rootElement = paddedIf.getElement();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

need to find the root element because we can run into nested elements such as

if(true) { return ; }
else if (true) { return ; }
else if (true) { return ; }
else return

The else block would be nested at the bottom of the of the If LST and we would need to find the root If statement because that is the Statement in the J.Block

@@ -69,20 +69,42 @@ private static class NeedBracesVisitor extends JavaIsoVisitor<ExecutionContext>
* We can use that to our advantage by saying if you aren't a block (e.g. a single {@link Statement}, etc.),
* then we're going to make this into a block. That's how we'll get the code bodies surrounded in braces.
*/
private <T extends Statement> J.Block buildBlock(Statement owner, T element) {
J j = getCursor().getParentTreeCursor().getValue();
private <T extends Statement> J.Block buildBlock(T element) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

remove owner as an argument because it is no longer needed

Comment on lines +100 to +103
if (last) {
blockParentCursor.putMessage("removeEndComments", true);
} else {
blockParentCursor.<List<Integer>>computeMessageIfAbsent("replaced", k -> new ArrayList<>()).add(currentIndex);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

messages can appear at the end of the block for cases such as.

static void method() {
    if (true) return; // if comment
    else return; // else comment
}

for this case the // else comment would be located at the end of the block

Comment on lines 136 to 140
Boolean removeEndComments = getCursor().pollMessage("removeEndComments");
if (removeEndComments != null) {
bl = bl.withEnd(bl.getEnd().withComments(Collections.emptyList()));
bl = maybeAutoFormat(block, bl, 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.

remove comments when the trailing comments found at the end of the block

Comment on lines +171 to +178
if (elem.getElsePart() != null && !elem.getElsePart().getPrefix().getComments().isEmpty()) {
Space end = elem.getElsePart().getPrefix();
elem = elem.withElsePart(elem.getElsePart().withPrefix(Space.EMPTY));
b = buildBlock(elem.getThenPart()).withEnd(end);
} else {
b = buildBlock(elem.getThenPart());
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

trailing comments for if statements can appear as a prefix in the else

example:

static void method() {
    if (true) return; // if comment
    else return; // else comment
}

Comment on lines 193 to 198
Space prefix = elem.getPrefix();
Statement body = elem.getBody();

if(!prefix.getComments().isEmpty() && prefix.getWhitespace().contains("\n")) {
body = body.withPrefix(prefix);
}
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 handles the case where braces appear on the if statement but not the else

if (true) {
    return;
} 
/*
* do something
/*
else return;

@Jammy-Louie Jammy-Louie force-pushed the issue-315-NeedsBraces-fix-comments branch from 1dd2db0 to 3209dcd Compare April 22, 2025 14:48
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceof.java
    • lines 36-37

@Jammy-Louie
Copy link
Contributor Author

testing locally on openrewrite

only 1 change suggestion and appears to be correct

Screenshot 2025-04-22 at 11 10 53 AM

@Jammy-Louie
Copy link
Contributor Author

Jammy-Louie commented Apr 22, 2025

testing locally on apache

Suggested changes appear to be correct for Java Source files.

Sample change:

Screenshot 2025-04-22 at 11 13 58 AM

although there does appear to be issues when running against Javascript files:

Screenshot 2025-04-22 at 11 17 11 AM

Issue appears to be present before my changes as I've tested the recipe from main and still see the same results.

@Jammy-Louie Jammy-Louie marked this pull request as ready for review April 22, 2025 15:27
@Jammy-Louie Jammy-Louie moved this from In Progress to Ready to Review in OpenRewrite Apr 22, 2025
@timtebeek timtebeek self-requested a review May 3, 2025 11:46
@Jenson3210
Copy link
Contributor

Other than this 1 comment, it looks good to me.
Glad to see this coverage of tests added as prefixes/comments removal could be easily overlooked in a PR generated by a recipe.

github-actions[bot]

This comment was marked as off-topic.

Copy link
Contributor

@Jenson3210 Jenson3210 left a comment

Choose a reason for hiding this comment

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

I see @timtebeek has pushed my proposed change, so other that this LGTM!

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/resources/META-INF/rewrite/examples.yml
    • lines 57-56
    • lines 634-633
    • lines 679-695
    • lines 1228-1247
    • lines 1280-1279
    • lines 2309-2308
    • lines 2324-2336
    • lines 3442-3453
    • lines 3467-3472

Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Some optional suggestions, fully realize these might be awkward to make work, and this is already a good improvement, so we need not drag it out. Could be worth briefly exploring before a merge.

Comment on lines +387 to +392
} else {
/*
* comment should be in else block
*/
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, I had expected the following to minimize the diff with what was there before. All we ought to be doing is adding braces, where possible.

Suggested change
} else {
/*
* comment should be in else block
*/
return;
}
}
/*
* comment should be in else block
*/
else {
return;
}

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/resources/META-INF/rewrite/examples.yml
    • lines 57-56
    • lines 634-633
    • lines 679-695
    • lines 1228-1247
    • lines 1280-1279
    • lines 2309-2308
    • lines 2324-2336
    • lines 3442-3453
    • lines 3467-3472

@timtebeek timtebeek merged commit 60fb46d into main May 4, 2025
1 of 2 checks passed
@timtebeek timtebeek deleted the issue-315-NeedsBraces-fix-comments branch May 4, 2025 21:04
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite May 4, 2025
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.

NeedsBraces deletes comments, or moves comments around in confusing, inconsistent way
4 participants