Skip to content

Commit

Permalink
linguistic naming should ignore regex/pattern variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Apr 6, 2024
1 parent 98e6726 commit 122f967
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void process(CtMethod<?> ctMethod) {
}

String methodName = ctMethod.getSimpleName();
if (ctMethod.getType().equals(ctMethod.getFactory().Type().createReference(boolean.class))
if (ctMethod.getType().equals(ctMethod.getFactory().Type().booleanPrimitiveType())
&& methodName.startsWith("get")) {
String newName = "is" + methodName.substring(3);
addLocalProblem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

@ExecutableCheck(reportedProblems = { ProblemType.CONFUSING_IDENTIFIER })
public class LinguisticNamingCheck extends IntegratedCheck {
private static final Set<String> IGNORE_VARIABLES_WITH = Set.of("regex", "pattern");
private static final Set<String> COMMON_BOOLEAN_GETTER_PREFIXES = Set.of(
"is", "are", "can", "could", "must", "has", "have", "does", "will", "should", "would",
"takes", "looks", "uses", "finds"
Expand Down Expand Up @@ -55,6 +56,10 @@ private void reportProblem(String key, CtNamedElement ctNamedElement, Map<String
}

private <T> void checkCtVariable(CtVariable<T> ctVariable) {
if (IGNORE_VARIABLES_WITH.stream().anyMatch(s -> ctVariable.getSimpleName().toLowerCase().contains(s))) {
return;
}

if (hasBooleanPrefix(ctVariable) && !SpoonUtil.isBoolean(ctVariable)) {
this.reportProblem(
"linguistic-naming-boolean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,20 @@ public boolean isInvalidValue(String value) {

problems.assertExhausted();
}

@Test
void testVariablesToIgnore() throws IOException, LinterException {
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceString(
JavaVersion.JAVA_17,
"Test",
"""
public class Test {
private static final String HAS_VALUE_REGEX = "value"; // ok
private static final String HAS_VALUE_PATTERN = "value"; // ok
}
"""
), PROBLEM_TYPES);

problems.assertExhausted();
}
}

0 comments on commit 122f967

Please sign in to comment.