-
Notifications
You must be signed in to change notification settings - Fork 39
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
Introduce ErrorProneTestHelperSourceFormat
check
#147
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.
While working on the tests for this new checker I already saved a lot of time just running patch.sh
. 😄
@@ -52,7 +52,7 @@ public Description matchMethod(MethodTree tree, VisitorState state) { | |||
|
|||
Optional<Fix> fix = tryFixOrdering(originalOrdering, sortedAnnotations, state); | |||
|
|||
Description.Builder description = buildDescription(tree); | |||
Description.Builder description = buildDescription(originalOrdering.get(0)); |
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 way this check reports the violation on the top-most annotation, which arguably is a better location. (Even better could be to report on the first out-of-order annotation, but not sure that's worth the hassle.)
@@ -124,7 +124,7 @@ void identificationWithinBinaryOperation() { | |||
" s + String.valueOf((String) null),", | |||
" s + String.valueOf(null),", | |||
" s + String.valueOf(new char[0]),", | |||
"", | |||
" //", |
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 one and the one below are introduced to retain the original grouping. Seemed worthwhile.
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.
Still couldn't figure out how to get the patch command to work with a self-apply on EPS. Would really love to see that working first. Would be nice if we could add that in the README.
This will make quite some people happy.
The code is really clean and nice IMO. Could find anything 🚀
import java.util.Optional; | ||
|
||
/** | ||
* A {@link BugChecker} which flags and corrects improperly formatted Error Prone test code. |
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.
Since this is quite a specific check, I'd say it makes sense to at least mention GJF and perhaps refer to their docs?
Could be like a second paragraph? I like the explanation but it feels that GJF is missing from here.
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.
Good point; added a sentence. (Linking to the correct/least ambiguous docs is tricky, so opted for mere Javadoc instead.
int startPos = ASTHelpers.getStartPosition(sourceLines.get(0)); | ||
int endPos = state.getEndPosition(sourceLines.get(sourceLines.size() - 1)); | ||
|
||
/* Attempt to format the source code only if it fully consists of constant expression. */ |
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.
/* Attempt to format the source code only if it fully consists of constant expression. */ | |
/* Attempt to format the source code only if it fully consists of constant expressions. */ |
Right? As in, every line needs to be a constant.
return Description.NO_MATCH; | ||
} | ||
|
||
List<? extends ExpressionTree> sourceLines = |
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.
Don't want to validate the name in a way? This is valid with the current setup:
.addSourceLines(\"D.java\", \"class C {}\")"
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.
OTOH, I can see why the in/
and out/
setup can make things a bit harder. (Perhaps it's out of scope)
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 like the idea of validating this, but indeed would like to defer that to another checker.
@@ -26,71 +26,137 @@ void identification() { | |||
"import org.junit.jupiter.params.ParameterizedTest;", | |||
"", | |||
"class A {", | |||
" @BeforeAll void setUp1() {}", | |||
" {", |
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 fail to understand where this is coming from 👀.
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.
It's to make sure that the static arguments
import isn't removed by the formatter. We need to keep it because it's important for one of the tests below:
// BUG: Diagnostic contains: (but note that
arguments
is already statically imported)
b91b16f
to
223a393
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.
Rebased and added a commit.
Still couldn't figure out how to get the patch command to work with a self-apply on EPS. Would really love to see that working first.
I filed #171.
import java.util.Optional; | ||
|
||
/** | ||
* A {@link BugChecker} which flags and corrects improperly formatted Error Prone test code. |
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.
Good point; added a sentence. (Linking to the correct/least ambiguous docs is tricky, so opted for mere Javadoc instead.
return Description.NO_MATCH; | ||
} | ||
|
||
List<? extends ExpressionTree> sourceLines = |
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 like the idea of validating this, but indeed would like to defer that to another checker.
@@ -26,71 +26,137 @@ void identification() { | |||
"import org.junit.jupiter.params.ParameterizedTest;", | |||
"", | |||
"class A {", | |||
" @BeforeAll void setUp1() {}", | |||
" {", |
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.
It's to make sure that the static arguments
import isn't removed by the formatter. We need to keep it because it's important for one of the tests below:
// BUG: Diagnostic contains: (but note that
arguments
is already statically imported)
223a393
to
99771b2
Compare
99771b2
to
1789863
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.
Still think this is a really cool check 🚀🚀🚀.
Verified that it works by using the new .patch.sh
😄, works like a charm!
Made two tiny tweaks to the release note. Without the |
As-is the text needs to be reflowed. The |
^ Made a new tweak, PTAL :). |
With "instances of" we should not have the |
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.
Super super nice that we're going to have this now 👍 No more formatting discussions on test code content! Tried very hard to squeeze two comments out but approving regardless 👍
import java.util.Optional; | ||
|
||
/** | ||
* A {@link BugChecker} which flags and corrects improperly formatted Error Prone test code. |
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.
It is -XepPatchChecks
that does the correcting right? IIUC, this BugChecker just flags it. Comitted suggestion.
* A {@link BugChecker} which flags and corrects improperly formatted Error Prone test code. | |
* A {@link BugChecker} which flags improperly formatted Error Prone test code. |
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.
Nice, this is consistent with other checks indeed.
String withReorderedImports = ImportOrderer.reorderImports(source, Style.GOOGLE); | ||
String withRemovedImports = |
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.
The name of withReorderedImports
makes sense, but withRemovedImports
felt odd at the formatSource
line as it intuitively indicates that imports were removed if applicable, while this is not the case if retainUnusedImports
is true. However, I really don't have an alternative apart from adding 'optionally' to it, which I don't like either. Placing comment to poke your brains 🧠
String withReorderedImports = ImportOrderer.reorderImports(source, Style.GOOGLE); | |
String withRemovedImports = | |
String withReorderedImports = ImportOrderer.reorderImports(source, Style.GOOGLE); | |
String withOptionallyRemovedImports = |
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.
TBH, withOptionallyRemovedImports
explains exactly what is going on here, so I kinda like it.
One other option could be something like: resultingImports
or finalImports
. To show that the variable will hold the "end result (a set of imports) that will be passed to the actual formatter".
Perhaps a synonym of final
or result
would be better here.
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.
Like those suggestions, as long as we keep the with
part, as it involves the entire source code. We could also do withExpectedReorderedImports
where what we expect depends on the value of the parameter.
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 think I like withOptionallyRemovedImports
the most actually. That one is most clear/accurate/honest about what is happening IMO.
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.
Committed. Feel free to further tweak @Stephan202.
Splitter.on('\n') | ||
.splitToStream(formatted) | ||
.map(state::getConstantExpression) | ||
.collect(joining(", ")))); |
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.
Hehe ironically, this leaves potentially unformatted code 😄 But nice that apply-error-prone-suggestion.sh
will also run fmt:format
to tackle this 💪
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.
Hmm, I think I'm missing that case, when will that happen 👀? Anyway, indeed nice it's already caught by the .sh
.
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 replaces the entire existing argument array with the result of the formatted source code split on newlines, Stringified (constant expression), joined with ,
. This puts it all on a single line. You can test this combining line 60 - 62 in RequestParamTypeCheckTest.java
(so new statement straight after ;
). Then run apply-error-prone-suggestions.sh
but without the fmt:format
in there. Now you can see the output is that the entire argument array is on a single line. After running mvn fmt:format
, everything looks as expected.
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.
Ahh ofcourse, nice, good catch 😄.
This new checker inspects inline code passed to `CompilationTestHelper` and `BugCheckerRefactoringTestHelper`. It requires that this code is properly formatted and that its imports are organized. Only code that represents the expected output of a refactoring operation is allowed to have unused imports, as most `BugChecker`s do not (and are not able to) remove imports that become obsolete as a result of applying their suggested fix(es).
c785a02
to
37be08a
Compare
I can live with that new name ;). Rebased; I think we can build if the PR passes. |
I've seen this being a topic of discussion several times (most recently here), so decided to do something about it. PTAL.
Suggested commit message: