Skip to content

Commit

Permalink
Remove duplicated semicolons, without removing newline (#216)
Browse files Browse the repository at this point in the history
* Remove duplicated semicolons, without removing newline

* Review suggestions

* No need for StringBuilder
  • Loading branch information
timtebeek authored Nov 15, 2023
1 parent 44b9903 commit aa57012
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@
*/
package org.openrewrite.staticanalysis;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
Expand All @@ -33,6 +24,9 @@
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.Statement;

import java.time.Duration;
import java.util.*;

public class RemoveExtraSemicolons extends Recipe {

@Override
Expand Down Expand Up @@ -69,9 +63,13 @@ public J.Block visitBlock(final J.Block block, final ExecutionContext executionC
if (statement instanceof J.Empty) {
nextNonEmptyAggregatedWithComments(statement, iterator)
.ifPresent(nextLine -> {
Space updatedPrefix = nextLine.getPrefix()
.withWhitespace(statement.getPrefix().getWhitespace());
result.add(nextLine.withPrefix(updatedPrefix));
String whitespace = statement.getPrefix().getWhitespace();
if (!whitespace.contains("\n") && nextLine.getComments().isEmpty()) {
result.add(nextLine);
} else {
Space updatedPrefix = nextLine.getPrefix().withWhitespace(whitespace);
result.add(nextLine.withPrefix(updatedPrefix));
}
});
} else {
result.add(statement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,37 @@ int test() {
)
);
}

@Test
void repeatedSemicolon() {
rewriteRun(
//language=java
java(
"""
class Test {
void test() {
int a = 1;;
int b = 2;
int c = 3;;;
int d = 4;
int e = 5; ;
int f = 6;
}
}
""",
"""
class Test {
void test() {
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
int f = 6;
}
}
"""
)
);
}
}

0 comments on commit aa57012

Please sign in to comment.