KCR22: builder: sort rules alphabetically
This commit is contained in:
parent
d6adfa74c4
commit
c14c9ea8ca
6 changed files with 75 additions and 35 deletions
44
README.md
44
README.md
|
@ -285,22 +285,6 @@ The following is a list of each of the checks and the expectations each has on y
|
||||||
|
|
||||||
Rules are listed in alphabetical order.
|
Rules are listed in alphabetical order.
|
||||||
|
|
||||||
#### [RegexpOnFilename](http://checkstyle.sourceforge.net/config_regexp.html#RegexpOnFilename)
|
|
||||||
|
|
||||||
Checks for the existence of forbidden java file names.
|
|
||||||
|
|
||||||
File names are forbidden if they match the pattern `(.sync-conflict-| conflicted copy )`.
|
|
||||||
|
|
||||||
N.B. only `*.java` files are checked.
|
|
||||||
|
|
||||||
This check is intended to detect Syncthing and Dropbox conflict files.
|
|
||||||
|
|
||||||
e.g.
|
|
||||||
````
|
|
||||||
DataClass (Bob's conflicted copy 2017-03-11).java
|
|
||||||
DataClass.sync-conflict-20170311-1648.java
|
|
||||||
````
|
|
||||||
|
|
||||||
#### [AbbreviationAsWordInName](http://checkstyle.sourceforge.net/config_naming.html#AbbreviationAsWordInName)
|
#### [AbbreviationAsWordInName](http://checkstyle.sourceforge.net/config_naming.html#AbbreviationAsWordInName)
|
||||||
|
|
||||||
Enforces proper `CamelCase` and avoids sequences of consecutive uppercase characters in identifiers. Does not apply to @Overridden methods.
|
Enforces proper `CamelCase` and avoids sequences of consecutive uppercase characters in identifiers. Does not apply to @Overridden methods.
|
||||||
|
@ -1759,6 +1743,22 @@ Checks for redundant modifiers. Checks for:
|
||||||
* Inner interface declarations that are declared as static.
|
* Inner interface declarations that are declared as static.
|
||||||
* Class constructors.
|
* Class constructors.
|
||||||
* Nested enum definitions that are declared as static.
|
* Nested enum definitions that are declared as static.
|
||||||
|
#### [RegexpOnFilename](http://checkstyle.sourceforge.net/config_regexp.html#RegexpOnFilename)
|
||||||
|
|
||||||
|
Checks for the existence of forbidden java file names.
|
||||||
|
|
||||||
|
File names are forbidden if they match the pattern `(.sync-conflict-| conflicted copy )`.
|
||||||
|
|
||||||
|
N.B. only `*.java` files are checked.
|
||||||
|
|
||||||
|
This check is intended to detect Syncthing and Dropbox conflict files.
|
||||||
|
|
||||||
|
e.g.
|
||||||
|
````
|
||||||
|
DataClass (Bob's conflicted copy 2017-03-11).java
|
||||||
|
DataClass.sync-conflict-20170311-1648.java
|
||||||
|
````
|
||||||
|
|
||||||
#### [RequireThis](http://checkstyle.sourceforge.net/config_coding.html#RequireThis)
|
#### [RequireThis](http://checkstyle.sourceforge.net/config_coding.html#RequireThis)
|
||||||
|
|
||||||
Checks that references to instance fields where a parameter name overlaps are qualified by `this.`.
|
Checks that references to instance fields where a parameter name overlaps are qualified by `this.`.
|
||||||
|
@ -1950,12 +1950,12 @@ Prevents the use of `@SuppressWarnings` for the following checks:
|
||||||
* [PackageDeclaration](#packagedeclaration)
|
* [PackageDeclaration](#packagedeclaration)
|
||||||
* [TypeName](#typename)
|
* [TypeName](#typename)
|
||||||
* [VisibilityModifier](#visibilitymodifier)
|
* [VisibilityModifier](#visibilitymodifier)
|
||||||
#### [SuppressWarningsHolder](http://checkstyle.sourceforge.net/config_annotation.html#SuppressWarningsHolder)
|
|
||||||
|
|
||||||
Used by Checkstyle to hold the checks to be suppressed from `@SuppressWarnings(...)` annotations.
|
|
||||||
#### [SuppressWarningsFilter](http://checkstyle.sourceforge.net/config_annotation.html#SuppressWarningsFilter)
|
#### [SuppressWarningsFilter](http://checkstyle.sourceforge.net/config_annotation.html#SuppressWarningsFilter)
|
||||||
|
|
||||||
Allows the use of the `@SuppressWarnings` annotation.
|
Allows the use of the `@SuppressWarnings` annotation.
|
||||||
|
#### [SuppressWarningsHolder](http://checkstyle.sourceforge.net/config_annotation.html#SuppressWarningsHolder)
|
||||||
|
|
||||||
|
Used by Checkstyle to hold the checks to be suppressed from `@SuppressWarnings(...)` annotations.
|
||||||
#### [ThrowsCount](http://checkstyle.sourceforge.net/config_design.html#ThrowsCount)
|
#### [ThrowsCount](http://checkstyle.sourceforge.net/config_design.html#ThrowsCount)
|
||||||
|
|
||||||
Restricts non-private methods to only `throws` 4 distinct Exception types. Exceptions should be hierarchical to allow catching suitable root Exceptions.
|
Restricts non-private methods to only `throws` 4 distinct Exception types. Exceptions should be hierarchical to allow catching suitable root Exceptions.
|
||||||
|
@ -2667,9 +2667,6 @@ Generic rule; doesn't embody a 'quality' check.
|
||||||
|
|
||||||
As the sevntu check are considered experimental not all those that are not enabled are listed here. Only where they are disabled due to a conflict with my 'style' or there is another irreconcilable difference that prevents them from being enabled, will they be documented to prevent repeated investigations.
|
As the sevntu check are considered experimental not all those that are not enabled are listed here. Only where they are disabled due to a conflict with my 'style' or there is another irreconcilable difference that prevents them from being enabled, will they be documented to prevent repeated investigations.
|
||||||
|
|
||||||
#### [ForbidWildcardAsReturnType](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/design/ForbidWildcardAsReturnTypeCheck.html)
|
|
||||||
|
|
||||||
Causes `NullPointerException` when used with `@Value.Immutables` from `org.immutables:value`
|
|
||||||
#### [AvoidConditionInversion](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/design/AvoidConditionInversionCheck.html)
|
#### [AvoidConditionInversion](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/design/AvoidConditionInversionCheck.html)
|
||||||
|
|
||||||
Should already be covered by [SimplifyBooleanExpression](simplifybooleanexpression).
|
Should already be covered by [SimplifyBooleanExpression](simplifybooleanexpression).
|
||||||
|
@ -2706,6 +2703,9 @@ Generic rule; doesn't embody a 'quality' check.
|
||||||
#### [ForbidThrowAnonymousExceptions](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/coding/ForbidThrowAnonymousExceptionsCheck.html)
|
#### [ForbidThrowAnonymousExceptions](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/coding/ForbidThrowAnonymousExceptionsCheck.html)
|
||||||
|
|
||||||
[IllegalThrows](#illegalthrows) performs a similar check.
|
[IllegalThrows](#illegalthrows) performs a similar check.
|
||||||
|
#### [ForbidWildcardAsReturnType](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/design/ForbidWildcardAsReturnTypeCheck.html)
|
||||||
|
|
||||||
|
Causes `NullPointerException` when used with `@Value.Immutables` from `org.immutables:value`
|
||||||
#### [RequiredParameterForAnnotation](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/annotation/RequiredParameterForAnnotationCheck.html)
|
#### [RequiredParameterForAnnotation](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/annotation/RequiredParameterForAnnotationCheck.html)
|
||||||
|
|
||||||
Generic rule; doesn't embody a 'quality' check.
|
Generic rule; doesn't embody a 'quality' check.
|
||||||
|
|
|
@ -24,10 +24,8 @@ package net.kemitix.checkstyle.ruleset.builder;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,15 +47,11 @@ public class DefaultReadmeIndexBuilder implements ReadmeIndexBuilder {
|
||||||
public final String build() {
|
public final String build() {
|
||||||
return rulesProperties.getRules()
|
return rulesProperties.getRules()
|
||||||
.stream()
|
.stream()
|
||||||
.sorted(Comparator.comparing(lowerCaseRuleName()))
|
.sorted(Rule::sortByName)
|
||||||
.map(this::formatRuleRow)
|
.map(this::formatRuleRow)
|
||||||
.collect(Collectors.joining(NEWLINE));
|
.collect(Collectors.joining(NEWLINE));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Function<Rule, String> lowerCaseRuleName() {
|
|
||||||
return this::getLowerCaseRuleName;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getLowerCaseRuleName(final Rule rule) {
|
private String getLowerCaseRuleName(final Rule rule) {
|
||||||
return rule.getName()
|
return rule.getName()
|
||||||
.toLowerCase(LOCALE);
|
.toLowerCase(LOCALE);
|
||||||
|
|
|
@ -88,6 +88,7 @@ class ReadmeWriter implements CommandLineRunner {
|
||||||
return rulesProperties.getRules()
|
return rulesProperties.getRules()
|
||||||
.stream()
|
.stream()
|
||||||
.filter(predicate)
|
.filter(predicate)
|
||||||
|
.sorted(Rule::sortByName)
|
||||||
.flatMap(ruleReadmeLoader::load)
|
.flatMap(ruleReadmeLoader::load)
|
||||||
.collect(Collectors.joining(NEWLINE));
|
.collect(Collectors.joining(NEWLINE));
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ import lombok.Setter;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,6 +38,8 @@ import java.util.Map;
|
||||||
@Getter
|
@Getter
|
||||||
public class Rule {
|
public class Rule {
|
||||||
|
|
||||||
|
private static final Locale LOCALE = Locale.ENGLISH;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name of the rule's Check class.
|
* The name of the rule's Check class.
|
||||||
*/
|
*/
|
||||||
|
@ -81,4 +84,24 @@ public class Rule {
|
||||||
* Configuration properties.
|
* Configuration properties.
|
||||||
*/
|
*/
|
||||||
private final Map<String, String> properties = new HashMap<>();
|
private final Map<String, String> properties = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compare two Rules lexicographically for sorting by rule name, ignoring case.
|
||||||
|
*
|
||||||
|
* @param left the first rule
|
||||||
|
* @param right the second rule
|
||||||
|
*
|
||||||
|
* @return the value 0 if the rules are equal; a value less than 0 if the left string is lexicographically less than
|
||||||
|
* the right string; and a value greater than 0 if the left string is lexicographically greater than the right
|
||||||
|
* string.
|
||||||
|
*/
|
||||||
|
static int sortByName(final Rule left, final Rule right) {
|
||||||
|
return left.getLowerCaseRuleName()
|
||||||
|
.compareTo(right.getLowerCaseRuleName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getLowerCaseRuleName() {
|
||||||
|
return getName().toLowerCase(LOCALE);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,10 +73,10 @@ public class ReadmeWriterTest {
|
||||||
"sd:sevntu-disabled"
|
"sd:sevntu-disabled"
|
||||||
);
|
);
|
||||||
val rules = rulesProperties.getRules();
|
val rules = rulesProperties.getRules();
|
||||||
final Rule checkstyleEnabled = rule(RuleSource.CHECKSTYLE, true);
|
final Rule checkstyleEnabled = rule(RuleSource.CHECKSTYLE, true, "checkstyle enabled");
|
||||||
final Rule checkstyleDisabled = rule(RuleSource.CHECKSTYLE, false);
|
final Rule checkstyleDisabled = rule(RuleSource.CHECKSTYLE, false, "checkstyle disabled");
|
||||||
final Rule sevntuEnabled = rule(RuleSource.SEVNTU, true);
|
final Rule sevntuEnabled = rule(RuleSource.SEVNTU, true, "sevntu enabled");
|
||||||
final Rule sevntuDisabled = rule(RuleSource.SEVNTU, false);
|
final Rule sevntuDisabled = rule(RuleSource.SEVNTU, false, "sevntu disabled");
|
||||||
rules.add(checkstyleEnabled);
|
rules.add(checkstyleEnabled);
|
||||||
rules.add(checkstyleDisabled);
|
rules.add(checkstyleDisabled);
|
||||||
rules.add(sevntuEnabled);
|
rules.add(sevntuEnabled);
|
||||||
|
@ -93,10 +93,33 @@ public class ReadmeWriterTest {
|
||||||
assertThat(lines).containsExactlyElementsOf(expected);
|
assertThat(lines).containsExactlyElementsOf(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Rule rule(final RuleSource source, final boolean enabled) {
|
private Rule rule(final RuleSource source, final boolean enabled, final String name) {
|
||||||
val rule = new Rule();
|
val rule = new Rule();
|
||||||
|
rule.setName(name);
|
||||||
rule.setSource(source);
|
rule.setSource(source);
|
||||||
rule.setEnabled(enabled);
|
rule.setEnabled(enabled);
|
||||||
return rule;
|
return rule;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void rulesAreAlphabetical() throws Exception {
|
||||||
|
//given
|
||||||
|
val expected = Arrays.asList("ce:alpha", "beta", "delta");
|
||||||
|
val rules = rulesProperties.getRules();
|
||||||
|
final Rule alpha = rule(RuleSource.CHECKSTYLE, true, "alpha");
|
||||||
|
final Rule beta = rule(RuleSource.CHECKSTYLE, true, "beta");
|
||||||
|
final Rule delta = rule(RuleSource.CHECKSTYLE, true, "delta");
|
||||||
|
rules.add(alpha);
|
||||||
|
rules.add(delta);
|
||||||
|
rules.add(beta);
|
||||||
|
given(indexBuilder.build()).willReturn("index");
|
||||||
|
given(ruleReadmeLoader.load(alpha)).willReturn(Stream.of(alpha.getName()));
|
||||||
|
given(ruleReadmeLoader.load(beta)).willReturn(Stream.of(beta.getName()));
|
||||||
|
given(ruleReadmeLoader.load(delta)).willReturn(Stream.of(delta.getName()));
|
||||||
|
//when
|
||||||
|
readmeWriter.run();
|
||||||
|
//then
|
||||||
|
final Stream<String> lines = Files.lines(readme, StandardCharsets.UTF_8);
|
||||||
|
assertThat(lines).containsSequence(expected);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,6 @@ public class CheckConfigurationTest {
|
||||||
.sourceDirectory(sourceDirectory)
|
.sourceDirectory(sourceDirectory)
|
||||||
.toString();
|
.toString();
|
||||||
//then
|
//then
|
||||||
System.out.println("result = " + result);
|
|
||||||
assertThat(result).contains("rulesetVersion=ruleset version")
|
assertThat(result).contains("rulesetVersion=ruleset version")
|
||||||
.contains("sourceDirectory=source directory");
|
.contains("sourceDirectory=source directory");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue