当我运行以下预提交配置时:
- repo: https://github.com/gherynos/pre-commit-java
rev: v0.2.9 # Use the ref you want to point at
hooks:
- id: pmd
# args: ["--cache"]
# exclude: /test/
- id: cpd
# exclude: /test/
- id: checkstyle
# Set the path to your Checkstyle configuration file
# args: ["-c", "checkstyle.xml"]
# exclude: /test/
checkstyle 抛出 32 个各种类型的警告,包括
InvalidJavadocPosition
。这正如预期的那样。但是,默认情况下它使用 80 个字符的 lineLength,而 google_style_java 使用 100 个字符。我想更改 checkstyle 以使用 100 个字符,我通过添加一个 checkstyle.xml
文件来实现,其内容为:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<!-- Configure the line length check with a maximum limit of 80 characters -->
<module name="LineLength">
<property name="max" value="80"/>
</module>
</module>
并启用:
args: ["-c", "checkstyle.xml"]
。
但是,后一种配置使 checkstyle only 检查 lineLength。为了解决这个问题,我可以创建一个自定义 checkstyle 配置,其中包含 checkstyle 支持的所有检查。然而,这是相当耗费人力的,更重要的是,如果 checkstyle 更新,它需要我手动更新配置。因此,我想“只使用默认的检查样式设置,因为预提交使用它”,但行长度应该是 100 而不是 80。
因此,我想问,如何使用预提交使用的默认 checkstyle 配置,除了 100(而不是 80)的行长度检查
通过在
TreeWalker
中包含其他检查找到了解决方案,例如:
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<!-- Formatting Check: Enforces a maximum line length of 80 characters -->
<module name="LineLength">
<property name="max" value="100"/>
</module>
<!-- Code Structure Check: Limits the length of a file to 1000 lines -->
<module name="FileLength">
<property name="max" value="1000"/>
</module>
<!-- Code Structure Check: Enforces Javadoc comments for package-info.java files -->
<module name="JavadocPackage"/>
<!-- TreeWalker Module: Performs checks on the abstract syntax tree -->
<module name="TreeWalker">
<!-- Formatting Check: Ensures consistent indentation -->
<!-- <module name="Indentation"> -->
<!-- <property name="basicOffset" value="2"/> -->
<!-- <property name="braceAdjustment" value="0"/> -->
<!-- <property name="caseIndent" value="0"/> -->
<!-- <property name="throwsIndent" value="2"/> -->
<!-- </module> -->
<!-- Formatting Check: Enforces right curly brace placement -->
<!-- <module name="RightCurly"> -->
<!-- <property name="option" value="alone"/> -->
<!-- </module> -->
<!-- Formatting Check: Enforces whitespace around certain tokens -->
<module name="WhitespaceAround">
<property name="allowEmptyConstructors" value="true"/>
<property name="allowEmptyMethods" value="true"/>
</module>
<!-- Code Structure Check: Disallows empty blocks -->
<module name="EmptyBlock">
<property name="option" value="text"/>
</module>
<!-- Code Structure Check: Enforces the use of braces for control statements -->
<module name="NeedBraces"/>
<!-- Formatting Check: Enforces consistent whitespace usage -->
<module name="GenericWhitespace"/>
<!-- Code Structure Check: Checks for redundant import statements -->
<module name="RedundantImport"/>
<!-- Code Structure Check: Disallows star import statements -->
<module name="AvoidStarImport"/>
<!-- Code Structure Check: Checks the placement of left curly braces -->
<module name="LeftCurly"></module>
<!-- Naming Convention Check: Enforces naming conventions for local final variables -->
<module name="LocalFinalVariableName"/>
<!-- Naming Convention Check: Enforces naming conventions for local variables -->
<module name="LocalVariableName"/>
<!-- Naming Convention Check: Enforces naming conventions for member variables -->
<module name="MemberName"/>
<!-- Naming Convention Check: Enforces naming conventions for method names -->
<module name="MethodName"/>
<!-- Naming Convention Check: Enforces naming conventions for package names -->
<module name="PackageName"/>
<!-- Naming Convention Check: Enforces naming conventions for method parameters -->
<module name="ParameterName"/>
<!-- Naming Convention Check: Enforces naming conventions for static variables -->
<module name="StaticVariableName"/>
<!-- Naming Convention Check: Enforces naming conventions for type names (classes, interfaces, enums, etc.) -->
<module name="TypeName"/>
<!-- Code Structure Check: Disallows unused import statements -->
<module name="UnusedImports"/>
<!-- Code Structure Check: Disallows unused private fields -->
<!-- <module name="UnusedPrivateField"/> -->
<!-- Code Structure Check: Enforces Javadoc comments for methods and classes -->
<module name="JavadocMethod"/>
<module name="JavadocType"/>
<!-- Code Structure Check: Disallows trailing whitespace -->
<module name="NoWhitespaceAfter"/>
<!-- Code Structure Check: Disallows whitespace before a semicolon -->
<module name="NoWhitespaceBefore"/>
<!-- Add: // CHECKSTYLE:OFF to the top of the file for which you want to skip checkstyle checks. -->
<module name="SuppressionCommentFilter"/>
</module>
</module>