Skip to content

Commit

Permalink
fix issue google#2709
Browse files Browse the repository at this point in the history
  • Loading branch information
jingjing-0919 committed Apr 24, 2022
1 parent afda5fd commit 933cc17
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,13 @@ public List<ErrorProneToken> getOffsetTokens(int start, int end) {
getSourceCode().subSequence(start, end).toString(), start, context);
}

/** Returns the start position of the node. */
public int getStartPosition(Tree node) {
JCCompilationUnit compilationUnit = (JCCompilationUnit) getPath().getCompilationUnit();
return ((JCTree) node).getStartPosition();
}


/** Returns the end position of the node, or -1 if it is not available. */
public int getEndPosition(Tree node) {
JCCompilationUnit compilationUnit = (JCCompilationUnit) getPath().getCompilationUnit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,21 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) {
if (statements != null && !statements.isEmpty()) {
return NO_MATCH;
}

if (defaultCase.getCaseKind() == CaseTree.CaseKind.RULE && defaultCase.getBody() != null ){
if (!defaultCase.getBody().toString().equals("{\r\n}")){
return NO_MATCH;
}
}

// If `default` case is empty, and last in switch, add `// fall out` comment
// TODO(epmjohnston): Maybe move comment logic to https://errorprone.info/bugpattern/FallThrough
int idx = tree.getCases().indexOf(defaultCase);
if (idx != tree.getCases().size() - 1) {
return NO_MATCH;
}
if (state
.getOffsetTokens(state.getEndPosition(defaultCase), state.getEndPosition(tree))
.getOffsetTokens(state.getStartPosition(defaultCase), state.getEndPosition(tree))
.stream()
.anyMatch(t -> !t.comments().isEmpty())) {
return NO_MATCH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,144 @@ public void arrowSwitchNegative() {
"}")
.doTest();
}


@Test
public void arrowSwitchWithoutComment(){
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"public class Test {",
" public static void nothing1(){ }",
" public static void nothing2(){ }",
" public static void nothing3(){ }",
" public static void foo(int i) {",
" switch (i) {",
" case 1 -> nothing1();",
" case 2 -> nothing2();",
" default -> nothing3();",
" }",
" }",
"}")
.doTest();
}


@Test
public void arrowSwitchCommentAboveBraces(){
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"public class Test {",
" public static void nothing1(){ }",
" public static void nothing2(){ }",
" public static void nothing3(){ }",
" public static void foo(int i) {",
" switch (i) {",
" case 1 -> nothing1();",
" case 2 -> nothing2();",
" //Here is a comment",
" default -> {",
" nothing3();",
" }",
" }",
" }",
"}")
.doTest();
}

@Test
public void arrowSwitchCommentInMiddle(){
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"public class Test {",
" public static void nothing1(){ }",
" public static void nothing2(){ }",
" public static void nothing3(){ }",
" public static void foo(int i) {",
" switch (i) {",
" case 1 -> nothing1();",
" case 2 -> nothing2();",
" default ->",
" //Here is a comment",
" nothing3();",
" }",
" }",
"}")
.doTest();
}

@Test
public void arrowSwitchCommentAfterArrow(){
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"public class Test {",
" public static void nothing1(){ }",
" public static void nothing2(){ }",
" public static void nothing3(){ }",
" public static void foo(int i) {",
" switch (i) {",
" case 1 -> nothing1();",
" case 2 -> nothing2();",
" default -> //Here is a comment",
" nothing3();",
" }",
" }",
"}")
.doTest();
}

@Test
public void arrowSwitchCommentInsideBraces(){
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"public class Test {",
" public static void nothing1(){ }",
" public static void nothing2(){ }",
" public static void nothing3(){ }",
" public static void foo(int i) {",
" switch (i) {",
" case 1 -> nothing1();",
" case 2 -> nothing2();",
" default -> {",
" //Here is a comment",
" nothing3();",
" }",
" }",
" }",
"}")
.doTest();
}


@Test
public void arrowSwitchWithComment(){
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"public class Test {",
" public static void nothing1(){ }",
" public static void nothing2(){ }",
" public static void nothing3(){ }",
" public static void foo(int i) {",
" switch (i) {",
" case 1 -> nothing1();",
" case 2 -> nothing2();",
" default -> nothing3();//Here is a comment",
" }",
" }",
"}")
.doTest();
}


}

0 comments on commit 933cc17

Please sign in to comment.