Skip to content

Commit

Permalink
Improvements to NamedParameterChecker
Browse files Browse the repository at this point in the history
Provide a more descriptor diagnostic message that includes the
comment and formal parameter name, to be more helpful in
contexts where the suggested fix isn't surfaced.

Also accept all comment styles as long as they match the formal
parameter name, instead of reporting findings to e.g. rewrite
`/*foo*/ foo` to `/* foo= */ foo`.

MOE_MIGRATED_REVID=163704182
  • Loading branch information
cushon committed Aug 10, 2017
1 parent 912b153 commit 6bf5888
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
Expand Up @@ -105,8 +105,9 @@ private Description matchNewClassOrMethodInvocation(
addComment(labelledArgument, fixBuilder); addComment(labelledArgument, fixBuilder);
incorrectParameterDescriptions.add( incorrectParameterDescriptions.add(
String.format( String.format(
"%s (comment does not conform to required style)", "`%s` should be `%s <arg>`",
labelledArgument.parameterName())); labelledArgument.matchedComment().comment().getText(),
NamedParameterComment.toCommentText(labelledArgument.parameterName())));
break; break;
case BAD_MATCH: case BAD_MATCH:
Comment badLabel = labelledArgument.matchedComment().comment(); Comment badLabel = labelledArgument.matchedComment().comment();
Expand Down Expand Up @@ -137,8 +138,8 @@ private Description matchNewClassOrMethodInvocation(
} }
incorrectParameterDescriptions.add( incorrectParameterDescriptions.add(
String.format( String.format(
"%s (comment does not match formal parameter name)", "`%s` does not match formal parameter name `%s`",
labelledArgument.parameterName())); badLabel.getText(), labelledArgument.parameterName()));
break; break;
} }
} }
Expand Down
Expand Up @@ -147,7 +147,16 @@ static MatchedComment match(Commented<ExpressionTree> actual, String formal) {
.findFirst(); .findFirst();


if (approximateMatchComment.isPresent()) { if (approximateMatchComment.isPresent()) {
return MatchedComment.create(approximateMatchComment.get(), MatchType.APPROXIMATE_MATCH); // Report EXACT_MATCH for comments that don't use the recommended style (e.g. `/*foo*/`
// instead of `/* foo= */`), but which match the formal parameter name exactly, since it's
// a style nit rather than a possible correctness issue.
// TODO(cushon): revisit this if we standardize on the recommended comment style.
String text =
CharMatcher.anyOf("=:")
.trimTrailingFrom(Comments.getTextFromComment(approximateMatchComment.get()).trim());
return MatchedComment.create(
approximateMatchComment.get(),
text.equals(formal) ? MatchType.EXACT_MATCH : MatchType.APPROXIMATE_MATCH);
} }


return MatchedComment.notAnnotated(); return MatchedComment.notAnnotated();
Expand Down
Expand Up @@ -108,44 +108,58 @@ public void namedParametersChecker_reformatsComment_onRequiredNamesMethod() {
} }


@Test @Test
public void namedParametersChecker_reformatsComment_withNoEquals() { public void namedParametersChecker_tolerateComment_withNoEquals() {
compilationHelper compilationHelper
.addSourceLines( .addSourceLines(
"Test.java", "Test.java",
"abstract class Test {", "abstract class Test {",
" abstract void target(Object param);", " abstract void target(Object param);",
" void test(Object arg) {", " void test(Object arg) {",
" // BUG: Diagnostic contains: target(/* param= */arg)",
" target(/*param*/arg);", " target(/*param*/arg);",
" }", " }",
"}") "}")
.doTest(); .doTest();
} }


@Test @Test
public void namedParametersChecker_reformatsComment_blockAfter() { public void namedParametersChecker_toleratesMatchingComment_blockAfter() {
compilationHelper compilationHelper
.addSourceLines( .addSourceLines(
"Test.java", "Test.java",
"abstract class Test {", "abstract class Test {",
" abstract void target(Object param);", " abstract void target(Object param);",
" void test(Object arg) {", " void test(Object arg) {",
" // BUG: Diagnostic contains: target(/* param= */arg)",
" target(arg/*param*/);", " target(arg/*param*/);",
" }", " }",
"}") "}")
.doTest(); .doTest();
} }


@Test @Test
public void namedParametersChecker_reformatsMatchingComment_lineAfter() { public void namedParametersChecker_refactorsComment_blockAfter() {
compilationHelper
.addSourceLines(
"Test.java",
"abstract class Test {",
" abstract void target(Object param);",
" void test(Object arg) {",
" // BUG: Diagnostic contains:",
" // target(/* param= */arg)",
" // `/*imprecise match for param*/` should be `/* param= */ <arg>`",
" target(arg/*imprecise match for param*/);",
" }",
"}")
.doTest();
}

@Test
public void namedParametersChecker_toleratesMatchingComment_lineAfter() {
compilationHelper compilationHelper
.addSourceLines( .addSourceLines(
"Test.java", "Test.java",
"abstract class Test {", "abstract class Test {",
" abstract void target(Object param);", " abstract void target(Object param);",
" void test(Object arg) {", " void test(Object arg) {",
" // BUG: Diagnostic contains: target(/* param= */arg)",
" target(arg); //param", " target(arg); //param",
" }", " }",
"}") "}")
Expand Down Expand Up @@ -219,7 +233,9 @@ public void namedParametersChecker_suggestsChangeComment_whenNoMatchingNames() {
"abstract class Test {", "abstract class Test {",
" abstract void target(Object param1, Object param2);", " abstract void target(Object param1, Object param2);",
" void test(Object arg1, Object arg2) {", " void test(Object arg1, Object arg2) {",
" // BUG: Diagnostic contains: target(/* param1= */arg1, arg2)", " // BUG: Diagnostic contains:",
" // target(/* param1= */arg1, arg2)",
" // `/* notMatching= */` does not match formal parameter name `param1`",
" target(/* notMatching= */arg1, arg2);", " target(/* notMatching= */arg1, arg2);",
" }", " }",
"}") "}")
Expand Down

0 comments on commit 6bf5888

Please sign in to comment.