Scalastyle Scala代码风格检测

独自空忆成欢 提交于 2020-01-15 01:47:18

记录下Scalastyle规则,方便查询。内容比较多,使用Ctrl + F 输入需要查询的检查器进行搜索查询。

目录

介绍

规则

使用示例

Maven集成Scalastyle

Spark的Scalastyle-config.xml


介绍

Scalastyle 是专门针对Scala代码开发的轻巧的code style检测工具,类似于Java代码检查工具Checkstyle、Findbugs等。可以在Maven中集成到项目中,方便代码风格的检测。

最下面附上了Spark2.4.3的 Scalastyle 配置代码,可以借鉴。

Scalastyle常用关键字 //scalastyle:on//scalastyle:off,如:

//scalastyle:on println
	if (args.length < 2) {
	  println("Usage:")
	  println("DRIVER_MEMORY=[mem] spark-submit " +
		"com.github.ehiggs.spark.terasort.TeraGen " +
		"spark-terasort-1.0-SNAPSHOT-with-dependencies.jar " +
		"[output-size] [output-directory]")
	  println(" ")
	  println("Example:")
	  println("DRIVER_MEMORY=50g spark-submit " +
		"com.github.ehiggs.spark.terasort.TeraGen " +
		"spark-terasort-1.0-SNAPSHOT-with-dependencies.jar " +
		"100G file:///scratch/username/terasort_in")
	  System.exit(0)
	}
//scalastyle:off println

规则

官网说明:已实现的规则 

目前已实现规则有69条

检查器 解释
org.scalastyle.file.FileLengthChecker 检查文件中的行数
org.scalastyle.file.FileLineLengthChecker 检查一行中的字符数
org.scalastyle.file.FileTabChecker 检查文件中是否有标签
org.scalastyle.file.HeaderMatchesChecker 检查每个文件的前几行与文本匹配
org.scalastyle.file.IndentationChecker 检查行是否由Tab的倍数缩进
org.scalastyle.file.NewLineAtEofChecker 检查文件是否以换行符结尾
org.scalastyle.file.NoNewLineAtEofChecker 检查文件不是以换行符结尾
org.scalastyle.file.RegexChecker 检查正则表达式是否匹配
org.scalastyle.file.WhitespaceEndOfLineChecker 检查行尾没有空格
org.scalastyle.scalariform.BlockImportChecker 检查导入块是否使用
org.scalastyle.scalariform.ClassNamesChecker 检查类名是否与正则表达式匹配
org.scalastyle.scalariform.ClassTypeParameterChecker 检查类的类型参数与正则表达式匹配
org.scalastyle.scalariform.CovariantEqualsChecker 检查类和对象在没有覆盖的情况下定义了相等(java.lang.object)
org.scalastyle.scalariform.CyclomaticComplexityChecker 检查方法的参数复杂度是否超过设定值
org.scalastyle.scalariform.DeprecatedJavaChecker 检查应该用Scala @deprecated代替Java @Deprecated
org.scalastyle.scalariform.DisallowSpaceAfterTokenChecker 检查某些既定标记后不允许使用空格
org.scalastyle.scalariform.DisallowSpaceBeforeTokenChecker 检查某些既定标记前不允许使用空格
org.scalastyle.scalariform.EmptyClassChecker 检查类或抽象类没有成员,则可以省略大括号
org.scalastyle.scalariform.EmptyInterpolatedStringChecker 检查是否有对字符串插入空值
org.scalastyle.scalariform.EnsureSingleSpaceAfterTokenChecker 检查某些既定标记后是否有空间
org.scalastyle.scalariform.EnsureSingleSpaceBeforeTokenChecker 检查某些既定标记前是否有空间
org.scalastyle.scalariform.EqualsHashCodeChecker 检查如果一个类实现了或等于Hash,那它应该实现另一个类
org.scalastyle.scalariform.FieldNamesChecker 检查字段名是否与正则表达式匹配
org.scalastyle.scalariform.ForBraceChecker 检查for是否使用大括号
org.scalastyle.scalariform.IfBraceChecker 检查if是否使用大括号
org.scalastyle.scalariform.IllegalImportsChecker 检查类没有导入某些类
org.scalastyle.scalariform.ImportGroupingChecker 检查导入是否分组
org.scalastyle.scalariform.ImportOrderChecker 检查是否根据样式配置对导入进行分组和排序
org.scalastyle.scalariform.LowercasePatternMatchChecker 检查case语句模式匹配是否为小写,因为这可能导致混淆
org.scalastyle.scalariform.MagicNumberChecker 检查魔数是否使用var定义
org.scalastyle.scalariform.MethodArgumentNamesChecker 检查方法参数名是否与正则表达式匹配
org.scalastyle.scalariform.MethodLengthChecker 检查方法不超过最大长度
org.scalastyle.scalariform.MethodNamesChecker 检查方法名是否与正则表达式匹配
org.scalastyle.scalariform.MultipleStringLiteralsChecker 检查字符串文字没有多次出现
org.scalastyle.scalariform.NamedArgumentChecker 检查参数是否命名
org.scalastyle.scalariform.NoCloneChecker 检查类和对象没有定义clone()方法
org.scalastyle.scalariform.NoFinalizeChecker 检查类和对象是否定义了Finish()方法
org.scalastyle.scalariform.NoWhitespaceAfterLeftBracketChecker 检查左括号‘[’后没有空格
org.scalastyle.scalariform.NoWhitespaceBeforeLeftBracketChecker 检查左括号‘[’之前没有空格
org.scalastyle.scalariform.NoWhitespaceBeforeRightBracketChecker 检查右括号“]”之前没有空格
org.scalastyle.scalariform.NonASCIICharacterChecker 检查是否使用非ASCII字符(Unicode字符)
org.scalastyle.scalariform.NotImplementedErrorUsage 检查代码是否有 ??? 操作符
org.scalastyle.scalariform.NullChecker 检查是否使用NULL
org.scalastyle.scalariform.NumberOfMethodsInTypeChecker 检查一个类/特性/对象是否申明太多的方法
org.scalastyle.scalariform.NumberOfTypesChecker 检查文件中声明的类型是否过多
org.scalastyle.scalariform.ObjectNamesChecker 检查对象名称是否与正则表达式匹配
org.scalastyle.scalariform.OverrideJavaChecker 检查java@重写是否被使用
org.scalastyle.scalariform.PackageNamesChecker 检查包名是否与正则表达式匹配
org.scalastyle.scalariform.PackageObjectNamesChecker 检查包对象名是否与正则表达式匹配
org.scalastyle.scalariform.ParameterNumberChecker 检查方法的参数个数是否超过设置数量
org.scalastyle.scalariform.PatternMatchAlignChecker 检查模式匹配箭头对齐
org.scalastyle.scalariform.ProcedureDeclarationChecker 检查申明过程 a:unit= 是否使用=号
org.scalastyle.scalariform.PublicMethodsHaveTypeChecker 检查方法是否具有显式返回类型
org.scalastyle.scalariform.RedundantIfChecker 检查表达式是否是冗余,如,if (cond) false else true
org.scalastyle.scalariform.ReturnChecker 检查是否使用return
org.scalastyle.scalariform.ScalaDocChecker 检查代码格式是否遵循Scaladoc文档说明
org.scalastyle.scalariform.SimplifyBooleanExpressionChecker 检查Boolean表达式是否可以简化
org.scalastyle.scalariform.SpaceAfterCommentStartChecker 检查在注释后是否有一个空格
org.scalastyle.scalariform.SpacesAfterPlusChecker 检查加号后面是否有空格
org.scalastyle.scalariform.SpacesBeforePlusChecker 检查加号前面是否有空格
org.scalastyle.scalariform.StructuralTypeChecker 检查结构类型是否未被使用
org.scalastyle.scalariform.TodoCommentChecker 检查是否使用Todo/Fixme单行注释
org.scalastyle.scalariform.TokenChecker 检查如果有正则表达式,是否在特殊标记中匹配
org.scalastyle.scalariform.UnderscoreImportChecker 检查是否使用通配符导入
org.scalastyle.scalariform.UppercaseLChecker 检查如果使用长字符串,则使用大写字母
org.scalastyle.scalariform.VarFieldChecker 检查类和对象是否定义可变字段
org.scalastyle.scalariform.VarLocalChecker 检查函数是否定义可变变量
org.scalastyle.scalariform.WhileChecker 检查是否使用While循环
org.scalastyle.scalariform.XmlLiteralChecker 检查是否未使用xml文本

使用示例

主要在 scalastyle-config.xml 中设置。

 org.scalastyle.file.FileLengthChecker    检查文件中的行数

<check enabled="true" class="org.scalastyle.file.FileLengthChecker" level="warning">
 <parameters>
  <parameter name="maxFileLength">800</parameter>
 </parameters>
</check>

org.scalastyle.file.FileLineLengthChecker    检查一行中的字符数

<check enabled="true" class="org.scalastyle.file.FileLineLengthChecker" level="warning">
 <parameters>
  <parameter name="maxLineLength">100</parameter>
  <parameter name="tabSize">2</parameter>
  <parameter name="ignoreImports">true</parameter>
 </parameters>
</check>

org.scalastyle.file.FileTabChecker    检查文件中是否有标签

<check enabled="true" class="org.scalastyle.file.FileTabChecker" level="warning"/>

org.scalastyle.file.HeaderMatchesChecker    检查每个文件的前几行与文本匹配

<check enabled="true" class="org.scalastyle.file.HeaderMatchesChecker" level="warning">
 <parameters>
  <parameter name="regex">false</parameter>
  <parameter name="header">// Copyright \(C\) 2011-2012 the original author or authors.</parameter>
 </parameters>
</check>

或者 

<check enabled="true" class="org.scalastyle.file.HeaderMatchesChecker" level="warning">
 <parameters>
  <parameter name="regex">true</parameter>
  <parameter name="header">// Copyright \(C\) (?:\d{4}-)?\d{4} the original author or authors.</parameter>
 </parameters>
</check>

org.scalastyle.file.IndentationChecker    检查行是否由Tab的倍数缩进

<check enabled="true" class="org.scalastyle.file.IndentationChecker" level="warning">
 <parameters>
  <parameter name="tabSize">2</parameter>
  <parameter name="methodParamIndentSize">2</parameter>
  <parameter name="classParamIndentSize">4</parameter>
 </parameters>
</check>

org.scalastyle.file.NewLineAtEofChecker    检查文件是否以换行符结尾

<check enabled="true" class="org.scalastyle.file.NewLineAtEofChecker" level="warning"/>

org.scalastyle.file.NoNewLineAtEofChecker    检查文件不是以换行符结尾

<check enabled="true" class="org.scalastyle.file.NoNewLineAtEofChecker" level="warning"/>

org.scalastyle.file.RegexChecker    检查正则表达式是否匹配

<check enabled="true" class="org.scalastyle.file.RegexChecker" level="warning">
 <parameters>
  <parameter name="regex">(?m)^\s*$(\r|)\n^\s*$(\r|)\n</parameter>
  <parameter name="line">false</parameter>
 </parameters>
 <customMessage>No double blank lines</customMessage>
</check>

org.scalastyle.file.WhitespaceEndOfLineChecker    检查行尾没有空格

<check enabled="true" class="org.scalastyle.file.WhitespaceEndOfLineChecker" level="warning">
 <parameters>
  <parameter default="false" type="boolean" name="ignoreWhitespaceLines"/>
 </parameters>
</check>

org.scalastyle.scalariform.BlockImportChecker    检查导入块是否使用

<check enabled="true" class="org.scalastyle.scalariform.BlockImportChecker" level="warning"/>

org.scalastyle.scalariform.ClassNamesChecker    检查类名是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.ClassNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[A-Z][A-Za-z]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.ClassTypeParameterChecker    检查类的类型参数与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.ClassTypeParameterChecker" level="warning">
 <parameters>
  <parameter name="regex">^[A-Z_]$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.CovariantEqualsChecker    检查类和对象在没有覆盖的情况下定义了相等(java.lang.object)

<check enabled="true" class="org.scalastyle.scalariform.CovariantEqualsChecker" level="warning"/>

org.scalastyle.scalariform.CyclomaticComplexityChecker    检查方法的参数复杂度是否超过设定值

<check enabled="true" class="org.scalastyle.scalariform.CyclomaticComplexityChecker" level="warning">
 <parameters>
  <parameter name="maximum">10</parameter>
  <parameter name="countCases">true</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.DeprecatedJavaChecker    检查应该用Scala @deprecated代替Java @Deprecated

<check enabled="true" class="org.scalastyle.scalariform.DeprecatedJavaChecker" level="warning"/>

org.scalastyle.scalariform.DisallowSpaceAfterTokenChecker    检查某些既定标记后不允许使用空格

<check enabled="true" class="org.scalastyle.scalariform.DisallowSpaceAfterTokenChecker" level="warning">
 <parameters>
  <parameter name="tokens">LPAREN</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.DisallowSpaceBeforeTokenChecker    检查某些既定标记前不允许使用空格

<check enabled="true" class="org.scalastyle.scalariform.DisallowSpaceBeforeTokenChecker" level="warning">
 <parameters>
  <parameter name="tokens">COLON, COMMA, RPAREN</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.EmptyClassChecker    检查类或抽象类没有成员,则可以省略大括号

<check enabled="true" class="org.scalastyle.scalariform.EmptyClassChecker" level="warning"/>

org.scalastyle.scalariform.EmptyInterpolatedStringChecker    检查是否有对字符串插入空值

<check enabled="true" level="warning" class="org.scalastyle.scalariform.EmptyInterpolatedStringChecker"/>

org.scalastyle.scalariform.EnsureSingleSpaceAfterTokenChecker    检查某些既定标记后是否有空间

<check enabled="true" class="org.scalastyle.scalariform.EnsureSingleSpaceAfterTokenChecker" level="warning">
 <parameters>
  <parameter name="tokens">COLON, IF</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.EnsureSingleSpaceBeforeTokenChecker    检查某些既定标记前是否有空间

<check enabled="true" class="org.scalastyle.scalariform.EnsureSingleSpaceBeforeTokenChecker" level="warning">
 <parameters>
  <parameter name="tokens">LPAREN</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.EqualsHashCodeChecker    检查如果一个类实现了或等于Hash,那它应该实现另一个类

<check enabled="true" class="org.scalastyle.scalariform.EqualsHashCodeChecker" level="warning"/>

org.scalastyle.scalariform.FieldNamesChecker    检查字段名是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.FieldNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[a-z][A-Za-z0-9]*$</parameter>
  <parameter name="objectFieldRegex">^[A-Z][A-Za-z0-9]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.ForBraceChecker    检查for是否使用大括号

<check enabled="true" class="org.scalastyle.scalariform.ForBraceChecker" level="warning"/>

org.scalastyle.scalariform.IfBraceChecker    检查if是否使用大括号

<check enabled="true" class="org.scalastyle.scalariform.IfBraceChecker" level="warning">
 <parameters>
  <parameter name="singleLineAllowed">true</parameter>
  <parameter name="doubleLineAllowed">false</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.IllegalImportsChecker    检查类没有导入某些类

<check enabled="true" class="org.scalastyle.scalariform.IllegalImportsChecker" level="warning"/>

org.scalastyle.scalariform.ImportGroupingChecker    检查导入是否分组

<check enabled="true" class="org.scalastyle.scalariform.ImportGroupingChecker" level="warning"/>

org.scalastyle.scalariform.ImportOrderChecker    检查是否根据样式配置对导入进行分组和排序

<check enabled="true" class="org.scalastyle.scalariform.ImportOrderChecker" level="warning">
 <parameters>
  <parameter name="groups">java,scala,others</parameter>
  <parameter name="group.java">javax?\..+</parameter>
  <parameter name="group.scala">scala\..+</parameter>
  <parameter name="group.others">.+</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.LowercasePatternMatchChecker    检查case语句模式匹配是否为小写,因为这可能导致混淆

<check enabled="true" class="org.scalastyle.scalariform.LowercasePatternMatchChecker" level="warning"/>

org.scalastyle.scalariform.MagicNumberChecker    检查魔数是否使用var定义

<check enabled="true" class="org.scalastyle.scalariform.MagicNumberChecker" level="warning">
 <parameters>
  <parameter name="ignore">-1,0,1,2,3</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.MethodArgumentNamesChecker    检查方法参数名是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.MethodArgumentNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[a-z][A-Za-z0-9]*$</parameter>
  <parameter name="ignoreRegex">^$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.MethodLengthChecker    检查方法不超过最大长度

<check enabled="true" class="org.scalastyle.scalariform.MethodLengthChecker" level="warning">
 <parameters>
  <parameter name="maxLength">50</parameter>
  <parameter name="ignoreComments">false</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.MethodNamesChecker    检查方法名是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.MethodNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[A-Za-z]*$</parameter>
  <parameter name="ignoreRegex">^.*$</parameter>
  <parameter name="ignoreOverride">false</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.MultipleStringLiteralsChecker    检查字符串文字没有多次出现

<check enabled="true" class="org.scalastyle.scalariform.MultipleStringLiteralsChecker" level="warning">
 <parameters>
  <parameter name="allowed">1</parameter>
  <parameter name="ignoreRegex">^\&quot;\&quot;$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.NamedArgumentChecker    检查参数是否命名

<check enabled="true" class="org.scalastyle.scalariform.NamedArgumentChecker" level="warning">
 <parameters>
  <parameter name="checkString">false</parameter>
  <parameter name="ignoreMethod">^set.+$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.NoCloneChecker    检查类和对象没有定义clone()方法

<check enabled="true" class="org.scalastyle.scalariform.NoCloneChecker" level="warning"/>

org.scalastyle.scalariform.NoFinalizeChecker    检查类和对象是否定义了Finish()方法

<check enabled="true" class="org.scalastyle.scalariform.NoFinalizeChecker" level="warning"/>

org.scalastyle.scalariform.NoWhitespaceAfterLeftBracketChecker    检查左括号‘[’后没有空格

<check enabled="true" class="org.scalastyle.scalariform.NoWhitespaceAfterLeftBracketChecker" level="warning"/>

org.scalastyle.scalariform.NoWhitespaceBeforeLeftBracketChecker    检查左括号‘[’之前没有空格

<check enabled="true" class="org.scalastyle.scalariform.NoWhitespaceBeforeLeftBracketChecker" level="warning"/>

org.scalastyle.scalariform.NoWhitespaceBeforeRightBracketChecker    检查右括号“]”之前没有空格

<check enabled="true" class="org.scalastyle.scalariform.NoWhitespaceBeforeRightBracketChecker" level="warning"/>

org.scalastyle.scalariform.NonASCIICharacterChecker    检查是否使用非ASCII字符(Unicode字符)

<check enabled="true" class="org.scalastyle.scalariform.NonASCIICharacterChecker" level="warning"/>

org.scalastyle.scalariform.NotImplementedErrorUsage    检查代码是否有 ??? 操作符

<check enabled="true" class="org.scalastyle.scalariform.NotImplementedErrorUsage" level="warning"/>

org.scalastyle.scalariform.NullChecker    检查是否使用NULL

<check enabled="true" class="org.scalastyle.scalariform.NullChecker" level="warning">
 <parameters>
  <parameter name="allowNullChecks">true</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.NumberOfMethodsInTypeChecker    检查一个类/特性/对象是否申明太多的方法

<check enabled="true" class="org.scalastyle.scalariform.NumberOfMethodsInTypeChecker" level="warning">
 <parameters>
  <parameter name="maxMethods">30</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.NumberOfTypesChecker    检查文件中声明的类型是否过多

<check enabled="true" class="org.scalastyle.scalariform.NumberOfTypesChecker" level="warning">
 <parameters>
  <parameter name="maxTypes">20</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.ObjectNamesChecker    检查对象名称是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.ObjectNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[A-Z][A-Za-z]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.OverrideJavaChecker    检查java@重写是否被使用

<check enabled="true" class="org.scalastyle.scalariform.OverrideJavaChecker" level="warning"/>

org.scalastyle.scalariform.PackageNamesChecker    检查包名是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.PackageNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[a-z][A-Za-z]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.PackageObjectNamesChecker    检查包对象名是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.PackageObjectNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[a-z][A-Za-z]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.ParameterNumberChecker    检查方法的参数个数是否超过设置数量

<check enabled="true" class="org.scalastyle.scalariform.ParameterNumberChecker" level="warning">
 <parameters>
  <parameter name="maxParameters">8</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.PatternMatchAlignChecker    检查模式匹配箭头对齐

<check enabled="true" class="org.scalastyle.scalariform.PatternMatchAlignChecker" level="warning"/>

org.scalastyle.scalariform.ProcedureDeclarationChecker    检查申明过程 a:unit= 是否使用=号

<check enabled="true" class="org.scalastyle.scalariform.ProcedureDeclarationChecker" level="warning"/>

org.scalastyle.scalariform.PublicMethodsHaveTypeChecker    检查方法是否具有显式返回类型

<check enabled="true" class="org.scalastyle.scalariform.PublicMethodsHaveTypeChecker" level="warning">
 <parameters>
  <parameter name="ignoreOverride">false</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.RedundantIfChecker    检查表达式是否是冗余,如,if (cond) false else true

<check enabled="true" class="org.scalastyle.scalariform.RedundantIfChecker" level="warning"/>

org.scalastyle.scalariform.ReturnChecker    检查是否使用return

<check enabled="true" class="org.scalastyle.scalariform.ReturnChecker" level="warning"/>

org.scalastyle.scalariform.ScalaDocChecker    检查代码格式是否遵循Scaladoc文档说明

<check enabled="true" class="org.scalastyle.scalariform.ScalaDocChecker" level="warning">
 <parameters>
  <parameter name="ignoreRegex">(.*Spec$)|(.*SpecIT$)</parameter>
  <parameter name="ignoreTokenTypes">PatDefOrDcl,TypeDefOrDcl,FunDefOrDcl,TmplDef</parameter>
  <parameter name="ignoreOverride">false</parameter>
  <parameter name="indentStyle">anydoc</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.SimplifyBooleanExpressionChecker    检查Boolean表达式是否可以简化

<check enabled="true" class="org.scalastyle.scalariform.SimplifyBooleanExpressionChecker" level="warning"/>

org.scalastyle.scalariform.SpaceAfterCommentStartChecker    检查在注释后是否有一个空格

<check enabled="true" class="org.scalastyle.scalariform.SpaceAfterCommentStartChecker" level="warning"/>

org.scalastyle.scalariform.SpacesAfterPlusChecker    检查加号后面是否有空格

<check enabled="true" class="org.scalastyle.scalariform.SpacesAfterPlusChecker" level="warning"/>

org.scalastyle.scalariform.SpacesBeforePlusChecker    检查加号前面是否有空格

<check enabled="true" class="org.scalastyle.scalariform.SpacesBeforePlusChecker" level="warning"/>

org.scalastyle.scalariform.StructuralTypeChecker    检查结构类型是否未被使用

<check enabled="true" class="org.scalastyle.scalariform.StructuralTypeChecker" level="warning"/>

org.scalastyle.scalariform.TodoCommentChecker    检查是否使用Todo/Fixme单行注释

<check enabled="true" class="org.scalastyle.scalariform.TodoCommentChecker" level="warning">
 <parameters>
  <parameter default="TODO|FIXME" type="string" name="words"/>
 </parameters>
</check>

org.scalastyle.scalariform.TokenChecker    检查如果有正则表达式,是否在特殊标记中匹配

<check enabled="true" class="org.scalastyle.scalariform.TokenChecker" level="warning">
 <parameters>
  <parameter name="regex">^[ai]sInstanceOf$</parameter>
 </parameters>
 <customMessage>Avoid casting.</customMessage>
</check>

org.scalastyle.scalariform.UnderscoreImportChecker    检查是否使用通配符导入

<check enabled="true" class="org.scalastyle.scalariform.UnderscoreImportChecker" level="warning">
 <parameters>
  <parameter name="ignoreRegex">collection\.JavaConverters\._|scala\.concurrent\.duration\._</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.UppercaseLChecker    检查如果使用长字符串,则使用大写字母

<check enabled="true" class="org.scalastyle.scalariform.UppercaseLChecker" level="warning"/>

org.scalastyle.scalariform.VarFieldChecker    检查类和对象是否定义可变字段

<check enabled="true" class="org.scalastyle.scalariform.VarFieldChecker" level="warning"/>

org.scalastyle.scalariform.VarLocalChecker    检查函数是否定义可变变量

<check enabled="true" class="org.scalastyle.scalariform.VarLocalChecker" level="warning"/>

org.scalastyle.scalariform.WhileChecker    检查是否使用While循环

<check enabled="true" class="org.scalastyle.scalariform.WhileChecker" level="warning"/>

org.scalastyle.scalariform.XmlLiteralChecker    检查是否未使用xml文本

<check enabled="true" class="org.scalastyle.scalariform.XmlLiteralChecker" level="warning"/>

 

Maven集成Scalastyle

如Spark的pom.xml中集成Scalastyle。

<build>
  <plugins> 
    ...
	<plugin>
	  <groupId>org.scalastyle</groupId>
	  <artifactId>scalastyle-maven-plugin</artifactId>
	  <version>1.0.0</version>
	  <configuration>
		<verbose>false</verbose>
		<failOnViolation>true</failOnViolation>
		<includeTestSourceDirectory>false</includeTestSourceDirectory>
		<failOnWarning>false</failOnWarning>
		<sourceDirectory>${basedir}/src/main/scala</sourceDirectory>
		<testSourceDirectory>${basedir}/src/test/scala</testSourceDirectory>
		<configLocation>scalastyle-config.xml</configLocation>
		<outputFile>${basedir}/target/scalastyle-output.xml</outputFile>
		<inputEncoding>${project.build.sourceEncoding}</inputEncoding>
		<outputEncoding>${project.reporting.outputEncoding}</outputEncoding>
	  </configuration>
	  <executions>
		<execution>
			<goals>
				<goal>check</goal>
			</goals>
		</execution>
	  </executions>
	</plugin>
    ...
  </plugins>
</build>

也可以将<sourceDirectories>设置多个目录。

<sourceDirectories>
 <dir>${project.basedir}/src/main/scala</dir>
 <dir>${project.basedir}/src/main/generated-scala</dir>
</sourceDirectories>

IDEA下可以设置风格检测

 

Spark2.4.3 的 Scalastyle-config.xml

If you wish to turn off checking for a section of code, you can put a comment in the source
before and after the section, with the following syntax:

  // scalastyle:off
  ...  // stuff that breaks the styles
  // scalastyle:on

You can also disable only one rule, by specifying its rule id, as specified in:
  http://www.scalastyle.org/rules-0.7.0.html

  // scalastyle:off no.finalize
  override def finalize(): Unit = ...
  // scalastyle:on no.finalize

This file is divided into 3 sections:
 (1) rules that we enforce.
 (2) rules that we would like to enforce, but haven't cleaned up the codebase to turn on yet
     (or we need to make the scalastyle rule more configurable).
 (3) rules that we don't want to enforce.
-->

<scalastyle>
  <name>Scalastyle standard configuration</name>

  <!-- ================================================================================ -->
  <!--                               rules we enforce                                   -->
  <!-- ================================================================================ -->

  <check level="error" class="org.scalastyle.file.FileTabChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.file.HeaderMatchesChecker" enabled="true">
    <parameters>
       <parameter name="header"><![CDATA[/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */]]></parameter>
    </parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.SpacesAfterPlusChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.SpacesBeforePlusChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.file.WhitespaceEndOfLineChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.file.FileLineLengthChecker" enabled="true">
    <parameters>
      <parameter name="maxLineLength"><![CDATA[100]]></parameter>
      <parameter name="tabSize"><![CDATA[2]]></parameter>
      <parameter name="ignoreImports">true</parameter>
    </parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.ClassNamesChecker" enabled="true">
    <parameters><parameter name="regex"><![CDATA[[A-Z][A-Za-z]*]]></parameter></parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.ObjectNamesChecker" enabled="true">
    <parameters><parameter name="regex"><![CDATA[(config|[A-Z][A-Za-z]*)]]></parameter></parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.PackageObjectNamesChecker" enabled="true">
    <parameters><parameter name="regex"><![CDATA[^[a-z][A-Za-z]*$]]></parameter></parameters>
  </check>

  <check customId="argcount" level="error" class="org.scalastyle.scalariform.ParameterNumberChecker" enabled="true">
    <parameters><parameter name="maxParameters"><![CDATA[10]]></parameter></parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.NoFinalizeChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.CovariantEqualsChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.StructuralTypeChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.UppercaseLChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.IfBraceChecker" enabled="true">
    <parameters>
      <parameter name="singleLineAllowed"><![CDATA[true]]></parameter>
      <parameter name="doubleLineAllowed"><![CDATA[true]]></parameter>
    </parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.PublicMethodsHaveTypeChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.file.NewLineAtEofChecker" enabled="true"></check>

  <check customId="nonascii" level="error" class="org.scalastyle.scalariform.NonASCIICharacterChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.SpaceAfterCommentStartChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.EnsureSingleSpaceBeforeTokenChecker" enabled="true">
   <parameters>
     <parameter name="tokens">ARROW, EQUALS, ELSE, TRY, CATCH, FINALLY, LARROW, RARROW</parameter>
   </parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.EnsureSingleSpaceAfterTokenChecker" enabled="true">
    <parameters>
     <parameter name="tokens">ARROW, EQUALS, COMMA, COLON, IF, ELSE, DO, WHILE, FOR, MATCH, TRY, CATCH, FINALLY, LARROW, RARROW</parameter>
    </parameters>
  </check>

  <!-- ??? usually shouldn't be checked into the code base. -->
  <check level="error" class="org.scalastyle.scalariform.NotImplementedErrorUsage" enabled="true"></check>

  <!-- As of SPARK-7558, all tests in Spark should extend o.a.s.SparkFunSuite instead of FunSuite directly -->
  <check customId="funsuite" level="error" class="org.scalastyle.scalariform.TokenChecker" enabled="true">
    <parameters><parameter name="regex">^FunSuite[A-Za-z]*$</parameter></parameters>
    <customMessage>Tests must extend org.apache.spark.SparkFunSuite instead.</customMessage>
  </check>

  <!-- As of SPARK-7977 all printlns need to be wrapped in '// scalastyle:off/on println' -->
  <check customId="println" level="error" class="org.scalastyle.scalariform.TokenChecker" enabled="true">
    <parameters><parameter name="regex">^println$</parameter></parameters>
    <customMessage><![CDATA[Are you sure you want to println? If yes, wrap the code block with
      // scalastyle:off println
      println(...)
      // scalastyle:on println]]></customMessage>
  </check>

  <check customId="hadoopconfiguration" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">spark(.sqlContext)?.sparkContext.hadoopConfiguration</parameter></parameters>
    <customMessage><![CDATA[
      Are you sure that you want to use sparkContext.hadoopConfiguration? In most cases, you should use
      spark.sessionState.newHadoopConf() instead, so that the hadoop configurations specified in Spark session
      configuration will come into effect.
      If you must use sparkContext.hadoopConfiguration, wrap the code block with
      // scalastyle:off hadoopconfiguration
      spark.sparkContext.hadoopConfiguration...
      // scalastyle:on hadoopconfiguration
    ]]></customMessage>
  </check>

  <check customId="visiblefortesting" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">@VisibleForTesting</parameter></parameters>
    <customMessage><![CDATA[
      @VisibleForTesting causes classpath issues. Please note this in the java doc instead (SPARK-11615).
    ]]></customMessage>
  </check>

  <check customId="runtimeaddshutdownhook" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">Runtime\.getRuntime\.addShutdownHook</parameter></parameters>
    <customMessage><![CDATA[
      Are you sure that you want to use Runtime.getRuntime.addShutdownHook? In most cases, you should use
      ShutdownHookManager.addShutdownHook instead.
      If you must use Runtime.getRuntime.addShutdownHook, wrap the code block with
      // scalastyle:off runtimeaddshutdownhook
      Runtime.getRuntime.addShutdownHook(...)
      // scalastyle:on runtimeaddshutdownhook
    ]]></customMessage>
  </check>

  <check customId="mutablesynchronizedbuffer" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">mutable\.SynchronizedBuffer</parameter></parameters>
    <customMessage><![CDATA[
      Are you sure that you want to use mutable.SynchronizedBuffer? In most cases, you should use
      java.util.concurrent.ConcurrentLinkedQueue instead.
      If you must use mutable.SynchronizedBuffer, wrap the code block with
      // scalastyle:off mutablesynchronizedbuffer
      mutable.SynchronizedBuffer[...]
      // scalastyle:on mutablesynchronizedbuffer
    ]]></customMessage>
  </check>

  <check customId="classforname" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">Class\.forName</parameter></parameters>
    <customMessage><![CDATA[
      Are you sure that you want to use Class.forName? In most cases, you should use Utils.classForName instead.
      If you must use Class.forName, wrap the code block with
      // scalastyle:off classforname
      Class.forName(...)
      // scalastyle:on classforname
    ]]></customMessage>
  </check>

  <check customId="awaitresult" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">Await\.result</parameter></parameters>
    <customMessage><![CDATA[
      Are you sure that you want to use Await.result? In most cases, you should use ThreadUtils.awaitResult instead.
      If you must use Await.result, wrap the code block with
      // scalastyle:off awaitresult
      Await.result(...)
      // scalastyle:on awaitresult
    ]]></customMessage>
  </check>

  <check customId="awaitready" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">Await\.ready</parameter></parameters>
    <customMessage><![CDATA[
      Are you sure that you want to use Await.ready? In most cases, you should use ThreadUtils.awaitReady instead.
      If you must use Await.ready, wrap the code block with
      // scalastyle:off awaitready
      Await.ready(...)
      // scalastyle:on awaitready
    ]]></customMessage>
  </check>

  <!-- As of SPARK-9613 JavaConversions should be replaced with JavaConverters -->
  <check customId="javaconversions" level="error" class="org.scalastyle.scalariform.TokenChecker" enabled="true">
    <parameters><parameter name="regex">JavaConversions</parameter></parameters>
    <customMessage>Instead of importing implicits in scala.collection.JavaConversions._, import
    scala.collection.JavaConverters._ and use .asScala / .asJava methods</customMessage>
  </check>

  <check customId="commonslang2" level="error" class="org.scalastyle.scalariform.TokenChecker" enabled="true">
    <parameters><parameter name="regex">org\.apache\.commons\.lang\.</parameter></parameters>
    <customMessage>Use Commons Lang 3 classes (package org.apache.commons.lang3.*) instead
    of Commons Lang 2 (package org.apache.commons.lang.*)</customMessage>
  </check>

  <check customId="extractopt" level="error" class="org.scalastyle.scalariform.TokenChecker" enabled="true">
    <parameters><parameter name="regex">extractOpt</parameter></parameters>
    <customMessage>Use jsonOption(x).map(.extract[T]) instead of .extractOpt[T], as the latter
    is slower.  </customMessage>
  </check>

  <check level="error" class="org.scalastyle.scalariform.ImportOrderChecker" enabled="true">
    <parameters>
      <parameter name="groups">java,scala,3rdParty,spark</parameter>
      <parameter name="group.java">javax?\..*</parameter>
      <parameter name="group.scala">scala\..*</parameter>
      <parameter name="group.3rdParty">(?!org\.apache\.spark\.).*</parameter>
      <parameter name="group.spark">org\.apache\.spark\..*</parameter>
    </parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.DisallowSpaceBeforeTokenChecker" enabled="true">
    <parameters>
      <parameter name="tokens">COMMA</parameter>
    </parameters>
  </check>

  <!-- SPARK-3854: Single Space between ')' and '{' -->
  <check customId="SingleSpaceBetweenRParenAndLCurlyBrace" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">\)\{</parameter></parameters>
    <customMessage><![CDATA[
      Single Space between ')' and `{`.
    ]]></customMessage>
  </check>

  <check customId="NoScalaDoc" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">(?m)^(\s*)/[*][*].*$(\r|)\n^\1  [*]</parameter></parameters>
    <customMessage>Use Javadoc style indentation for multiline comments</customMessage>
  </check>

  <check customId="OmitBracesInCase" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">case[^\n>]*=>\s*\{</parameter></parameters>
    <customMessage>Omit braces in case clauses.</customMessage>
  </check>

  <!-- SPARK-16877: Avoid Java annotations -->
  <check level="error" class="org.scalastyle.scalariform.OverrideJavaChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.DeprecatedJavaChecker" enabled="true"></check>

  <!-- ================================================================================ -->
  <!--       rules we'd like to enforce, but haven't cleaned up the codebase yet        -->
  <!-- ================================================================================ -->

  <!-- We cannot turn the following two on, because it'd fail a lot of string interpolation use cases. -->
  <!-- Ideally the following two rules should be configurable to rule out string interpolation. -->
  <check level="error" class="org.scalastyle.scalariform.NoWhitespaceBeforeLeftBracketChecker" enabled="false"></check>
  <check level="error" class="org.scalastyle.scalariform.NoWhitespaceAfterLeftBracketChecker" enabled="false"></check>

  <!-- This breaks symbolic method names so we don't turn it on. -->
  <!-- Maybe we should update it to allow basic symbolic names, and then we are good to go. -->
  <check level="error" class="org.scalastyle.scalariform.MethodNamesChecker" enabled="false">
    <parameters>
    <parameter name="regex"><![CDATA[^[a-z][A-Za-z0-9]*$]]></parameter>
    </parameters>
  </check>

  <!-- Should turn this on, but we have a few places that need to be fixed first -->
  <check level="error" class="org.scalastyle.scalariform.EqualsHashCodeChecker" enabled="true"></check>

  <!-- ================================================================================ -->
  <!--                               rules we don't want                                -->
  <!-- ================================================================================ -->

  <check level="error" class="org.scalastyle.scalariform.IllegalImportsChecker" enabled="false">
    <parameters><parameter name="illegalImports"><![CDATA[sun._,java.awt._]]></parameter></parameters>
  </check>

  <!-- We want the opposite of this: NewLineAtEofChecker -->
  <check level="error" class="org.scalastyle.file.NoNewLineAtEofChecker" enabled="false"></check>

  <!-- This one complains about all kinds of random things. Disable. -->
  <check level="error" class="org.scalastyle.scalariform.SimplifyBooleanExpressionChecker" enabled="false"></check>

  <!-- We use return quite a bit for control flows and guards -->
  <check level="error" class="org.scalastyle.scalariform.ReturnChecker" enabled="false"></check>

  <!-- We use null a lot in low level code and to interface with 3rd party code -->
  <check level="error" class="org.scalastyle.scalariform.NullChecker" enabled="false"></check>

  <!-- Doesn't seem super big deal here ... -->
  <check level="error" class="org.scalastyle.scalariform.NoCloneChecker" enabled="false"></check>

  <!-- Doesn't seem super big deal here ... -->
  <check level="error" class="org.scalastyle.file.FileLengthChecker" enabled="false">
    <parameters><parameter name="maxFileLength">800></parameter></parameters>
  </check>

  <!-- Doesn't seem super big deal here ... -->
  <check level="error" class="org.scalastyle.scalariform.NumberOfTypesChecker" enabled="false">
    <parameters><parameter name="maxTypes">30</parameter></parameters>
  </check>

  <!-- Doesn't seem super big deal here ... -->
  <check level="error" class="org.scalastyle.scalariform.CyclomaticComplexityChecker" enabled="false">
    <parameters><parameter name="maximum">10</parameter></parameters>
  </check>

  <!-- Doesn't seem super big deal here ... -->
  <check level="error" class="org.scalastyle.scalariform.MethodLengthChecker" enabled="false">
    <parameters><parameter name="maxLength">50</parameter></parameters>
  </check>

  <!-- Not exactly feasible to enforce this right now. -->
  <!-- It is also infrequent that somebody introduces a new class with a lot of methods. -->
  <check level="error" class="org.scalastyle.scalariform.NumberOfMethodsInTypeChecker" enabled="false">
    <parameters><parameter name="maxMethods"><![CDATA[30]]></parameter></parameters>
  </check>

  <!-- Doesn't seem super big deal here, and we have a lot of magic numbers ... -->
  <check level="error" class="org.scalastyle.scalariform.MagicNumberChecker" enabled="false">
    <parameters><parameter name="ignore">-1,0,1,2,3</parameter></parameters>
  </check>

</scalastyle>

记录下Scalastyle规则,方便查询。内容比较多,使用Ctrl + F 输入需要查询的检查器进行搜索查询。

目录

介绍

规则

使用示例

Maven集成Scalastyle

Spark的Scalastyle-config.xml


介绍

Scalastyle 是专门针对Scala代码开发的轻巧的code style检测工具,类似于Java代码检查工具Checkstyle、Findbugs等。可以在Maven中集成到项目中,方便代码风格的检测。

最下面附上了Spark2.4.3的 Scalastyle 配置代码,可以借鉴。

Scalastyle常用关键字 //scalastyle:on//scalastyle:off,如:

//scalastyle:on println
	if (args.length < 2) {
	  println("Usage:")
	  println("DRIVER_MEMORY=[mem] spark-submit " +
		"com.github.ehiggs.spark.terasort.TeraGen " +
		"spark-terasort-1.0-SNAPSHOT-with-dependencies.jar " +
		"[output-size] [output-directory]")
	  println(" ")
	  println("Example:")
	  println("DRIVER_MEMORY=50g spark-submit " +
		"com.github.ehiggs.spark.terasort.TeraGen " +
		"spark-terasort-1.0-SNAPSHOT-with-dependencies.jar " +
		"100G file:///scratch/username/terasort_in")
	  System.exit(0)
	}
//scalastyle:off println

规则

官网说明:已实现的规则 

目前已实现规则有69条

检查器 解释
org.scalastyle.file.FileLengthChecker 检查文件中的行数
org.scalastyle.file.FileLineLengthChecker 检查一行中的字符数
org.scalastyle.file.FileTabChecker 检查文件中是否有标签
org.scalastyle.file.HeaderMatchesChecker 检查每个文件的前几行与文本匹配
org.scalastyle.file.IndentationChecker 检查行是否由Tab的倍数缩进
org.scalastyle.file.NewLineAtEofChecker 检查文件是否以换行符结尾
org.scalastyle.file.NoNewLineAtEofChecker 检查文件不是以换行符结尾
org.scalastyle.file.RegexChecker 检查正则表达式是否匹配
org.scalastyle.file.WhitespaceEndOfLineChecker 检查行尾没有空格
org.scalastyle.scalariform.BlockImportChecker 检查导入块是否使用
org.scalastyle.scalariform.ClassNamesChecker 检查类名是否与正则表达式匹配
org.scalastyle.scalariform.ClassTypeParameterChecker 检查类的类型参数与正则表达式匹配
org.scalastyle.scalariform.CovariantEqualsChecker 检查类和对象在没有覆盖的情况下定义了相等(java.lang.object)
org.scalastyle.scalariform.CyclomaticComplexityChecker 检查方法的参数复杂度是否超过设定值
org.scalastyle.scalariform.DeprecatedJavaChecker 检查应该用Scala @deprecated代替Java @Deprecated
org.scalastyle.scalariform.DisallowSpaceAfterTokenChecker 检查某些既定标记后不允许使用空格
org.scalastyle.scalariform.DisallowSpaceBeforeTokenChecker 检查某些既定标记前不允许使用空格
org.scalastyle.scalariform.EmptyClassChecker 检查类或抽象类没有成员,则可以省略大括号
org.scalastyle.scalariform.EmptyInterpolatedStringChecker 检查是否有对字符串插入空值
org.scalastyle.scalariform.EnsureSingleSpaceAfterTokenChecker 检查某些既定标记后是否有空间
org.scalastyle.scalariform.EnsureSingleSpaceBeforeTokenChecker 检查某些既定标记前是否有空间
org.scalastyle.scalariform.EqualsHashCodeChecker 检查如果一个类实现了或等于Hash,那它应该实现另一个类
org.scalastyle.scalariform.FieldNamesChecker 检查字段名是否与正则表达式匹配
org.scalastyle.scalariform.ForBraceChecker 检查for是否使用大括号
org.scalastyle.scalariform.IfBraceChecker 检查if是否使用大括号
org.scalastyle.scalariform.IllegalImportsChecker 检查类没有导入某些类
org.scalastyle.scalariform.ImportGroupingChecker 检查导入是否分组
org.scalastyle.scalariform.ImportOrderChecker 检查是否根据样式配置对导入进行分组和排序
org.scalastyle.scalariform.LowercasePatternMatchChecker 检查case语句模式匹配是否为小写,因为这可能导致混淆
org.scalastyle.scalariform.MagicNumberChecker 检查魔数是否使用var定义
org.scalastyle.scalariform.MethodArgumentNamesChecker 检查方法参数名是否与正则表达式匹配
org.scalastyle.scalariform.MethodLengthChecker 检查方法不超过最大长度
org.scalastyle.scalariform.MethodNamesChecker 检查方法名是否与正则表达式匹配
org.scalastyle.scalariform.MultipleStringLiteralsChecker 检查字符串文字没有多次出现
org.scalastyle.scalariform.NamedArgumentChecker 检查参数是否命名
org.scalastyle.scalariform.NoCloneChecker 检查类和对象没有定义clone()方法
org.scalastyle.scalariform.NoFinalizeChecker 检查类和对象是否定义了Finish()方法
org.scalastyle.scalariform.NoWhitespaceAfterLeftBracketChecker 检查左括号‘[’后没有空格
org.scalastyle.scalariform.NoWhitespaceBeforeLeftBracketChecker 检查左括号‘[’之前没有空格
org.scalastyle.scalariform.NoWhitespaceBeforeRightBracketChecker 检查右括号“]”之前没有空格
org.scalastyle.scalariform.NonASCIICharacterChecker 检查是否使用非ASCII字符(Unicode字符)
org.scalastyle.scalariform.NotImplementedErrorUsage 检查代码是否有 ??? 操作符
org.scalastyle.scalariform.NullChecker 检查是否使用NULL
org.scalastyle.scalariform.NumberOfMethodsInTypeChecker 检查一个类/特性/对象是否申明太多的方法
org.scalastyle.scalariform.NumberOfTypesChecker 检查文件中声明的类型是否过多
org.scalastyle.scalariform.ObjectNamesChecker 检查对象名称是否与正则表达式匹配
org.scalastyle.scalariform.OverrideJavaChecker 检查java@重写是否被使用
org.scalastyle.scalariform.PackageNamesChecker 检查包名是否与正则表达式匹配
org.scalastyle.scalariform.PackageObjectNamesChecker 检查包对象名是否与正则表达式匹配
org.scalastyle.scalariform.ParameterNumberChecker 检查方法的参数个数是否超过设置数量
org.scalastyle.scalariform.PatternMatchAlignChecker 检查模式匹配箭头对齐
org.scalastyle.scalariform.ProcedureDeclarationChecker 检查申明过程 a:unit= 是否使用=号
org.scalastyle.scalariform.PublicMethodsHaveTypeChecker 检查方法是否具有显式返回类型
org.scalastyle.scalariform.RedundantIfChecker 检查表达式是否是冗余,如,if (cond) false else true
org.scalastyle.scalariform.ReturnChecker 检查是否使用return
org.scalastyle.scalariform.ScalaDocChecker 检查代码格式是否遵循Scaladoc文档说明
org.scalastyle.scalariform.SimplifyBooleanExpressionChecker 检查Boolean表达式是否可以简化
org.scalastyle.scalariform.SpaceAfterCommentStartChecker 检查在注释后是否有一个空格
org.scalastyle.scalariform.SpacesAfterPlusChecker 检查加号后面是否有空格
org.scalastyle.scalariform.SpacesBeforePlusChecker 检查加号前面是否有空格
org.scalastyle.scalariform.StructuralTypeChecker 检查结构类型是否未被使用
org.scalastyle.scalariform.TodoCommentChecker 检查是否使用Todo/Fixme单行注释
org.scalastyle.scalariform.TokenChecker 检查如果有正则表达式,是否在特殊标记中匹配
org.scalastyle.scalariform.UnderscoreImportChecker 检查是否使用通配符导入
org.scalastyle.scalariform.UppercaseLChecker 检查如果使用长字符串,则使用大写字母
org.scalastyle.scalariform.VarFieldChecker 检查类和对象是否定义可变字段
org.scalastyle.scalariform.VarLocalChecker 检查函数是否定义可变变量
org.scalastyle.scalariform.WhileChecker 检查是否使用While循环
org.scalastyle.scalariform.XmlLiteralChecker 检查是否未使用xml文本

使用示例

主要在 scalastyle-config.xml 中设置。

 org.scalastyle.file.FileLengthChecker    检查文件中的行数

<check enabled="true" class="org.scalastyle.file.FileLengthChecker" level="warning">
 <parameters>
  <parameter name="maxFileLength">800</parameter>
 </parameters>
</check>

org.scalastyle.file.FileLineLengthChecker    检查一行中的字符数

<check enabled="true" class="org.scalastyle.file.FileLineLengthChecker" level="warning">
 <parameters>
  <parameter name="maxLineLength">100</parameter>
  <parameter name="tabSize">2</parameter>
  <parameter name="ignoreImports">true</parameter>
 </parameters>
</check>

org.scalastyle.file.FileTabChecker    检查文件中是否有标签

<check enabled="true" class="org.scalastyle.file.FileTabChecker" level="warning"/>

org.scalastyle.file.HeaderMatchesChecker    检查每个文件的前几行与文本匹配

<check enabled="true" class="org.scalastyle.file.HeaderMatchesChecker" level="warning">
 <parameters>
  <parameter name="regex">false</parameter>
  <parameter name="header">// Copyright \(C\) 2011-2012 the original author or authors.</parameter>
 </parameters>
</check>

或者 

<check enabled="true" class="org.scalastyle.file.HeaderMatchesChecker" level="warning">
 <parameters>
  <parameter name="regex">true</parameter>
  <parameter name="header">// Copyright \(C\) (?:\d{4}-)?\d{4} the original author or authors.</parameter>
 </parameters>
</check>

org.scalastyle.file.IndentationChecker    检查行是否由Tab的倍数缩进

<check enabled="true" class="org.scalastyle.file.IndentationChecker" level="warning">
 <parameters>
  <parameter name="tabSize">2</parameter>
  <parameter name="methodParamIndentSize">2</parameter>
  <parameter name="classParamIndentSize">4</parameter>
 </parameters>
</check>

org.scalastyle.file.NewLineAtEofChecker    检查文件是否以换行符结尾

<check enabled="true" class="org.scalastyle.file.NewLineAtEofChecker" level="warning"/>

org.scalastyle.file.NoNewLineAtEofChecker    检查文件不是以换行符结尾

<check enabled="true" class="org.scalastyle.file.NoNewLineAtEofChecker" level="warning"/>

org.scalastyle.file.RegexChecker    检查正则表达式是否匹配

<check enabled="true" class="org.scalastyle.file.RegexChecker" level="warning">
 <parameters>
  <parameter name="regex">(?m)^\s*$(\r|)\n^\s*$(\r|)\n</parameter>
  <parameter name="line">false</parameter>
 </parameters>
 <customMessage>No double blank lines</customMessage>
</check>

org.scalastyle.file.WhitespaceEndOfLineChecker    检查行尾没有空格

<check enabled="true" class="org.scalastyle.file.WhitespaceEndOfLineChecker" level="warning">
 <parameters>
  <parameter default="false" type="boolean" name="ignoreWhitespaceLines"/>
 </parameters>
</check>

org.scalastyle.scalariform.BlockImportChecker    检查导入块是否使用

<check enabled="true" class="org.scalastyle.scalariform.BlockImportChecker" level="warning"/>

org.scalastyle.scalariform.ClassNamesChecker    检查类名是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.ClassNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[A-Z][A-Za-z]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.ClassTypeParameterChecker    检查类的类型参数与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.ClassTypeParameterChecker" level="warning">
 <parameters>
  <parameter name="regex">^[A-Z_]$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.CovariantEqualsChecker    检查类和对象在没有覆盖的情况下定义了相等(java.lang.object)

<check enabled="true" class="org.scalastyle.scalariform.CovariantEqualsChecker" level="warning"/>

org.scalastyle.scalariform.CyclomaticComplexityChecker    检查方法的参数复杂度是否超过设定值

<check enabled="true" class="org.scalastyle.scalariform.CyclomaticComplexityChecker" level="warning">
 <parameters>
  <parameter name="maximum">10</parameter>
  <parameter name="countCases">true</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.DeprecatedJavaChecker    检查应该用Scala @deprecated代替Java @Deprecated

<check enabled="true" class="org.scalastyle.scalariform.DeprecatedJavaChecker" level="warning"/>

org.scalastyle.scalariform.DisallowSpaceAfterTokenChecker    检查某些既定标记后不允许使用空格

<check enabled="true" class="org.scalastyle.scalariform.DisallowSpaceAfterTokenChecker" level="warning">
 <parameters>
  <parameter name="tokens">LPAREN</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.DisallowSpaceBeforeTokenChecker    检查某些既定标记前不允许使用空格

<check enabled="true" class="org.scalastyle.scalariform.DisallowSpaceBeforeTokenChecker" level="warning">
 <parameters>
  <parameter name="tokens">COLON, COMMA, RPAREN</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.EmptyClassChecker    检查类或抽象类没有成员,则可以省略大括号

<check enabled="true" class="org.scalastyle.scalariform.EmptyClassChecker" level="warning"/>

org.scalastyle.scalariform.EmptyInterpolatedStringChecker    检查是否有对字符串插入空值

<check enabled="true" level="warning" class="org.scalastyle.scalariform.EmptyInterpolatedStringChecker"/>

org.scalastyle.scalariform.EnsureSingleSpaceAfterTokenChecker    检查某些既定标记后是否有空间

<check enabled="true" class="org.scalastyle.scalariform.EnsureSingleSpaceAfterTokenChecker" level="warning">
 <parameters>
  <parameter name="tokens">COLON, IF</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.EnsureSingleSpaceBeforeTokenChecker    检查某些既定标记前是否有空间

<check enabled="true" class="org.scalastyle.scalariform.EnsureSingleSpaceBeforeTokenChecker" level="warning">
 <parameters>
  <parameter name="tokens">LPAREN</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.EqualsHashCodeChecker    检查如果一个类实现了或等于Hash,那它应该实现另一个类

<check enabled="true" class="org.scalastyle.scalariform.EqualsHashCodeChecker" level="warning"/>

org.scalastyle.scalariform.FieldNamesChecker    检查字段名是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.FieldNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[a-z][A-Za-z0-9]*$</parameter>
  <parameter name="objectFieldRegex">^[A-Z][A-Za-z0-9]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.ForBraceChecker    检查for是否使用大括号

<check enabled="true" class="org.scalastyle.scalariform.ForBraceChecker" level="warning"/>

org.scalastyle.scalariform.IfBraceChecker    检查if是否使用大括号

<check enabled="true" class="org.scalastyle.scalariform.IfBraceChecker" level="warning">
 <parameters>
  <parameter name="singleLineAllowed">true</parameter>
  <parameter name="doubleLineAllowed">false</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.IllegalImportsChecker    检查类没有导入某些类

<check enabled="true" class="org.scalastyle.scalariform.IllegalImportsChecker" level="warning"/>

org.scalastyle.scalariform.ImportGroupingChecker    检查导入是否分组

<check enabled="true" class="org.scalastyle.scalariform.ImportGroupingChecker" level="warning"/>

org.scalastyle.scalariform.ImportOrderChecker    检查是否根据样式配置对导入进行分组和排序

<check enabled="true" class="org.scalastyle.scalariform.ImportOrderChecker" level="warning">
 <parameters>
  <parameter name="groups">java,scala,others</parameter>
  <parameter name="group.java">javax?\..+</parameter>
  <parameter name="group.scala">scala\..+</parameter>
  <parameter name="group.others">.+</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.LowercasePatternMatchChecker    检查case语句模式匹配是否为小写,因为这可能导致混淆

<check enabled="true" class="org.scalastyle.scalariform.LowercasePatternMatchChecker" level="warning"/>

org.scalastyle.scalariform.MagicNumberChecker    检查魔数是否使用var定义

<check enabled="true" class="org.scalastyle.scalariform.MagicNumberChecker" level="warning">
 <parameters>
  <parameter name="ignore">-1,0,1,2,3</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.MethodArgumentNamesChecker    检查方法参数名是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.MethodArgumentNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[a-z][A-Za-z0-9]*$</parameter>
  <parameter name="ignoreRegex">^$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.MethodLengthChecker    检查方法不超过最大长度

<check enabled="true" class="org.scalastyle.scalariform.MethodLengthChecker" level="warning">
 <parameters>
  <parameter name="maxLength">50</parameter>
  <parameter name="ignoreComments">false</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.MethodNamesChecker    检查方法名是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.MethodNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[A-Za-z]*$</parameter>
  <parameter name="ignoreRegex">^.*$</parameter>
  <parameter name="ignoreOverride">false</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.MultipleStringLiteralsChecker    检查字符串文字没有多次出现

<check enabled="true" class="org.scalastyle.scalariform.MultipleStringLiteralsChecker" level="warning">
 <parameters>
  <parameter name="allowed">1</parameter>
  <parameter name="ignoreRegex">^\&quot;\&quot;$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.NamedArgumentChecker    检查参数是否命名

<check enabled="true" class="org.scalastyle.scalariform.NamedArgumentChecker" level="warning">
 <parameters>
  <parameter name="checkString">false</parameter>
  <parameter name="ignoreMethod">^set.+$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.NoCloneChecker    检查类和对象没有定义clone()方法

<check enabled="true" class="org.scalastyle.scalariform.NoCloneChecker" level="warning"/>

org.scalastyle.scalariform.NoFinalizeChecker    检查类和对象是否定义了Finish()方法

<check enabled="true" class="org.scalastyle.scalariform.NoFinalizeChecker" level="warning"/>

org.scalastyle.scalariform.NoWhitespaceAfterLeftBracketChecker    检查左括号‘[’后没有空格

<check enabled="true" class="org.scalastyle.scalariform.NoWhitespaceAfterLeftBracketChecker" level="warning"/>

org.scalastyle.scalariform.NoWhitespaceBeforeLeftBracketChecker    检查左括号‘[’之前没有空格

<check enabled="true" class="org.scalastyle.scalariform.NoWhitespaceBeforeLeftBracketChecker" level="warning"/>

org.scalastyle.scalariform.NoWhitespaceBeforeRightBracketChecker    检查右括号“]”之前没有空格

<check enabled="true" class="org.scalastyle.scalariform.NoWhitespaceBeforeRightBracketChecker" level="warning"/>

org.scalastyle.scalariform.NonASCIICharacterChecker    检查是否使用非ASCII字符(Unicode字符)

<check enabled="true" class="org.scalastyle.scalariform.NonASCIICharacterChecker" level="warning"/>

org.scalastyle.scalariform.NotImplementedErrorUsage    检查代码是否有 ??? 操作符

<check enabled="true" class="org.scalastyle.scalariform.NotImplementedErrorUsage" level="warning"/>

org.scalastyle.scalariform.NullChecker    检查是否使用NULL

<check enabled="true" class="org.scalastyle.scalariform.NullChecker" level="warning">
 <parameters>
  <parameter name="allowNullChecks">true</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.NumberOfMethodsInTypeChecker    检查一个类/特性/对象是否申明太多的方法

<check enabled="true" class="org.scalastyle.scalariform.NumberOfMethodsInTypeChecker" level="warning">
 <parameters>
  <parameter name="maxMethods">30</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.NumberOfTypesChecker    检查文件中声明的类型是否过多

<check enabled="true" class="org.scalastyle.scalariform.NumberOfTypesChecker" level="warning">
 <parameters>
  <parameter name="maxTypes">20</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.ObjectNamesChecker    检查对象名称是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.ObjectNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[A-Z][A-Za-z]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.OverrideJavaChecker    检查java@重写是否被使用

<check enabled="true" class="org.scalastyle.scalariform.OverrideJavaChecker" level="warning"/>

org.scalastyle.scalariform.PackageNamesChecker    检查包名是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.PackageNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[a-z][A-Za-z]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.PackageObjectNamesChecker    检查包对象名是否与正则表达式匹配

<check enabled="true" class="org.scalastyle.scalariform.PackageObjectNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[a-z][A-Za-z]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.ParameterNumberChecker    检查方法的参数个数是否超过设置数量

<check enabled="true" class="org.scalastyle.scalariform.ParameterNumberChecker" level="warning">
 <parameters>
  <parameter name="maxParameters">8</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.PatternMatchAlignChecker    检查模式匹配箭头对齐

<check enabled="true" class="org.scalastyle.scalariform.PatternMatchAlignChecker" level="warning"/>

org.scalastyle.scalariform.ProcedureDeclarationChecker    检查申明过程 a:unit= 是否使用=号

<check enabled="true" class="org.scalastyle.scalariform.ProcedureDeclarationChecker" level="warning"/>

org.scalastyle.scalariform.PublicMethodsHaveTypeChecker    检查方法是否具有显式返回类型

<check enabled="true" class="org.scalastyle.scalariform.PublicMethodsHaveTypeChecker" level="warning">
 <parameters>
  <parameter name="ignoreOverride">false</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.RedundantIfChecker    检查表达式是否是冗余,如,if (cond) false else true

<check enabled="true" class="org.scalastyle.scalariform.RedundantIfChecker" level="warning"/>

org.scalastyle.scalariform.ReturnChecker    检查是否使用return

<check enabled="true" class="org.scalastyle.scalariform.ReturnChecker" level="warning"/>

org.scalastyle.scalariform.ScalaDocChecker    检查代码格式是否遵循Scaladoc文档说明

<check enabled="true" class="org.scalastyle.scalariform.ScalaDocChecker" level="warning">
 <parameters>
  <parameter name="ignoreRegex">(.*Spec$)|(.*SpecIT$)</parameter>
  <parameter name="ignoreTokenTypes">PatDefOrDcl,TypeDefOrDcl,FunDefOrDcl,TmplDef</parameter>
  <parameter name="ignoreOverride">false</parameter>
  <parameter name="indentStyle">anydoc</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.SimplifyBooleanExpressionChecker    检查Boolean表达式是否可以简化

<check enabled="true" class="org.scalastyle.scalariform.SimplifyBooleanExpressionChecker" level="warning"/>

org.scalastyle.scalariform.SpaceAfterCommentStartChecker    检查在注释后是否有一个空格

<check enabled="true" class="org.scalastyle.scalariform.SpaceAfterCommentStartChecker" level="warning"/>

org.scalastyle.scalariform.SpacesAfterPlusChecker    检查加号后面是否有空格

<check enabled="true" class="org.scalastyle.scalariform.SpacesAfterPlusChecker" level="warning"/>

org.scalastyle.scalariform.SpacesBeforePlusChecker    检查加号前面是否有空格

<check enabled="true" class="org.scalastyle.scalariform.SpacesBeforePlusChecker" level="warning"/>

org.scalastyle.scalariform.StructuralTypeChecker    检查结构类型是否未被使用

<check enabled="true" class="org.scalastyle.scalariform.StructuralTypeChecker" level="warning"/>

org.scalastyle.scalariform.TodoCommentChecker    检查是否使用Todo/Fixme单行注释

<check enabled="true" class="org.scalastyle.scalariform.TodoCommentChecker" level="warning">
 <parameters>
  <parameter default="TODO|FIXME" type="string" name="words"/>
 </parameters>
</check>

org.scalastyle.scalariform.TokenChecker    检查如果有正则表达式,是否在特殊标记中匹配

<check enabled="true" class="org.scalastyle.scalariform.TokenChecker" level="warning">
 <parameters>
  <parameter name="regex">^[ai]sInstanceOf$</parameter>
 </parameters>
 <customMessage>Avoid casting.</customMessage>
</check>

org.scalastyle.scalariform.UnderscoreImportChecker    检查是否使用通配符导入

<check enabled="true" class="org.scalastyle.scalariform.UnderscoreImportChecker" level="warning">
 <parameters>
  <parameter name="ignoreRegex">collection\.JavaConverters\._|scala\.concurrent\.duration\._</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.UppercaseLChecker    检查如果使用长字符串,则使用大写字母

<check enabled="true" class="org.scalastyle.scalariform.UppercaseLChecker" level="warning"/>

org.scalastyle.scalariform.VarFieldChecker    检查类和对象是否定义可变字段

<check enabled="true" class="org.scalastyle.scalariform.VarFieldChecker" level="warning"/>

org.scalastyle.scalariform.VarLocalChecker    检查函数是否定义可变变量

<check enabled="true" class="org.scalastyle.scalariform.VarLocalChecker" level="warning"/>

org.scalastyle.scalariform.WhileChecker    检查是否使用While循环

<check enabled="true" class="org.scalastyle.scalariform.WhileChecker" level="warning"/>

org.scalastyle.scalariform.XmlLiteralChecker    检查是否未使用xml文本

<check enabled="true" class="org.scalastyle.scalariform.XmlLiteralChecker" level="warning"/>

 

Maven集成Scalastyle

如Spark的pom.xml中集成Scalastyle。

<build>
  <plugins> 
    ...
	<plugin>
	  <groupId>org.scalastyle</groupId>
	  <artifactId>scalastyle-maven-plugin</artifactId>
	  <version>1.0.0</version>
	  <configuration>
		<verbose>false</verbose>
		<failOnViolation>true</failOnViolation>
		<includeTestSourceDirectory>false</includeTestSourceDirectory>
		<failOnWarning>false</failOnWarning>
		<sourceDirectory>${basedir}/src/main/scala</sourceDirectory>
		<testSourceDirectory>${basedir}/src/test/scala</testSourceDirectory>
		<configLocation>scalastyle-config.xml</configLocation>
		<outputFile>${basedir}/target/scalastyle-output.xml</outputFile>
		<inputEncoding>${project.build.sourceEncoding}</inputEncoding>
		<outputEncoding>${project.reporting.outputEncoding}</outputEncoding>
	  </configuration>
	  <executions>
		<execution>
			<goals>
				<goal>check</goal>
			</goals>
		</execution>
	  </executions>
	</plugin>
    ...
  </plugins>
</build>

也可以将<sourceDirectories>设置多个目录。

<sourceDirectories>
 <dir>${project.basedir}/src/main/scala</dir>
 <dir>${project.basedir}/src/main/generated-scala</dir>
</sourceDirectories>

IDEA下可以设置风格检测

 

Spark2.4.3 的 Scalastyle-config.xml

If you wish to turn off checking for a section of code, you can put a comment in the source
before and after the section, with the following syntax:

  // scalastyle:off
  ...  // stuff that breaks the styles
  // scalastyle:on

You can also disable only one rule, by specifying its rule id, as specified in:
  http://www.scalastyle.org/rules-0.7.0.html

  // scalastyle:off no.finalize
  override def finalize(): Unit = ...
  // scalastyle:on no.finalize

This file is divided into 3 sections:
 (1) rules that we enforce.
 (2) rules that we would like to enforce, but haven't cleaned up the codebase to turn on yet
     (or we need to make the scalastyle rule more configurable).
 (3) rules that we don't want to enforce.
-->

<scalastyle>
  <name>Scalastyle standard configuration</name>

  <!-- ================================================================================ -->
  <!--                               rules we enforce                                   -->
  <!-- ================================================================================ -->

  <check level="error" class="org.scalastyle.file.FileTabChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.file.HeaderMatchesChecker" enabled="true">
    <parameters>
       <parameter name="header"><![CDATA[/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */]]></parameter>
    </parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.SpacesAfterPlusChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.SpacesBeforePlusChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.file.WhitespaceEndOfLineChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.file.FileLineLengthChecker" enabled="true">
    <parameters>
      <parameter name="maxLineLength"><![CDATA[100]]></parameter>
      <parameter name="tabSize"><![CDATA[2]]></parameter>
      <parameter name="ignoreImports">true</parameter>
    </parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.ClassNamesChecker" enabled="true">
    <parameters><parameter name="regex"><![CDATA[[A-Z][A-Za-z]*]]></parameter></parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.ObjectNamesChecker" enabled="true">
    <parameters><parameter name="regex"><![CDATA[(config|[A-Z][A-Za-z]*)]]></parameter></parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.PackageObjectNamesChecker" enabled="true">
    <parameters><parameter name="regex"><![CDATA[^[a-z][A-Za-z]*$]]></parameter></parameters>
  </check>

  <check customId="argcount" level="error" class="org.scalastyle.scalariform.ParameterNumberChecker" enabled="true">
    <parameters><parameter name="maxParameters"><![CDATA[10]]></parameter></parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.NoFinalizeChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.CovariantEqualsChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.StructuralTypeChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.UppercaseLChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.IfBraceChecker" enabled="true">
    <parameters>
      <parameter name="singleLineAllowed"><![CDATA[true]]></parameter>
      <parameter name="doubleLineAllowed"><![CDATA[true]]></parameter>
    </parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.PublicMethodsHaveTypeChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.file.NewLineAtEofChecker" enabled="true"></check>

  <check customId="nonascii" level="error" class="org.scalastyle.scalariform.NonASCIICharacterChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.SpaceAfterCommentStartChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.EnsureSingleSpaceBeforeTokenChecker" enabled="true">
   <parameters>
     <parameter name="tokens">ARROW, EQUALS, ELSE, TRY, CATCH, FINALLY, LARROW, RARROW</parameter>
   </parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.EnsureSingleSpaceAfterTokenChecker" enabled="true">
    <parameters>
     <parameter name="tokens">ARROW, EQUALS, COMMA, COLON, IF, ELSE, DO, WHILE, FOR, MATCH, TRY, CATCH, FINALLY, LARROW, RARROW</parameter>
    </parameters>
  </check>

  <!-- ??? usually shouldn't be checked into the code base. -->
  <check level="error" class="org.scalastyle.scalariform.NotImplementedErrorUsage" enabled="true"></check>

  <!-- As of SPARK-7558, all tests in Spark should extend o.a.s.SparkFunSuite instead of FunSuite directly -->
  <check customId="funsuite" level="error" class="org.scalastyle.scalariform.TokenChecker" enabled="true">
    <parameters><parameter name="regex">^FunSuite[A-Za-z]*$</parameter></parameters>
    <customMessage>Tests must extend org.apache.spark.SparkFunSuite instead.</customMessage>
  </check>

  <!-- As of SPARK-7977 all printlns need to be wrapped in '// scalastyle:off/on println' -->
  <check customId="println" level="error" class="org.scalastyle.scalariform.TokenChecker" enabled="true">
    <parameters><parameter name="regex">^println$</parameter></parameters>
    <customMessage><![CDATA[Are you sure you want to println? If yes, wrap the code block with
      // scalastyle:off println
      println(...)
      // scalastyle:on println]]></customMessage>
  </check>

  <check customId="hadoopconfiguration" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">spark(.sqlContext)?.sparkContext.hadoopConfiguration</parameter></parameters>
    <customMessage><![CDATA[
      Are you sure that you want to use sparkContext.hadoopConfiguration? In most cases, you should use
      spark.sessionState.newHadoopConf() instead, so that the hadoop configurations specified in Spark session
      configuration will come into effect.
      If you must use sparkContext.hadoopConfiguration, wrap the code block with
      // scalastyle:off hadoopconfiguration
      spark.sparkContext.hadoopConfiguration...
      // scalastyle:on hadoopconfiguration
    ]]></customMessage>
  </check>

  <check customId="visiblefortesting" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">@VisibleForTesting</parameter></parameters>
    <customMessage><![CDATA[
      @VisibleForTesting causes classpath issues. Please note this in the java doc instead (SPARK-11615).
    ]]></customMessage>
  </check>

  <check customId="runtimeaddshutdownhook" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">Runtime\.getRuntime\.addShutdownHook</parameter></parameters>
    <customMessage><![CDATA[
      Are you sure that you want to use Runtime.getRuntime.addShutdownHook? In most cases, you should use
      ShutdownHookManager.addShutdownHook instead.
      If you must use Runtime.getRuntime.addShutdownHook, wrap the code block with
      // scalastyle:off runtimeaddshutdownhook
      Runtime.getRuntime.addShutdownHook(...)
      // scalastyle:on runtimeaddshutdownhook
    ]]></customMessage>
  </check>

  <check customId="mutablesynchronizedbuffer" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">mutable\.SynchronizedBuffer</parameter></parameters>
    <customMessage><![CDATA[
      Are you sure that you want to use mutable.SynchronizedBuffer? In most cases, you should use
      java.util.concurrent.ConcurrentLinkedQueue instead.
      If you must use mutable.SynchronizedBuffer, wrap the code block with
      // scalastyle:off mutablesynchronizedbuffer
      mutable.SynchronizedBuffer[...]
      // scalastyle:on mutablesynchronizedbuffer
    ]]></customMessage>
  </check>

  <check customId="classforname" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">Class\.forName</parameter></parameters>
    <customMessage><![CDATA[
      Are you sure that you want to use Class.forName? In most cases, you should use Utils.classForName instead.
      If you must use Class.forName, wrap the code block with
      // scalastyle:off classforname
      Class.forName(...)
      // scalastyle:on classforname
    ]]></customMessage>
  </check>

  <check customId="awaitresult" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">Await\.result</parameter></parameters>
    <customMessage><![CDATA[
      Are you sure that you want to use Await.result? In most cases, you should use ThreadUtils.awaitResult instead.
      If you must use Await.result, wrap the code block with
      // scalastyle:off awaitresult
      Await.result(...)
      // scalastyle:on awaitresult
    ]]></customMessage>
  </check>

  <check customId="awaitready" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">Await\.ready</parameter></parameters>
    <customMessage><![CDATA[
      Are you sure that you want to use Await.ready? In most cases, you should use ThreadUtils.awaitReady instead.
      If you must use Await.ready, wrap the code block with
      // scalastyle:off awaitready
      Await.ready(...)
      // scalastyle:on awaitready
    ]]></customMessage>
  </check>

  <!-- As of SPARK-9613 JavaConversions should be replaced with JavaConverters -->
  <check customId="javaconversions" level="error" class="org.scalastyle.scalariform.TokenChecker" enabled="true">
    <parameters><parameter name="regex">JavaConversions</parameter></parameters>
    <customMessage>Instead of importing implicits in scala.collection.JavaConversions._, import
    scala.collection.JavaConverters._ and use .asScala / .asJava methods</customMessage>
  </check>

  <check customId="commonslang2" level="error" class="org.scalastyle.scalariform.TokenChecker" enabled="true">
    <parameters><parameter name="regex">org\.apache\.commons\.lang\.</parameter></parameters>
    <customMessage>Use Commons Lang 3 classes (package org.apache.commons.lang3.*) instead
    of Commons Lang 2 (package org.apache.commons.lang.*)</customMessage>
  </check>

  <check customId="extractopt" level="error" class="org.scalastyle.scalariform.TokenChecker" enabled="true">
    <parameters><parameter name="regex">extractOpt</parameter></parameters>
    <customMessage>Use jsonOption(x).map(.extract[T]) instead of .extractOpt[T], as the latter
    is slower.  </customMessage>
  </check>

  <check level="error" class="org.scalastyle.scalariform.ImportOrderChecker" enabled="true">
    <parameters>
      <parameter name="groups">java,scala,3rdParty,spark</parameter>
      <parameter name="group.java">javax?\..*</parameter>
      <parameter name="group.scala">scala\..*</parameter>
      <parameter name="group.3rdParty">(?!org\.apache\.spark\.).*</parameter>
      <parameter name="group.spark">org\.apache\.spark\..*</parameter>
    </parameters>
  </check>

  <check level="error" class="org.scalastyle.scalariform.DisallowSpaceBeforeTokenChecker" enabled="true">
    <parameters>
      <parameter name="tokens">COMMA</parameter>
    </parameters>
  </check>

  <!-- SPARK-3854: Single Space between ')' and '{' -->
  <check customId="SingleSpaceBetweenRParenAndLCurlyBrace" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">\)\{</parameter></parameters>
    <customMessage><![CDATA[
      Single Space between ')' and `{`.
    ]]></customMessage>
  </check>

  <check customId="NoScalaDoc" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">(?m)^(\s*)/[*][*].*$(\r|)\n^\1  [*]</parameter></parameters>
    <customMessage>Use Javadoc style indentation for multiline comments</customMessage>
  </check>

  <check customId="OmitBracesInCase" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
    <parameters><parameter name="regex">case[^\n>]*=>\s*\{</parameter></parameters>
    <customMessage>Omit braces in case clauses.</customMessage>
  </check>

  <!-- SPARK-16877: Avoid Java annotations -->
  <check level="error" class="org.scalastyle.scalariform.OverrideJavaChecker" enabled="true"></check>

  <check level="error" class="org.scalastyle.scalariform.DeprecatedJavaChecker" enabled="true"></check>

  <!-- ================================================================================ -->
  <!--       rules we'd like to enforce, but haven't cleaned up the codebase yet        -->
  <!-- ================================================================================ -->

  <!-- We cannot turn the following two on, because it'd fail a lot of string interpolation use cases. -->
  <!-- Ideally the following two rules should be configurable to rule out string interpolation. -->
  <check level="error" class="org.scalastyle.scalariform.NoWhitespaceBeforeLeftBracketChecker" enabled="false"></check>
  <check level="error" class="org.scalastyle.scalariform.NoWhitespaceAfterLeftBracketChecker" enabled="false"></check>

  <!-- This breaks symbolic method names so we don't turn it on. -->
  <!-- Maybe we should update it to allow basic symbolic names, and then we are good to go. -->
  <check level="error" class="org.scalastyle.scalariform.MethodNamesChecker" enabled="false">
    <parameters>
    <parameter name="regex"><![CDATA[^[a-z][A-Za-z0-9]*$]]></parameter>
    </parameters>
  </check>

  <!-- Should turn this on, but we have a few places that need to be fixed first -->
  <check level="error" class="org.scalastyle.scalariform.EqualsHashCodeChecker" enabled="true"></check>

  <!-- ================================================================================ -->
  <!--                               rules we don't want                                -->
  <!-- ================================================================================ -->

  <check level="error" class="org.scalastyle.scalariform.IllegalImportsChecker" enabled="false">
    <parameters><parameter name="illegalImports"><![CDATA[sun._,java.awt._]]></parameter></parameters>
  </check>

  <!-- We want the opposite of this: NewLineAtEofChecker -->
  <check level="error" class="org.scalastyle.file.NoNewLineAtEofChecker" enabled="false"></check>

  <!-- This one complains about all kinds of random things. Disable. -->
  <check level="error" class="org.scalastyle.scalariform.SimplifyBooleanExpressionChecker" enabled="false"></check>

  <!-- We use return quite a bit for control flows and guards -->
  <check level="error" class="org.scalastyle.scalariform.ReturnChecker" enabled="false"></check>

  <!-- We use null a lot in low level code and to interface with 3rd party code -->
  <check level="error" class="org.scalastyle.scalariform.NullChecker" enabled="false"></check>

  <!-- Doesn't seem super big deal here ... -->
  <check level="error" class="org.scalastyle.scalariform.NoCloneChecker" enabled="false"></check>

  <!-- Doesn't seem super big deal here ... -->
  <check level="error" class="org.scalastyle.file.FileLengthChecker" enabled="false">
    <parameters><parameter name="maxFileLength">800></parameter></parameters>
  </check>

  <!-- Doesn't seem super big deal here ... -->
  <check level="error" class="org.scalastyle.scalariform.NumberOfTypesChecker" enabled="false">
    <parameters><parameter name="maxTypes">30</parameter></parameters>
  </check>

  <!-- Doesn't seem super big deal here ... -->
  <check level="error" class="org.scalastyle.scalariform.CyclomaticComplexityChecker" enabled="false">
    <parameters><parameter name="maximum">10</parameter></parameters>
  </check>

  <!-- Doesn't seem super big deal here ... -->
  <check level="error" class="org.scalastyle.scalariform.MethodLengthChecker" enabled="false">
    <parameters><parameter name="maxLength">50</parameter></parameters>
  </check>

  <!-- Not exactly feasible to enforce this right now. -->
  <!-- It is also infrequent that somebody introduces a new class with a lot of methods. -->
  <check level="error" class="org.scalastyle.scalariform.NumberOfMethodsInTypeChecker" enabled="false">
    <parameters><parameter name="maxMethods"><![CDATA[30]]></parameter></parameters>
  </check>

  <!-- Doesn't seem super big deal here, and we have a lot of magic numbers ... -->
  <check level="error" class="org.scalastyle.scalariform.MagicNumberChecker" enabled="false">
    <parameters><parameter name="ignore">-1,0,1,2,3</parameter></parameters>
  </check>

</scalastyle>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!