Skip to content

Commit e0bc6f7

Browse files
committed
Add generic ForbidRegex to gradle and maven.
1 parent bdfbc05 commit e0bc6f7

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,11 @@ public void replaceRegex(String name, String regex, String replacement) {
490490
addStep(ReplaceRegexStep.create(name, regex, replacement));
491491
}
492492

493+
/** A regex which generates a lint. */
494+
public void forbidRegex(String name, String regex, String lintDetail) {
495+
addStep(ReplaceRegexStep.lint(name, regex, lintDetail));
496+
}
497+
493498
/** Removes trailing whitespace. */
494499
public void trimTrailingWhitespace() {
495500
addStep(TrimTrailingWhitespaceStep.create());

plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.diffplug.spotless.LineEnding;
3939
import com.diffplug.spotless.maven.generic.EclipseWtp;
4040
import com.diffplug.spotless.maven.generic.EndWithNewline;
41+
import com.diffplug.spotless.maven.generic.ForbidRegex;
4142
import com.diffplug.spotless.maven.generic.Idea;
4243
import com.diffplug.spotless.maven.generic.Indent;
4344
import com.diffplug.spotless.maven.generic.Jsr223;
@@ -140,6 +141,10 @@ public final void addReplaceRegex(ReplaceRegex replaceRegex) {
140141
addStepFactory(replaceRegex);
141142
}
142143

144+
public final void addForbidRegex(ForbidRegex forbidRegex) {
145+
addStepFactory(forbidRegex);
146+
}
147+
143148
public final void addEclipseWtp(EclipseWtp eclipseWtp) {
144149
addStepFactory(eclipseWtp);
145150
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2016-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.maven.generic;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.generic.ReplaceRegexStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class ForbidRegex implements FormatterStepFactory {
26+
27+
@Parameter
28+
private String name;
29+
30+
@Parameter
31+
private String searchRegex;
32+
33+
@Parameter
34+
private String lintDetail;
35+
36+
@Override
37+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
38+
if (name == null || searchRegex == null) {
39+
throw new IllegalArgumentException("Must specify 'name' and 'searchRegex'.");
40+
}
41+
// Use empty string if replacement is not provided. In pom.xml there is no way to specify
42+
// an empty string as a property value as maven will always trim the value and if it is
43+
// empty, maven will consider the property as not provided.
44+
return ReplaceRegexStep.lint(name, searchRegex, lintDetail);
45+
}
46+
}

0 commit comments

Comments
 (0)