diff --git a/CHANGES.md b/CHANGES.md index fffedbbf9b..eac754f5cb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,9 +12,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed - GrEclipse no longer leaves a `file%3A...` directory in the working directory. ([#2019](https://github.com/diffplug/spotless/issues/2019)) +- `removeSemicolons()` no longer removes semicolons inside dollar-slashy strings (`$/ ... /$`), after escaped triple quotes in multiline strings, or in multi-line `for (init; cond; update)` headers. ([#2780](https://github.com/diffplug/spotless/issues/2780)) - `RemoveUnusedImportsStep` and `GoogleJavaFormatStep` no longer remove imports that are only used by links in Markdown doc comments (`///`, JDK 23+). ([#3161](https://github.com/diffplug/spotless/issues/3161)) - -### Fixed - `forbidWildcardImports` no longer reports import statements inside Java text blocks or comments. ([#2997](https://github.com/diffplug/spotless/issues/2997)) ## [4.11.0] - 2026-11-02 diff --git a/lib/src/main/java/com/diffplug/spotless/groovy/RemoveSemicolonsStep.java b/lib/src/main/java/com/diffplug/spotless/groovy/RemoveSemicolonsStep.java index 8a5c325346..4dd17a93bb 100644 --- a/lib/src/main/java/com/diffplug/spotless/groovy/RemoveSemicolonsStep.java +++ b/lib/src/main/java/com/diffplug/spotless/groovy/RemoveSemicolonsStep.java @@ -17,12 +17,20 @@ import java.io.Serial; import java.io.Serializable; +import java.util.ArrayDeque; +import java.util.Deque; import com.diffplug.spotless.FormatterFunc; import com.diffplug.spotless.FormatterStep; /** - * Removes unnecessary semicolons from Groovy code. Preserves semicolons inside strings and comments. + * Removes unnecessary semicolons from Groovy code. A semicolon is only removed when it is the last non-whitespace + * character on its line, outside any string literal or comment, and not part of a multi-line {@code for (init; cond; + * update)} header. + *

+ * Recognised literals: {@code '...'}, {@code "..."}, {@code '''...'''}, {@code """..."""} and dollar-slashy + * {@code $/.../$} strings. Plain slashy strings ({@code /.../}) are not recognised, because telling them apart from + * division requires a full Groovy lexer. * * @author Jose Luis Badano */ @@ -46,113 +54,120 @@ private static final class State implements Serializable { private static final long serialVersionUID = 1L; FormatterFunc toFormatter() { - return raw -> { - StringBuilder result = new StringBuilder(raw.length()); - - // State tracking - boolean inSingleQuoteString = false; - boolean inDoubleQuoteString = false; - boolean inTripleSingleQuoteString = false; - boolean inTripleDoubleQuoteString = false; - boolean inSingleLineComment = false; - boolean inMultiLineComment = false; - boolean escaped = false; + return State::removeSemicolons; + } - for (int i = 0; i < raw.length(); i++) { - char c = raw.charAt(i); + private enum Mode { + CODE, LINE_COMMENT, BLOCK_COMMENT, SINGLE_QUOTE, DOUBLE_QUOTE, TRIPLE_SINGLE_QUOTE, TRIPLE_DOUBLE_QUOTE, DOLLAR_SLASHY + } - // Check for triple quotes first (needs lookahead) - if (!inSingleLineComment && !inMultiLineComment && i + 2 < raw.length()) { - String triple = raw.substring(i, i + 3); - if ("'''".equals(triple) && !inDoubleQuoteString && !inTripleDoubleQuoteString) { - inTripleSingleQuoteString = !inTripleSingleQuoteString; - result.append(triple); - i += 2; - continue; - } else if ("\"\"\"".equals(triple) && !inSingleQuoteString && !inTripleSingleQuoteString) { - inTripleDoubleQuoteString = !inTripleDoubleQuoteString; - result.append(triple); - i += 2; - continue; - } + private static String removeSemicolons(String raw) { + StringBuilder result = new StringBuilder(raw.length()); + Mode mode = Mode.CODE; + // one entry per open bracket, true if it is the '(' of a `for` header + Deque brackets = new ArrayDeque<>(); + int i = 0; + while (i < raw.length()) { + char c = raw.charAt(i); + int consumed = 1; + switch (mode) { + case CODE: + if (raw.startsWith("//", i)) { + mode = Mode.LINE_COMMENT; + consumed = 2; + } else if (raw.startsWith("/*", i)) { + mode = Mode.BLOCK_COMMENT; + consumed = 2; + } else if (raw.startsWith("'''", i)) { + mode = Mode.TRIPLE_SINGLE_QUOTE; + consumed = 3; + } else if (raw.startsWith("\"\"\"", i)) { + mode = Mode.TRIPLE_DOUBLE_QUOTE; + consumed = 3; + } else if (raw.startsWith("$/", i) && (i == 0 || !Character.isJavaIdentifierPart(raw.charAt(i - 1)))) { + mode = Mode.DOLLAR_SLASHY; + consumed = 2; + } else if (c == '\'') { + mode = Mode.SINGLE_QUOTE; + } else if (c == '"') { + mode = Mode.DOUBLE_QUOTE; + } else if (c == '(') { + brackets.push(isPrecededByFor(raw, i)); + } else if (c == '{' || c == '[') { + brackets.push(Boolean.FALSE); + } else if (c == ')' || c == '}' || c == ']') { + brackets.poll(); + } else if (c == ';' && !Boolean.TRUE.equals(brackets.peek()) && isLastOnLine(raw, i)) { + i++; + continue; // drop the semicolon } - - // Handle escaping - if (c == '\\' && (inSingleQuoteString || inDoubleQuoteString || - inTripleSingleQuoteString || inTripleDoubleQuoteString)) { - escaped = !escaped; - result.append(c); - continue; + break; + case LINE_COMMENT: + if (c == '\n' || c == '\r') { + mode = Mode.CODE; } - - // Check for comments (only if not in string) - if (!inSingleQuoteString && !inDoubleQuoteString && - !inTripleSingleQuoteString && !inTripleDoubleQuoteString && !escaped) { - - // Single line comment - if (c == '/' && i + 1 < raw.length() && raw.charAt(i + 1) == '/' && !inMultiLineComment) { - inSingleLineComment = true; - } - // Multi-line comment start - else if (c == '/' && i + 1 < raw.length() && raw.charAt(i + 1) == '*' && !inSingleLineComment) { - inMultiLineComment = true; - } - // Multi-line comment end - else if (c == '*' && i + 1 < raw.length() && raw.charAt(i + 1) == '/' && inMultiLineComment) { - inMultiLineComment = false; - result.append(c); - if (i + 1 < raw.length()) { - result.append(raw.charAt(i + 1)); - i++; - } - continue; - } + break; + case BLOCK_COMMENT: + if (raw.startsWith("*/", i)) { + mode = Mode.CODE; + consumed = 2; } - - // Check for string quotes (only if not in comment and not already in triple quotes) - if (!inSingleLineComment && !inMultiLineComment && !escaped) { - if (c == '\'' && !inDoubleQuoteString && !inTripleSingleQuoteString && !inTripleDoubleQuoteString) { - inSingleQuoteString = !inSingleQuoteString; - } else if (c == '"' && !inSingleQuoteString && !inTripleSingleQuoteString && !inTripleDoubleQuoteString) { - inDoubleQuoteString = !inDoubleQuoteString; - } + break; + case SINGLE_QUOTE: + case DOUBLE_QUOTE: + if (c == '\\') { + consumed = 2; // skip the escaped character + } else if (c == (mode == Mode.SINGLE_QUOTE ? '\'' : '"') || c == '\n' || c == '\r') { + // a newline can't occur in a single-line string, so it means we misread the input: resync + mode = Mode.CODE; } - - // End single line comment on newline - if ((c == '\n' || c == '\r') && inSingleLineComment) { - inSingleLineComment = false; + break; + case TRIPLE_SINGLE_QUOTE: + case TRIPLE_DOUBLE_QUOTE: + if (c == '\\') { + consumed = 2; // skip the escaped character, so \''' or \""" doesn't end the string + } else if (raw.startsWith(mode == Mode.TRIPLE_SINGLE_QUOTE ? "'''" : "\"\"\"", i)) { + mode = Mode.CODE; + consumed = 3; } - - // Check if we should remove this semicolon - if (c == ';' && !inSingleQuoteString && !inDoubleQuoteString && - !inTripleSingleQuoteString && !inTripleDoubleQuoteString && - !inSingleLineComment && !inMultiLineComment) { - - // Look ahead to see if this semicolon is at the end of the line - boolean isEndOfLine = true; - for (int j = i + 1; j < raw.length(); j++) { - char next = raw.charAt(j); - if (next == '\n' || next == '\r') { - break; // End of line - } else if (!Character.isWhitespace(next)) { - isEndOfLine = false; - break; - } - // If it's whitespace but not newline, continue checking - } - - // Only remove if it's at the end of the line - if (isEndOfLine) { - continue; // Skip this semicolon - } + break; + case DOLLAR_SLASHY: + if (raw.startsWith("$$", i) || raw.startsWith("$/", i)) { + consumed = 2; // escaped dollar or slash + } else if (raw.startsWith("/$", i)) { + mode = Mode.CODE; + consumed = 2; } - - result.append(c); - escaped = false; + break; + default: + throw new IllegalStateException("Unknown mode " + mode); } + int end = Math.min(raw.length(), i + consumed); + result.append(raw, i, end); + i = end; + } + return result.toString(); + } + + private static boolean isPrecededByFor(String raw, int parenIndex) { + int j = parenIndex - 1; + while (j >= 0 && Character.isWhitespace(raw.charAt(j))) { + j--; + } + return j >= 2 && raw.startsWith("for", j - 2) + && (j < 3 || !Character.isJavaIdentifierPart(raw.charAt(j - 3))); + } - return result.toString(); - }; + private static boolean isLastOnLine(String raw, int semicolonIndex) { + for (int j = semicolonIndex + 1; j < raw.length(); j++) { + char next = raw.charAt(j); + if (next == '\n' || next == '\r') { + return true; + } else if (!Character.isWhitespace(next)) { + return false; + } + } + return true; } } } diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 144e75ebf9..cbd0ef9b1f 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -5,9 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed - GrEclipse no longer leaves a `file%3A...` directory in the working directory. ([#2019](https://github.com/diffplug/spotless/issues/2019)) +- `removeSemicolons()` no longer removes semicolons inside dollar-slashy strings (`$/ ... /$`), after escaped triple quotes in multiline strings, or in multi-line `for (init; cond; update)` headers. ([#2780](https://github.com/diffplug/spotless/issues/2780)) - `removeUnusedImports()` and `googleJavaFormat()` no longer remove imports that are only used by links in Markdown doc comments (`///`, JDK 23+). ([#3161](https://github.com/diffplug/spotless/issues/3161)) - -### Fixed - `forbidWildcardImports` no longer reports import statements inside Java text blocks or comments. ([#2997](https://github.com/diffplug/spotless/issues/2997)) ## [8.11.0] - 2026-11-02 diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 5a2a689c61..251d23233c 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -5,9 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed - GrEclipse no longer leaves a `file%3A...` directory in the working directory. ([#2019](https://github.com/diffplug/spotless/issues/2019)) +- `` no longer removes semicolons inside dollar-slashy strings (`$/ ... /$`), after escaped triple quotes in multiline strings, or in multi-line `for (init; cond; update)` headers. ([#2780](https://github.com/diffplug/spotless/issues/2780)) - `` and `` no longer remove imports that are only used by links in Markdown doc comments (`///`, JDK 23+). ([#3161](https://github.com/diffplug/spotless/issues/3161)) - -### Fixed - `forbidWildcardImports` no longer reports import statements inside Java text blocks or comments. ([#2997](https://github.com/diffplug/spotless/issues/2997)) ## [3.11.0] - 2026-11-02 diff --git a/testlib/src/test/java/com/diffplug/spotless/groovy/RemoveSemicolonsStepTest.java b/testlib/src/test/java/com/diffplug/spotless/groovy/RemoveSemicolonsStepTest.java new file mode 100644 index 0000000000..057b4e7290 --- /dev/null +++ b/testlib/src/test/java/com/diffplug/spotless/groovy/RemoveSemicolonsStepTest.java @@ -0,0 +1,211 @@ +/* + * Copyright 2026 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.groovy; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import com.diffplug.spotless.StepHarness; + +class RemoveSemicolonsStepTest { + private final StepHarness harness = StepHarness.forStep(RemoveSemicolonsStep.create()); + + private static String lines(String... lines) { + return String.join("\n", lines); + } + + @Test + void removesTrailingSemicolons() { + harness.testResource("groovy/removeSemicolons/GroovyCodeWithSemicolons.test", + "groovy/removeSemicolons/GroovyCodeWithSemicolonsFormatted.test"); + } + + @Test + void keepsSemicolonsBetweenStatementsAndBeforeTrailingComments() { + harness.testUnaffected(lines( + "def a = 1; def b = 2", + "println(a); // trailing comment", + "for (;;) { break }")); + } + + /** + * `removeSemicolons()` should not be applied to multiline strings in groovy + */ + @Nested + class Issue2780 { + @Test + void multilineStrings() { + harness.testResource("groovy/removeSemicolons/Issue2780/MultilineString.test", + "groovy/removeSemicolons/Issue2780/MultilineStringFormatted.test"); + } + + @Test + void comments() { + harness.testResource("groovy/removeSemicolons/Issue2780/Comments.test", + "groovy/removeSemicolons/Issue2780/CommentsFormatted.test"); + } + + @Test + void dollarSlashyString() { + harness.test( + lines( + "def js = $/", + " function (doc, meta) {", + " emit(meta.id, null);", + " }", + "/$;", + "println(js);"), + lines( + "def js = $/", + " function (doc, meta) {", + " emit(meta.id, null);", + " }", + "/$", + "println(js)")); + } + + @Test + void dollarSlashyStringWithEscapes() { + // `$/` is an escaped slash and `$$` an escaped dollar, neither terminates the string + harness.test( + lines( + "def s = $/", + " a $/$ b;", + " $$ c;", + "/$;"), + lines( + "def s = $/", + " a $/$ b;", + " $$ c;", + "/$")); + } + + @Test + void escapedTripleDoubleQuotes() { + harness.test( + lines( + "def s = \"\"\"", + " say \\\"\"\"hi;", + " emit(x);", + "\"\"\";"), + lines( + "def s = \"\"\"", + " say \\\"\"\"hi;", + " emit(x);", + "\"\"\"")); + } + + @Test + void escapedTripleSingleQuotes() { + harness.test( + lines( + "def s = '''", + " it\\'''s;", + " emit(x);", + "''';"), + lines( + "def s = '''", + " it\\'''s;", + " emit(x);", + "'''")); + } + + @Test + void multiLineForHeader() { + harness.test( + lines( + "for (int i = 0;", + " i < 10;", + " i++) {", + " println(i);", + "}"), + lines( + "for (int i = 0;", + " i < 10;", + " i++) {", + " println(i)", + "}")); + } + + @Test + void closuresInsideParenthesesAreStillCleaned() { + harness.test( + lines( + "example.andThen(val -> {", + " return val + 2;", + "});", + "list.each({ x ->", + " println(x);", + "})"), + lines( + "example.andThen(val -> {", + " return val + 2", + "})", + "list.each({ x ->", + " println(x)", + "})")); + } + + @Test + void multiLineForHeaderWithClosureBody() { + harness.test( + lines( + "for (int i = 0;", + " i < 10;", + " i++) {", + " run({", + " println(i);", + " });", + "}"), + lines( + "for (int i = 0;", + " i < 10;", + " i++) {", + " run({", + " println(i)", + " })", + "}")); + } + + @Test + void onlyTheForKeywordProtectsParentheses() { + harness.test( + lines( + "platfor(a;", + " b);"), + lines( + "platfor(a", + " b)")); + } + + @Test + void bracketsInsideStringsAndCommentsAreIgnored() { + harness.test( + lines( + "def a = \"(\";", + "def b = ')';", + "// (", + "/* ) */", + "println(a + b);"), + lines( + "def a = \"(\"", + "def b = ')'", + "// (", + "/* ) */", + "println(a + b)")); + } + } +}