-
Notifications
You must be signed in to change notification settings - Fork 70
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
Conversation
There was a problem hiding this 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
01f25d9
to
edfda4f
Compare
There was a problem hiding this 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
edfda4f
to
a4b7233
Compare
There was a problem hiding this 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
8955931
to
586cf50
Compare
586cf50
to
e6f509e
Compare
There was a problem hiding this 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
There was a problem hiding this 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
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(); | ||
} |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
if (last) { | ||
blockParentCursor.putMessage("removeEndComments", true); | ||
} else { | ||
blockParentCursor.<List<Integer>>computeMessageIfAbsent("replaced", k -> new ArrayList<>()).add(currentIndex); |
There was a problem hiding this comment.
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
Boolean removeEndComments = getCursor().pollMessage("removeEndComments"); | ||
if (removeEndComments != null) { | ||
bl = bl.withEnd(bl.getEnd().withComments(Collections.emptyList())); | ||
bl = maybeAutoFormat(block, bl, ctx); | ||
} |
There was a problem hiding this comment.
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
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()); | ||
} | ||
|
There was a problem hiding this comment.
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
}
Space prefix = elem.getPrefix(); | ||
Statement body = elem.getBody(); | ||
|
||
if(!prefix.getComments().isEmpty() && prefix.getWhitespace().contains("\n")) { | ||
body = body.withPrefix(prefix); | ||
} |
There was a problem hiding this comment.
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;
1dd2db0
to
3209dcd
Compare
There was a problem hiding this 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
Other than this 1 comment, it looks good to me. |
Co-authored-by: Jente Sondervorst <[email protected]>
There was a problem hiding this 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!
There was a problem hiding this 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
There was a problem hiding this 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.
src/test/java/org/openrewrite/staticanalysis/NeedBracesTest.java
Outdated
Show resolved
Hide resolved
} else { | ||
/* | ||
* comment should be in else block | ||
*/ | ||
return; | ||
} |
There was a problem hiding this comment.
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.
} else { | |
/* | |
* comment should be in else block | |
*/ | |
return; | |
} | |
} | |
/* | |
* comment should be in else block | |
*/ | |
else { | |
return; | |
} |
There was a problem hiding this 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
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?
NeedsBraces
deletes comments, or moves comments around in confusing, inconsistent way #315Anything 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