diff --git a/CHANGES.md b/CHANGES.md index fffedbbf9b..e99fecee87 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,9 +12,9 @@ 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)) +- `ExpandWildcardImportsStep` no longer fails with `Symbol resolution not configured` on records and other syntax newer than Java 11, and leaves files JavaParser cannot validate unchanged. ([#2946](https://github.com/diffplug/spotless/issues/2946)) +- `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/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java b/lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java index c563b7a4a2..251905cabb 100644 --- a/lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java +++ b/lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java @@ -33,6 +33,9 @@ import javassist.ClassPool; import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.ParserConfiguration.LanguageLevel; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.ImportDeclaration; import com.github.javaparser.ast.Node; @@ -71,7 +74,8 @@ public class ExpandWildcardsFormatterFunc implements FormatterFunc.NeedsFile { } public ExpandWildcardsFormatterFunc(Collection typeSolverClasspath) throws IOException { - this.parser = new JavaParser(); + // JavaParser defaults to Java 11 and rejects newer syntax such as records; align with JavaParserTypeSolver's default. + this.parser = new JavaParser(new ParserConfiguration().setLanguageLevel(LanguageLevel.BLEEDING_EDGE)); CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver(); combinedTypeSolver.add(new ReflectionTypeSolver(ReflectionTypeSolver.JCL_ONLY)); @@ -89,11 +93,12 @@ public ExpandWildcardsFormatterFunc(Collection typeSolverClasspath) throws @Override public String applyWithFile(String rawUnix, File file) throws Exception { - Optional parseResult = parser.parse(rawUnix).getResult(); - if (parseResult.isEmpty()) { + ParseResult parseResult = parser.parse(rawUnix); + if (!parseResult.isSuccessful()) { + // JavaParser only attaches the symbol resolver to successfully parsed compilation units return rawUnix; } - CompilationUnit cu = parseResult.get(); + CompilationUnit cu = parseResult.getResult().orElseThrow(); Map> importMap = findWildcardImports(cu) .stream() .collect(toMap(Function.identity(), 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..222d111457 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -5,9 +5,10 @@ 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)) +- `expandWildcardImports()` no longer fails with `Symbol resolution not configured` on records and other syntax newer than Java 11, and leaves files JavaParser cannot validate unchanged. ([#2946](https://github.com/diffplug/spotless/issues/2946)) +- String `targetExclude` patterns ending in `/**` (e.g. `**/build/**`) are now applied as exclude patterns on String `target`s, so excluded directories are skipped while walking the target instead of walking the whole project directory, which made configuration cache store and reuse scale with the size of the project. ([#3147](https://github.com/diffplug/spotless/issues/3147)) +- `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-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java index e1cc198067..19ea0ac5a3 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java @@ -30,9 +30,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Random; +import java.util.Set; import java.util.TreeMap; import java.util.function.Consumer; import java.util.function.Function; @@ -227,6 +230,22 @@ public void encoding(String charset) { protected FileCollection target; protected FileCollection targetExclude; + /* + * Bookkeeping which lets String excludes be applied as fileTree exclude patterns on the String targets, so that + * excluded directories are pruned while walking the target instead of subtracting a fileTree which walks the whole + * project directory, see https://github.com/diffplug/spotless/issues/3147 + */ + /** Trees created from String targets, all rooted at the project dir. */ + private final Set projectDirTargetTrees = Collections.newSetFromMap(new IdentityHashMap<>()); + /** The parts of each union created by {@link #parseTargetsIsExclude} for a target. */ + private final Map> targetUnionParts = new IdentityHashMap<>(); + /** The {@link #targetExclude} which {@link #targetExcludePrunable} and {@link #targetExcludeRest} were split from. */ + @Nullable private FileCollection targetExcludeSplitFrom; + /** String excludes ending in a {@code **} segment, which exclude exactly the same files as a tree exclude. */ + private List targetExcludePrunable = List.of(); + /** The rest of {@link #targetExclude}, or null if it consists only of prunable patterns. */ + @Nullable private FileCollection targetExcludeRest; + /** The value from which files will be excluded if their content contain it. */ @Nullable protected String targetExcludeContentPattern; @@ -263,9 +282,39 @@ public void target(Object... targets) { * fileTree, with project.rootDir as the dir. List are treated as the * 'includes' arg to fileTree, with project.rootDir as the dir. Anything else * gets passed to getProject().files(). + *

+ * String patterns which end with {@code /**} (e.g. {@code "**\/build/**"}) are + * applied directly to String targets, so the excluded directories are skipped + * while walking the target rather than walking the whole project directory. */ public void targetExclude(Object... targets) { this.targetExclude = parseTargetsIsExclude(targets, true); + List prunable = new ArrayList<>(); + List rest = new ArrayList<>(); + splitExcludes(Arrays.asList(targets), prunable, rest); + this.targetExcludeSplitFrom = targetExclude; + this.targetExcludePrunable = prunable; + this.targetExcludeRest = rest.isEmpty() ? null : parseTargetsIsExclude(rest.toArray(), true); + } + + private static void splitExcludes(Collection targets, List prunable, List rest) { + for (Object target : targets) { + if (target instanceof Collection collection) { + splitExcludes(collection, prunable, rest); + } else if (target instanceof String pattern && endsWithDoubleStarSegment(pattern)) { + prunable.add(pattern); + } else { + rest.add(target); + } + } + } + + /** + * A tree exclude prunes every directory matching the pattern, while subtracting {@code fileTree.include(pattern)} + * only removes the files matching it. Both remove the same files iff the pattern ends with a {@code **} segment. + */ + private static boolean endsWithDoubleStarSegment(String pattern) { + return pattern.equals("**") || pattern.endsWith("/**") || pattern.endsWith("\\**"); } /** @@ -294,8 +343,14 @@ private FileCollection parseTargetsIsExclude(Object[] targets, boolean isExclude return parseTargetIsExclude(targets[0], isExclude); } else { FileCollection union = getProject().files(); + List parts = new ArrayList<>(targets.length); for (Object target : targets) { - union = union.plus(parseTargetIsExclude(target, isExclude)); + FileCollection part = parseTargetIsExclude(target, isExclude); + parts.add(part); + union = union.plus(part); + } + if (!isExclude) { + targetUnionParts.put(union, parts); } return union; } @@ -345,6 +400,7 @@ private final FileCollection parseTargetIsExclude(Object target, boolean isExclu } matchedFiles.exclude(excludes); } + projectDirTargetTrees.add(matchedFiles); return matchedFiles; } else if (isExclude && (target instanceof FileSystemLocation || target instanceof Provider)) { // an excluded directory means the files under it, and the tree resolves them lazily @@ -1093,7 +1149,7 @@ public void toggleOffOnDisable() { protected void setupTask(SpotlessTask task) { task.setEncoding(getEncoding().name()); task.setLintSuppressions(lintSuppressions); - FileCollection totalTarget = targetExclude == null ? target : target.minus(targetExclude); + FileCollection totalTarget = targetExclude == null ? target : excludeFrom(target); task.setTarget(totalTarget); List steps; if (toggleFence != null) { @@ -1116,6 +1172,28 @@ protected void setupTask(SpotlessTask task) { task.setupRatchet(getRatchetFrom() != null ? getRatchetFrom() : ""); } + /** Returns {@code fromTarget - targetExclude}, applying prunable String excludes as tree excludes where possible. */ + private FileCollection excludeFrom(FileCollection fromTarget) { + List parts = targetUnionParts.get(fromTarget); + if (parts != null) { + FileCollection union = getProject().files(); + for (FileCollection part : parts) { + union = union.plus(excludeFrom(part)); + } + return union; + } else if (fromTarget instanceof ConfigurableFileTree tree && projectDirTargetTrees.contains(tree) + && targetExclude == targetExcludeSplitFrom && !targetExcludePrunable.isEmpty()) { + // same root as the String excludes, so the patterns match the same relative paths + ConfigurableFileTree pruned = getProject().fileTree(tree.getDir()); + pruned.setIncludes(tree.getIncludes()); + pruned.setExcludes(tree.getExcludes()); + pruned.exclude(targetExcludePrunable); + return targetExcludeRest == null ? pruned : pruned.minus(targetExcludeRest); + } else { + return fromTarget.minus(targetExclude); + } + } + /** Returns the project that this extension is attached to. */ protected Project getProject() { return spotless.project; diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TargetExcludePatternTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TargetExcludePatternTest.java new file mode 100644 index 0000000000..a07e5a7755 --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TargetExcludePatternTest.java @@ -0,0 +1,128 @@ +/* + * 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.gradle.spotless; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; + +import org.gradle.testkit.runner.BuildResult; +import org.junit.jupiter.api.Test; + +/** String {@code targetExclude} patterns, see #3147. */ +class TargetExcludePatternTest extends GradleIntegrationHarness { + private void lowercaseFormat(String... targetAndExclude) throws IOException { + String[] lines = new String[targetAndExclude.length + 6]; + lines[0] = "plugins { id 'com.diffplug.spotless' }"; + lines[1] = "spotless {"; + lines[2] = " format 'toLower', {"; + System.arraycopy(targetAndExclude, 0, lines, 3, targetAndExclude.length); + lines[lines.length - 3] = " custom 'lowercase', { str -> str.toLowerCase() }"; + lines[lines.length - 2] = " }"; + lines[lines.length - 1] = "}"; + setFile("build.gradle").toLines(lines); + } + + @Test + void excludePatternIsAppliedToTheTargetTree() throws IOException { + // subtracting fileTree(projectDir).include('**/build/**') walks the whole project dir as soon as the + // target is resolved, which happens at configuration time with the configuration cache + lowercaseFormat( + " target 'src/**/*.md'", + " targetExclude '**/build/**'"); + setFile("build.gradle").toContent(read("build.gradle") + String.join("\n", + "", + "afterEvaluate {", + " def target = tasks.getByName('spotlessToLower').target", + " println('prunedTarget=' + (target instanceof ConfigurableFileTree && target.excludes.contains('**/build/**')))", + "}")); + setFile("src/manual.md").toLines("A B C"); + setFile("src/build/generated.md").toLines("A B C"); + + BuildResult result = gradleRunner().withArguments("spotlessApply").build(); + + assertThat(result.getOutput()).contains("prunedTarget=true"); + assertFile("src/manual.md").hasLines("a b c"); + assertFile("src/build/generated.md").hasLines("A B C"); + } + + @Test + void mixOfDirectoryAndFilePatterns() throws IOException { + lowercaseFormat( + " target '**/*.md'", + " targetExclude '**/build/**', 'docs/skip.md', ['**/gen/**']"); + setFile("a.md").toLines("A B C"); + setFile("docs/keep.md").toLines("A B C"); + setFile("docs/skip.md").toLines("A B C"); + setFile("module/build/out.md").toLines("A B C"); + setFile("module/gen/nested/out.md").toLines("A B C"); + + gradleRunner().withArguments("spotlessApply").build(); + + assertFile("a.md").hasLines("a b c"); + assertFile("docs/keep.md").hasLines("a b c"); + assertFile("docs/skip.md").hasLines("A B C"); + assertFile("module/build/out.md").hasLines("A B C"); + assertFile("module/gen/nested/out.md").hasLines("A B C"); + } + + @Test + void patternTogetherWithDirectoryExclude() throws IOException { + lowercaseFormat( + " target 'src/**/*.md', 'docs/**/*.md'", + " targetExclude '**/tmp/**', layout.projectDirectory.dir('docs/generated')"); + setFile("src/a.md").toLines("A B C"); + setFile("src/tmp/a.md").toLines("A B C"); + setFile("docs/b.md").toLines("A B C"); + setFile("docs/generated/b.md").toLines("A B C"); + + gradleRunner().withArguments("spotlessApply").build(); + + assertFile("src/a.md").hasLines("a b c"); + assertFile("src/tmp/a.md").hasLines("A B C"); + assertFile("docs/b.md").hasLines("a b c"); + assertFile("docs/generated/b.md").hasLines("A B C"); + } + + @Test + void patternAgainstUserProvidedFileTree() throws IOException { + // excludes stay relative to the project dir, even when the target tree has another root + lowercaseFormat( + " target fileTree('src') { include '**/*.md' }", + " targetExclude 'src/build/**'"); + setFile("src/a.md").toLines("A B C"); + setFile("src/build/a.md").toLines("A B C"); + + gradleRunner().withArguments("spotlessApply").build(); + + assertFile("src/a.md").hasLines("a b c"); + assertFile("src/build/a.md").hasLines("A B C"); + } + + @Test + void targetExcludeBeforeTarget() throws IOException { + lowercaseFormat( + " targetExclude '**/build/**'", + " target 'src/**/*.md'"); + setFile("src/a.md").toLines("A B C"); + setFile("src/build/a.md").toLines("A B C"); + + gradleRunner().withArguments("spotlessApply").build(); + + assertFile("src/a.md").hasLines("a b c"); + assertFile("src/build/a.md").hasLines("A B C"); + } +} diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 5a2a689c61..ea6f272a79 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -5,9 +5,9 @@ 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 fails with `Symbol resolution not configured` on records and other syntax newer than Java 11, and leaves files JavaParser cannot validate unchanged. ([#2946](https://github.com/diffplug/spotless/issues/2946)) +- `` 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/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/ExpandWildcardImportsStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/ExpandWildcardImportsStepTest.java index a3edd82849..99a7d99c5f 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/ExpandWildcardImportsStepTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/ExpandWildcardImportsStepTest.java @@ -185,6 +185,83 @@ void expandWildcardImports_withMixedPomAndJarDependencies() throws Exception { assertFile(path).sameAsResource("java/expandwildcardimports/JavaClassWithWildcardsFormatted.test"); } + /** + * Regression test for #2946: + * a reactor where module {@code two} depends on module {@code one}, and {@code two} + * contains a record using a wildcard import. + */ + @Test + void expandWildcardImports_givenMultiModuleReactorWithRecords_whenApply_thenExpandsImports() throws Exception { + // given + String spotlessVersion = System.getProperty("spotlessMavenPluginVersion"); + setFile("pom.xml").toContent(""" + + 4.0.0 + com.example + parent + 1.0.0-SNAPSHOT + pom + + one + two + + + UTF-8 + + + + + com.diffplug.spotless + spotless-maven-plugin + %s + + + + + + + + """.formatted(spotlessVersion)); + setFile("one/pom.xml").toContent(childPom("one", "")); + setFile("two/pom.xml").toContent(childPom("two", """ + + + com.example + one + 1.0.0-SNAPSHOT + + + """)); + setFile("one/src/main/java/one/One.java").toContent(""" + package one; + + public record One(Integer x, Integer y) {} + """); + String path = "two/src/main/java/two/Two.java"; + setFile(path).toResource("java/expandwildcardimports/RecordUnformatted.test"); + + // when + mavenRunner().withArguments("spotless:apply").runNoError(); + + // then + assertFile(path).sameAsResource("java/expandwildcardimports/RecordFormatted.test"); + } + + private static String childPom(String artifactId, String dependencies) { + return """ + + 4.0.0 + + com.example + parent + 1.0.0-SNAPSHOT + + %s + %s + + """.formatted(artifactId, dependencies); + } + private void setupLocalMavenRepo() throws Exception { // Write a minimal POM so that mvnw can start for install:install-file setFile("pom.xml").toContent(""" diff --git a/testlib/src/main/resources/java/expandwildcardimports/ModernSyntaxFormatted.test b/testlib/src/main/resources/java/expandwildcardimports/ModernSyntaxFormatted.test new file mode 100644 index 0000000000..ed262c36c1 --- /dev/null +++ b/testlib/src/main/resources/java/expandwildcardimports/ModernSyntaxFormatted.test @@ -0,0 +1,19 @@ +package shapes; + +import java.time.LocalDate; +import java.util.List; + +public sealed interface Shape permits Shape.Circle, Shape.Square { + + record Circle(double radius, LocalDate created) implements Shape {} + + record Square(double side, List tags) implements Shape {} + + static String describe(Shape shape) { + return switch (shape) { + case Circle c when c.radius() > 10 -> "big circle"; + case Circle c -> "circle"; + case Square(double side, var tags) -> "square " + side + tags; + }; + } +} diff --git a/testlib/src/main/resources/java/expandwildcardimports/ModernSyntaxUnformatted.test b/testlib/src/main/resources/java/expandwildcardimports/ModernSyntaxUnformatted.test new file mode 100644 index 0000000000..4d601b1e97 --- /dev/null +++ b/testlib/src/main/resources/java/expandwildcardimports/ModernSyntaxUnformatted.test @@ -0,0 +1,19 @@ +package shapes; + +import java.time.*; +import java.util.*; + +public sealed interface Shape permits Shape.Circle, Shape.Square { + + record Circle(double radius, LocalDate created) implements Shape {} + + record Square(double side, List tags) implements Shape {} + + static String describe(Shape shape) { + return switch (shape) { + case Circle c when c.radius() > 10 -> "big circle"; + case Circle c -> "circle"; + case Square(double side, var tags) -> "square " + side + tags; + }; + } +} diff --git a/testlib/src/main/resources/java/expandwildcardimports/RecordFormatted.test b/testlib/src/main/resources/java/expandwildcardimports/RecordFormatted.test new file mode 100644 index 0000000000..6f22d67e93 --- /dev/null +++ b/testlib/src/main/resources/java/expandwildcardimports/RecordFormatted.test @@ -0,0 +1,5 @@ +package two; + +import java.time.LocalDate; + +public record Two(LocalDate date) {} diff --git a/testlib/src/main/resources/java/expandwildcardimports/RecordUnformatted.test b/testlib/src/main/resources/java/expandwildcardimports/RecordUnformatted.test new file mode 100644 index 0000000000..b9683a416d --- /dev/null +++ b/testlib/src/main/resources/java/expandwildcardimports/RecordUnformatted.test @@ -0,0 +1,5 @@ +package two; + +import java.time.*; + +public record Two(LocalDate date) {} diff --git a/testlib/src/main/resources/java/expandwildcardimports/ValidationProblem.test b/testlib/src/main/resources/java/expandwildcardimports/ValidationProblem.test new file mode 100644 index 0000000000..896c249ca2 --- /dev/null +++ b/testlib/src/main/resources/java/expandwildcardimports/ValidationProblem.test @@ -0,0 +1,7 @@ +package invalid; + +import java.util.*; + +public abstract final class Invalid { + private List items; +} 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)")); + } + } +} diff --git a/testlib/src/test/java/com/diffplug/spotless/java/ExpandWildcardImportsStepTest.java b/testlib/src/test/java/com/diffplug/spotless/java/ExpandWildcardImportsStepTest.java index 25ef9e2aab..2766eb76e2 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/ExpandWildcardImportsStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/ExpandWildcardImportsStepTest.java @@ -151,6 +151,34 @@ void expandWildcardImports_keepsStaticFieldsAndEnumConstants() throws Exception StepHarness.forStep(step).testResource("java/expandwildcardimports/StaticFieldsAndEnumConstantsUnformatted.test", "java/expandwildcardimports/StaticFieldsAndEnumConstantsFormatted.test"); } + /** Regression test for #2946. */ + @Test + void expandWildcardImports_givenRecord_whenExpanding_thenImportsUsedType() throws Exception { + // given + FormatterStep step = ExpandWildcardImportsStep.create(Collections.emptySet(), TestProvisioner.mavenCentral()); + + // when / then + StepHarness.forStep(step).testResource("java/expandwildcardimports/RecordUnformatted.test", "java/expandwildcardimports/RecordFormatted.test"); + } + + @Test + void expandWildcardImports_givenPostJava11Syntax_whenExpanding_thenImportsUsedTypes() throws Exception { + // given: sealed interface, records, guarded and record-deconstruction patterns in a switch expression + FormatterStep step = ExpandWildcardImportsStep.create(Collections.emptySet(), TestProvisioner.mavenCentral()); + + // when / then + StepHarness.forStep(step).testResource("java/expandwildcardimports/ModernSyntaxUnformatted.test", "java/expandwildcardimports/ModernSyntaxFormatted.test"); + } + + @Test + void expandWildcardImports_givenValidationProblem_whenExpanding_thenLeavesSourceUnchanged() throws Exception { + // given: syntactically parseable, but rejected by JavaParser's validator (duplicated modifier) + FormatterStep step = ExpandWildcardImportsStep.create(Collections.emptySet(), TestProvisioner.mavenCentral()); + + // when / then: treated like an unparseable file instead of failing on symbol resolution + StepHarness.forStep(step).testResourceUnaffected("java/expandwildcardimports/ValidationProblem.test"); + } + @Test void expandWildcardImports_nullSafety() throws Exception { // Test null safety