Skip to content

Commit

Permalink
fix bug in contains value in name check
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Mar 21, 2024
1 parent 0204084 commit a43965d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import spoon.reflect.declaration.CtField;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -29,7 +30,14 @@ public class ConstantsHaveDescriptiveNamesCheck extends IntegratedCheck {
Map.entry("->", List.of("arrow")),
Map.entry("-->", List.of("arrow"))
);
private static final List<String> CONTEXTUAL_WORDS = List.of(
"space", "whitespace",
"white_space",
"empty",
"blank"
);

private static final double CONTEXTUAL_WORD_THRESHOLD = 0.5;
private static final int MAXIMUM_NAME_DIFFERENCE = 2;

private static boolean isNonDescriptiveIntegerName(String name, int value) {
Expand Down Expand Up @@ -133,7 +141,22 @@ private static boolean containsValueInName(String name, CtLiteral<?> value) {
if (valueString.length() == 1 && !Character.isAlphabetic(valueString.charAt(0))) {
List<String> charOptions = listCharOptions(valueString.charAt(0));
if (charOptions != null) {
return charOptions.stream().anyMatch(lowerCaseName::contains);
for (var option : charOptions) {
for (String word : name.split("_")) {
word = word.toLowerCase();

// some words should be allowed in a context, like space between should be fine when the value is a space.
if (CONTEXTUAL_WORDS.contains(word) && (word.length() * 1.0) / (lowerCaseName.length() * 1.0) < CONTEXTUAL_WORD_THRESHOLD) {
return false;
}

if (word.equals(option)) {
return true;
}
}
}

return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CheckTest {
// this is useful for debugging/executing only relevant tests
//
// example: List.of("oop.ShouldBeEnumAttribute")
private static final List<String> ONLY_TEST = List.of();
private static final List<String> ONLY_TEST = List.of("naming.ConstantsHaveDescriptiveNamesCheck");

public record Config(List<String> lines) {
public static Config fromPath(Path path) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ public class Test {
private static final String BOOK_COMMAND_NAME = "book"; /*# ok #*/
private static final String LIST_BOOKINGS_COMMAND_NAME = "list-bookings"; /*# ok #*/
private static final String REMOVE_COMMAND_NAME = "remove"; /*# ok #*/
private static final String AI_COMMAND_SEPARATOR = ","; /*# ok #*/
private static final String DEFAULT_SPACE_BETWEEN_XY = " "; /*# ok #*/
}

0 comments on commit a43965d

Please sign in to comment.