Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add check-style plugin #1810

Merged
merged 7 commits into from
Apr 16, 2022
Merged

chore: add check-style plugin #1810

merged 7 commits into from
Apr 16, 2022

Conversation

seagle-yuan
Copy link
Contributor

@seagle-yuan seagle-yuan commented Apr 7, 2022

Add checkstyle plugin
implement #735

TODO:

  • forced to check with error level after fix style issues

@CLAassistant
Copy link

CLAassistant commented Apr 7, 2022

CLA assistant check
All committers have signed the CLA.

@github-actions
Copy link

github-actions bot commented Apr 7, 2022

CLA Assistant Lite bot Good! All Contributors have signed the CLA.

@seagle-yuan
Copy link
Contributor Author

I had signed CLA

</module>

<module name="LineLength">
<property name="max" value="200"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<codeStyleSettings language="JAVA">
<option name="BINARY_OPERATION_SIGN_ON_NEXT_LINE" value="true" />
</codeStyleSettings>
</code_scheme>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect an empty line at end of file

@seagle-yuan
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@seagle-yuan
Copy link
Contributor Author

seagle-yuan commented Apr 11, 2022

hugegraph checkstyle的对比

一、插件版本

hugegraph-common 3.1.0
dolphinscheduler 3.1.2
建议使用3.1.2。两个版本配置上稍微有点区别,hugegraph-common中的checkstyle.xml不能直接在高版本中使用。需要调整部分model的依赖级别

二、注释方面

hugegraph-common 采用了大量中文注释,虽然便于国内社区人员理解,但是会影响hugegraph在全球传播。
dolphinscheduler 注释误解较多,应该是某位同学大量复制并未仔细检查造成的。
建议不使用注释,或者注释全部参考官方注释,保证无歧义 https://checkstyle.sourceforge.io/

三、具体参数对比

“无” 表示 无此项配置

“默认” 表示该项配置使用默认值

对比情况“一致”表示双方配置一样,或者配置使用效果一样。“补充”表示dolphinscheduler中由此配置,但是hugegraph中无此项配置。"有冲突"表示双方均有此项配置,且不一致。

子模块名称 hugegraph版本 dolphinscher版本 对比情况 说明
FileTabCharacter eachLine:true eachLine:true 一致
LineLength 最大长度:80
ignorePattern:^package.*|
^import.*|
a href|href|
http://|
https://|ftp://
最大长度:200
ignorePattern:^ *\* *[^ ]+$
有冲突 限制每行字符长度
RegexpSingleline format:System\.out\.println 补充 根据正则表达式检查单行是否有不良操作
PackageName format:
^[a-z]+(\.[a-z][a-z0-9]*)*$
format:^[a-z]+(\.[a-z][a-z0-9]*)*$ 一致 检查包名称是否遵守命名规约
ImportOrder staticGroups:
org.apache.dolphinscheduler,
org.apache,java,javax,org,com
separatedStaticGroups:true
groups:org.apache.dolphinscheduler,
org.apache,java,javax,org,com
ordered:true
separated:true
option:top
sortStaticImportsAlphabetically:true
补充 定义导入包的顺序
AvoidStarImport 默认 默认 一致 检查没有import语句使用*号
RedundantImport 默认 默认 一致 检查是否存在多余的import语句,比如重复的,java自带的包,相同包下的其他类
UnusedImports 默认 默认 一致 检查是否存在没有使用的import语句
EmptyLineSeparator allowMultipleEmptyLines:false
allowMultipleEmptyLinesInsideClassMembers:
false
tokens:PACKAGE_DEF, IMPORT, STATIC_IMPORT,
CLASS_DEF,INTERFACE_DEF,
ENUM_DEF, STATIC_INIT,
INSTANCE_INIT, METHOD_DEF,CTOR_DEF
补充 检查空行是否符合要求。
案例https://checkstyle.sourceforge.io/config_whitespace.html#EmptyLineSeparator
ModifierOrder 默认 默认 一致 检查修饰符是否符合Java建议,顺序是:
public、protected、private、abstract、default、static、final、transient、volatile、synchronized、native、strictfp
TypeName 默认 补充 检查类名称是否符合规范
ClassTypeParameterName format:
(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)
补充 检查泛型类类型参数是否符合规范
ConstantName format:
^[A-Z][A-Z0-9](_[A-Z0-9]+)$
ds缺失 检查常量(用static final修饰的字段)的名称是否遵守命名规约
LocalVariableName format:
^[a-z][a-zA-Z0-9_]*$
tokens:VARIABLE_DEF
format:^[a-z]([a-zA-Z0-9]*)?$
有冲突 检查局部变量的名称是否遵守命名规约。
dolphinscheduler不允许有下划线
hugegraph下可以有下划线
MemberName format:
^[a-z][a-zA-Z0-9]*$
format:^[a-z][a-zA-Z0-9]*$ 一致 检查成员变量(非静态字段)的名称是否遵守命名规约
ParameterName format:
^[a-z][a-zA-Z0-9]*$
format:^[a-z]([a-zA-Z0-9]*)?$ 一致 检查参数名称是否遵守命名规约
CatchParameterName format:^[a-z]([a-zA-Z0-9]*)?$ 补充 检查catch参数名是否符合要求
ArrayTypeStyle javaStyle:true 默认 一致 检查数组是否属于java风格,方括号放在类型后面,而不是变量后面,比如:int[] nums(合法),int nums[](不合法)
MethodTypeParameterName format:
(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)
补充 检查方法类型是否遵守命名规约
MethodName format:
^[a-z][a-zA-Z0-9]*$
format:^[a-z][a-z0-9][a-zA-Z0-9_]*$ 有冲突 检查方法名称是否遵守命名规约
dolphinscheduler不允许有下划线
hugegraph下可以有下划线
InterfaceTypeParameterName format:
(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)
补充
EqualsHashCode 默认 ds缺失 覆盖equals()方法的类必须也覆盖了hashCode()方法
MissingSwitchDefault 默认 ds缺失 switch语句必须含有default子句
DefaultComesLast 默认 ds缺失 switch语句的default必须放在所有的case分支之后
SuperClone 默认 ds缺失 覆盖clone()方法时调用了super.clone()方法
NoFinalizer 补充 验证类中是否定义了finalize()方法。
GenericWhitespace 默认 补充 检查范型标记<>的周围的空格是否遵守标准规约
https://checkstyle.sourceforge.io/config_whitespace.html#GenericWhitespace
Indentation basicOffset:4
braceAdjustment:0
caseIndent:4
throwsIndent:2
lineWrappingIndentation:4
arrayInitIndent:4
补充 检查Java代码的缩进是否正确。
UpperEll 默认 默认 一致 long类型的字面量如果要以"L"结尾,必须是大写的"L",而非小写的"l"
IllegalImport regexp:4
illegalPkgs:
^com\.google\.api\.client\.repackaged,
^avro\.shaded,
^org\.apache\.hadoop\.hbase\.shaded,
^org\.apache\.hadoop\.shaded,
^javax\.ws\.rs\.ext,
^cc\.concurrent\.mango\.util\.concurrent,
^org\.apache\.curator-test\.shaded,
^com\.sun\.istack,
^org\.jetbrains\.annotations,
^jline\.internal,
^com\.cronutils\.utils,
^javax\.ws\.rs\.ext,
^org\.jboss\.netty\.util\.internal,
^com\.sun\.javafx,
^io\.reactivex\.annotations,
^org\.codehaus\.jackson
补充 检查是否有非法导入的包与类
NoWhitespaceBefore tokens:
COMMA, SEMI, POST_INC, POST_DEC,
DOT, ELLIPSIS, METHOD_REF
allowLineBreaks:true
补充 检查指定标记之前没有空格。
ParenPad 默认 默认 一致 左圆括号之后和右圆括号之前是否需要有一个空格,不需要
OperatorWrap option:eol option:NL
tokens:
BAND, BOR, BSR, BXOR, DIV, EQUAL,
GE, GT, LAND, LE,LITERAL_INSTANCEOF,
LOR, LT, MINUS, MOD,
NOT_EQUAL, PLUS, QUESTION, SL, SR, STAR, METHOD_REF
有冲突 代码换行时,运算符位置,比如:+、&&、?、: 等
dolphinscheduler要求在下一行行首
hugegraph 要求在本行末尾
EmptyStatement 默认 severity:error 有冲突 代码中不允许有空语句,也就是单独的;符号
dolphinscheduler将该错误级别置为error
AnnotationLocation allowSamelineMultipleAnnotations:false
allowSamelineSingleParameterlessAnnotation:
false
allowSamelineParameterizedAnnotation:true
tokens:METHOD_DEF, CTOR_DEF
补充 检查注解位置是否符合规范
EmptyCatchBlock exceptionVariableName:expected 补充 检查空的catch块。
CommentsIndentation 默认 补充 检查缩进与代码是否一致
NoLineWrap severity:error 补充 检查(默认import & package)是否换行
dolphinscheduler将该错误级别置为error
NeedBraces severity:error 补充 检查是否需要大括号,在if lese等场景使用
dolphinscheduler将该错误级别置为error
LeftCurly option:eol
ignoreEnums:false
severity:error 有冲突 检查{和左边的代码是否在同行
dolphinscheduler将该错误级别置为error
WhitespaceAround 默认 allowEmptyConstructors:true
allowEmptyMethods:true
allowEmptyTypes:true
allowEmptyLoops:true
有冲突 检查指定标记的周围是否有空格,比如:if、for、while、synchoronized 等
AvoidNestedBlocks allowInSwitchCase:true 补充 检查是否有嵌套块 (允许switch语句中使用)
IllegalTokenText tokens:STRING_LITERAL, CHAR_LITERAL
format:
\\u00(09|0(a|A)|0(c|C)|0(d|D)|22|27|5(C|c))|
\\(0(10|11|12|14|15|42|47)|134)
补充 检查文本内容是否非法字符
AvoidEscapedUnicodeCharacters allowEscapesForControlCharacters:true
allowByTailComment:true
allowNonPrintableEscapes:true
补充 限制使用Unicode escapes
EmptyBlock option:TEXT
tokens:LITERAL_TRY, LITERAL_FINALLY,
LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH
补充 检查空块
OneStatementPerLine allowNonPrintableEscapes:true 补充 确保每行只有一条语句
MultipleVariableDeclarations 默认 补充 检查代码段和行中是否有多次变量声明
MissingSwitchDefault 默认 补充 检测switch语句里是否有默认部分
FallThrough 默认 补充 检查switch是否缺少break、continue、return、throw
JavadocStyle endOfSentenceFormat: 补充 自定义检查以验证javadoc
JavadocType scope:protected
allowMissingParamTags:true
补充 检查javadoc的类型

Comment on lines 1 to 3
<code_scheme name="hugegraph-style" version="173">
<option name="LINE_SEPARATOR" value="&#xA;" />
<option name="RIGHT_MARGIN" value="80" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems we already have one code-style file (in dir root), u could update that directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your reminder,I have noticed this problems.
first i will delete "style/intellij-java-code-style.xml"
and then can you review "code/checkstyle.xml"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, if u update in the old file, we could see the diff easily

@javeme
Copy link
Contributor

javeme commented Apr 14, 2022

@seagle-yuan Thank you very much, your sorting is very clear.

There are 2 conflicts I think can keep the existing style:

  1. LineLength: Prefer to keep line length 80 chars, because too long code can lead to poor readability. Maybe it can be relaxed to 100 characters later if necessary.
  2. OperatorWrap: The operator location is placed at the end of the line when wrapping line, we consider that when we read the code, it means "it's not over yet, please continue to the next line".

IMO the other 5 conflicts can follow the style of dolphinscheduler.

Comment on lines 89 to 91



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one blank line is ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait a minute,let me resubmit it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had deleted unnecessary balnk lines.

@codecov
Copy link

codecov bot commented Apr 14, 2022

Codecov Report

Merging #1810 (c5d0075) into master (0a3599d) will not change coverage.
The diff coverage is n/a.

@@            Coverage Diff            @@
##             master    #1810   +/-   ##
=========================================
  Coverage     70.90%   70.90%           
  Complexity      980      980           
=========================================
  Files           446      446           
  Lines         37902    37902           
  Branches       5398     5398           
=========================================
  Hits          26876    26876           
  Misses         8296     8296           
  Partials       2730     2730           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0a3599d...c5d0075. Read the comment docs.

@seagle-yuan seagle-yuan requested review from javeme and imbajin April 15, 2022 01:15
@javeme
Copy link
Contributor

javeme commented Apr 15, 2022

recheck

<module name="MissingSwitchDefault"/>
<module name="FallThrough"/>
<module name="OuterTypeFilename">
<!-- <property name="severity" value="error"/>-->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should enable after style issues are all fixed?

</module>
<module name="LineLength">
<property name="max" value="100"/>
<property name="ignorePattern" value="^ *\* *[^ ]+$"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep ignorePattern:

^package.*|
^import.*|
a href|href|
http://|
https://|ftp://

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the different property to test and got the same result。so i think we can change this property at the next version.

"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<property name="charset" value="UTF-8"/>
<property name="severity" value="info"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should set to error after style issues are all fixed?

Copy link
Member

@imbajin imbajin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution, LGTM

Comment on lines +153 to +154
<property name="allowSamelineSingleParameterlessAnnotation"
value="false"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems we could merge 154 to 153

@imbajin imbajin changed the title add checkstyle plugin chore: add check-style plugin Apr 16, 2022
Copy link
Member

@imbajin imbajin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge first, enhance it in another pr

@imbajin imbajin merged commit c904dcb into apache:master Apr 16, 2022
@seagle-yuan seagle-yuan deleted the checkstyle-branch branch April 17, 2022 03:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants