Skip to content

Commit cbb03fa

Browse files
committed
Replace mutable removeSuppressedLints with immutable withRemovedSuppressions.
1 parent b231189 commit cbb03fa

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

lib/src/main/java/com/diffplug/spotless/LintState.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
import java.io.File;
1919
import java.io.IOException;
2020
import java.nio.file.Files;
21+
import java.util.ArrayList;
2122
import java.util.Iterator;
2223
import java.util.LinkedHashMap;
2324
import java.util.List;
24-
import java.util.Objects;
2525

2626
import javax.annotation.Nullable;
2727

2828
public class LintState {
2929
private final DirtyState dirtyState;
30-
private @Nullable List<List<Lint>> lintsPerStep;
30+
private final @Nullable List<List<Lint>> lintsPerStep;
3131

3232
LintState(DirtyState dirtyState, @Nullable List<List<Lint>> lintsPerStep) {
3333
this.dirtyState = dirtyState;
@@ -64,36 +64,41 @@ public LinkedHashMap<String, List<Lint>> getLintsByStep(Formatter formatter) {
6464
return result;
6565
}
6666

67-
public void removeSuppressedLints(Formatter formatter, String relativePath, List<LintSuppression> suppressions) {
67+
public LintState withRemovedSuppressions(Formatter formatter, String relativePath, List<LintSuppression> suppressions) {
6868
if (lintsPerStep == null) {
69-
return;
69+
return this;
7070
}
7171
if (formatter.getSteps().size() != lintsPerStep.size()) {
7272
throw new IllegalStateException("LintState was created with a different formatter!");
7373
}
74+
boolean changed = false;
75+
ValuePerStep<List<Lint>> perStepFiltered = new ValuePerStep<>(formatter);
7476
for (int i = 0; i < lintsPerStep.size(); i++) {
7577
FormatterStep step = formatter.getSteps().get(i);
76-
List<Lint> lints = lintsPerStep.get(i);
77-
if (lints != null) {
78+
List<Lint> lintsOriginal = lintsPerStep.get(i);
79+
if (lintsOriginal != null) {
80+
List<Lint> lints = new ArrayList<>(lintsOriginal);
7881
Iterator<Lint> iter = lints.iterator();
7982
while (iter.hasNext()) {
8083
Lint lint = iter.next();
8184
for (LintSuppression suppression : suppressions) {
8285
if (suppression.suppresses(relativePath, step, lint)) {
86+
changed = true;
8387
iter.remove();
8488
break;
8589
}
8690
}
8791
}
88-
if (lints.isEmpty()) {
89-
lintsPerStep.set(i, null);
90-
if (lintsPerStep.stream().allMatch(Objects::isNull)) {
91-
lintsPerStep = null;
92-
return;
93-
}
92+
if (!lints.isEmpty()) {
93+
perStepFiltered.set(i, lints);
9494
}
9595
}
9696
}
97+
if (changed) {
98+
return new LintState(dirtyState, perStepFiltered.indexOfFirstValue() == -1 ? null : perStepFiltered);
99+
} else {
100+
return this;
101+
}
97102
}
98103

99104
public String asStringDetailed(File file, Formatter formatter) {

lib/src/test/java/com/diffplug/spotless/LintSuppressionTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ private Formatter formatter() {
4646

4747
@Test
4848
public void testMatchSingle() {
49-
var noSuppressions = dummyLintState();
50-
noSuppressions.removeSuppressedLints(formatter(), "file", List.of());
49+
var noSuppressions = dummyLintState().withRemovedSuppressions(formatter(), "file", List.of());
5150
assertThat(noSuppressions.isHasLints()).isTrue();
5251
removesLint(s -> s.setStep("blah")).isFalse();
5352
removesLint(s -> s.setStep("endWithNewline")).isTrue();
@@ -77,8 +76,7 @@ private AbstractBooleanAssert<?> removesLint(Consumer<LintSuppression> suppressi
7776
var s = new LintSuppression();
7877
suppression.accept(s);
7978

80-
var ls = dummyLintState();
81-
ls.removeSuppressedLints(formatter(), "testFile", List.of(s));
79+
var ls = dummyLintState().withRemovedSuppressions(formatter(), "testFile", List.of(s));
8280
return assertThat(!ls.isHasLints());
8381
}
8482
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter, File in
118118
lintState = LintState.clean();
119119
} else {
120120
try {
121-
lintState = LintState.of(formatter, input);
122-
lintState.removeSuppressedLints(formatter, relativePath, getLintSuppressions());
121+
lintState = LintState.of(formatter, input).withRemovedSuppressions(formatter, relativePath, getLintSuppressions());
123122
} catch (Throwable e) {
124123
throw new IllegalArgumentException("Issue processing file: " + input, e);
125124
}

0 commit comments

Comments
 (0)