Skip to content

Commit

Permalink
SpacesVisitor: Remove spaces before statement semicolon (#3905)
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden authored Jan 13, 2024
1 parent 86f8164 commit 02d0ee9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void beforeParensMethodCallTrue() {
"""
class Test {
void foo() {
foo();
foo() ;
Test test = new Test();
}
}
Expand All @@ -219,6 +219,39 @@ void foo() {
);
}

@Test
void noSpaceBeforeSemicolon() {
rewriteRun(
spaces(style -> style),
java(
"""
class Test {
boolean b = true ;
void foo() {
foo() ;
Test test = new Test()
;
if (b)
System.out.println("OK") ;
}
}
""",
"""
class Test {
boolean b = true;
void foo() {
foo();
Test test = new Test()
;
if (b)
System.out.println("OK");
}
}
"""
)
);
}

@Test
void beforeParensMethodCallFalse() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P
return c;
}

@Override
public J.Block visitBlock(J.Block block, P p) {
J.Block b = super.visitBlock(block, p);
b = b.getPadding().withStatements(
ListUtils.map(b.getPadding().getStatements(), stmt -> spaceAfter(stmt, false))
);
return b;
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, P p) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, p);
Expand Down Expand Up @@ -391,7 +400,7 @@ public J.MultiCatch visitMultiCatch(J.MultiCatch multiCatch, P p) {
public J.If visitIf(J.If iff, P p) {
J.If i = super.visitIf(iff, p);
i = i.withIfCondition(spaceBefore(i.getIfCondition(), style.getBeforeParentheses().getIfParentheses()));
i = i.getPadding().withThenPart(spaceBeforeRightPaddedElement(i.getPadding().getThenPart(), style.getBeforeLeftBrace().getIfLeftBrace()));
i = i.getPadding().withThenPart(spaceAfter(spaceBeforeRightPaddedElement(i.getPadding().getThenPart(), style.getBeforeLeftBrace().getIfLeftBrace()), false));
boolean useSpaceWithinIfParentheses = style.getWithin().getIfParentheses();
i = i.withIfCondition(
i.getIfCondition().getPadding().withTree(
Expand Down

0 comments on commit 02d0ee9

Please sign in to comment.