Skip to content

Commit 362502a

Browse files
committed
use lint-errors
1 parent 0f95d2f commit 362502a

File tree

3 files changed

+61
-55
lines changed

3 files changed

+61
-55
lines changed

lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717

1818
import static com.diffplug.spotless.Lint.atLine;
1919

20+
import java.io.File;
2021
import java.io.Serializable;
22+
import java.util.ArrayList;
23+
import java.util.List;
2124
import java.util.Objects;
2225
import java.util.regex.Pattern;
2326

2427
import com.diffplug.spotless.FormatterFunc;
2528
import com.diffplug.spotless.FormatterStep;
29+
import com.diffplug.spotless.Lint;
2630

2731
public final class ReplaceRegexStep {
2832
// prevent direct instantiation
@@ -62,13 +66,22 @@ FormatterFunc toFormatter() {
6266
}
6367

6468
FormatterFunc toLinter() {
65-
return raw -> {
66-
var matcher = regex.matcher(raw);
67-
if (matcher.find()) {
68-
int line = 1 + (int) raw.codePoints().limit(matcher.start()).filter(c -> c == '\n').count();
69-
throw atLine(line, matcher.group(0), replacement).shortcut();
69+
return new FormatterFunc() {
70+
@Override
71+
public String apply(String raw) {
72+
return raw;
73+
}
74+
75+
@Override
76+
public List<Lint> lint(String raw, File file) {
77+
List<Lint> lints = new ArrayList<>();
78+
var matcher = regex.matcher(raw);
79+
while (matcher.find()) {
80+
int line = 1 + (int) raw.codePoints().limit(matcher.start()).filter(c -> c == '\n').count();
81+
lints.add(atLine(line, matcher.group(0), replacement));
82+
}
83+
return lints;
7084
}
71-
return raw;
7285
};
7386
}
7487
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveWildcardImportsStepTest.java

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.generic;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import com.diffplug.common.base.StringPrinter;
21+
import com.diffplug.spotless.FormatterStep;
22+
import com.diffplug.spotless.StepHarness;
23+
24+
class ReplaceRegexStepTest {
25+
@Test
26+
void formatter() throws Exception {
27+
FormatterStep step = ReplaceRegexStep.create("replace", "bad", "good");
28+
StepHarness.forStep(step)
29+
.test("bad bad", "good good");
30+
}
31+
32+
@Test
33+
void lint() throws Exception {
34+
FormatterStep step = ReplaceRegexStep.lint("regex", "bad", "no bad words");
35+
StepHarness.forStep(step)
36+
.expectLintsOf(StringPrinter.buildStringFromLines(
37+
"bad",
38+
"x bad y",
39+
"ok"))
40+
.toBe("L1 regex(bad) no bad words\nL2 regex(bad) no bad words");
41+
}
42+
}

0 commit comments

Comments
 (0)