Scalastyle examines your Scala code and indicates potential problems with it.
For a list of (planned) rules, see the Scalastyle WIKI. For a list of actual rules, see the Implemented Rules section below
$ mvn exec:java
This process will be improved in time, but for now, you need to build scalastyle and then build scalastyle-batch. First install scalastyle in your local maven repo
$ git clone git://github.com/scalastyle/scalastyle.git
$ cd scalastyle
$ mvn install
Then get and build scalastyle-batch
$ git clone git://github.com/scalastyle/scalastyle-batch.git
$ cd scalastyle-batch
$ mvn package # or mvn install
This will create in scalastyle-batch/target a zip file which contains an executable jar along with the dependencies in lib/. Unzip this file somewhere, and run the jar as such:
$ java -jar scalastyle-batch_2.9.1.jar --config lib/scalastyle_config.xml src/main/scala
or similar. You can copy your initial configuration from lib/scalastyle_config.xml in the scalastyle project, and change it from there.
Snapshots are available from the Sonatype nexus repository: https://oss.sonatype.org/content/repositories/snapshots/. To use, add something like the following to your settings.xml:
<repository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype OSS</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<layout>default</layout>
<releases>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
There are only snapshots available at the minute.
To run the tests:
$ mvn test
Want to contribute? Great! Look at the wiki for potential rules to implement, or do one of your own, and implement it.
- Read the developer guide
- Fork the repo.
- Create a branch (
git checkout -b my_new_rule
) - Commit your changes (
git commit -am "Added NoFooAllowed"
) - Push to the branch (
git push origin my_new_rule
) - Create an Issue with a link to your branch
- Enjoy a coffee and wait
If you wish to ignore a particular scalastyle rule, you can put a comment before and after the line, with the following syntax:
// scalastyle:off
// scalastyle:on
You can also switch off checking for a particular rule by specifying the id of the rule to ignore:
// scalastyle:off magic.number
var foobar = 134
// scalastyle:on magic.number
Messages are defined within the scalastyle_messages.properties. If however, you wish to have a custom error message for a particular rule, then you can do so by defining a customMessage element in the configuration, such as:
<check level="warning" class="org.scalastyle.scalariform.MagicNumberChecker" enabled="true">
<customMessage>Please don't use magic numbers</customMessage>
</check>
These are the rules which are currently implemented:
- id - file.size.limit
- default level - WarningLevel
- Maximum file Length
- id - line.size.limit
- default level - WarningLevel
- Maximum line length
- Tab size
- id - line.contains.tab
- default level - WarningLevel
No Parameters
Class org.scalastyle.file.HeaderMatchesChecker - Check the first lines of each file matches the text
- id - header.matches
- default level - WarningLevel
- Header
Class org.scalastyle.file.WhitespaceEndOfLineChecker - Check that there is no trailing whitespace at the end of lines
- id - whitespace.end.of.line
- default level - WarningLevel
No Parameters
Class org.scalastyle.scalariform.ClassNamesChecker - Check that class names match a regular expression
- id - class.name
- default level - WarningLevel
- Regular expression
Class org.scalastyle.scalariform.CovariantEqualsChecker - Check that classes and objects do not define equals without overriding equals(java.lang.Object).
- id - covariant.equals
- default level - WarningLevel
No Parameters
Class org.scalastyle.scalariform.EqualsHashCodeChecker - Check that if a class implements either equals or hashCode, it should implement the other
- id - equals.hash.code
- default level - WarningLevel
No Parameters
Class org.scalastyle.scalariform.IllegalImportsChecker - Check that a class does not import certain classes
- id - illegal.imports
- default level - WarningLevel
- Illegal Imports
A simple assignment to a val is not considered to be a magic number, for example:
val foo = 4
is not a magic number, but
var foo = 4
is considered to be a magic number.
- id - magic.number
- default level - WarningLevel
- Ignore (list of numbers to ignore)
<check level="warning" class="org.scalastyle.scalariform.MagicNumberChecker" enabled="true">
<parameters>
<parameter name="ignore">-1,0,1,2</parameter>
</parameters>
</check>
Class org.scalastyle.scalariform.NoCloneChecker - Check that classes and objects do not define the clone() method
- id - no.clone
- default level - WarningLevel
No Parameters
Class org.scalastyle.scalariform.NoFinalizeChecker - Check that classes and objects do not define the finalize() method
- id - no.finalize
- default level - WarningLevel
No Parameters
Class org.scalastyle.scalariform.NoWhitespaceAfterLeftBracketChecker - No whitespace after left bracket ''(''
- id - no.whitespace.after.left.bracket
- default level - WarningLevel
No Parameters
Class org.scalastyle.scalariform.NoWhitespaceBeforeLeftBracketChecker - No whitespace before left bracket ''(''
- id - no.whitespace.before.left.bracket
- default level - WarningLevel
No Parameters
- id - null
- default level - WarningLevel
No Parameters
Class org.scalastyle.scalariform.ObjectNamesChecker - Check that object names match a regular expression
- id - object.name
- default level - WarningLevel
- Regular expression
- id - parameter.number
- default level - WarningLevel
- Maximum Number
- id - return
- default level - WarningLevel
No Parameters
Class org.scalastyle.scalariform.SpacesAfterPlusChecker - Check that the plus sign is followed by a space
- id - spaces.after.plus
- default level - WarningLevel
No Parameters
Class org.scalastyle.scalariform.SpacesBeforePlusChecker - Check that the plus sign is preceded by a space
- id - spaces.before.plus
- default level - WarningLevel
No Parameters
- id - structural.type
- default level - WarningLevel
No Parameters
- id - regex
- default level - WarningLevel
- Regular expression the regular expression to use
<check level="warning" class="org.scalastyle.file.RegexChecker" enabled="true">
<parameters>
<parameter name="regex"><![CDATA[(?m)^\s\*$(\r|)\n^\s*$(\r|)\n]]></parameter>
</parameters>
<customMessage>No double blank lines</customMessage>
</check>
The above example would check for double blank lines in a file and report with a custom error.
Class org.scalastyle.scalariform.NumberOfTypesChecker - Check that a file doesn't contain too many types
- id - number.of.types
- default level - WarningLevel
- maxTypes: The maximum number of types (outer & inner classes) to allow per file
<check level="warning" class="org.scalastyle.scalariform.NumberOfTypesChecker" enabled="true">
<parameters>
<parameter name="maxTypes">20</parameter>
</parameters>
</check>
Class org.scalastyle.scalariform.CyclomaticComplexityChecker - Check the cyclomatic complexity of methods
- id - cyclomatic.complexity
- default level - WarningLevel
- maximum: The maximum allowed cyclomatic complexity of a method
<check level="warning" class="org.scalastyle.scalariform.CyclomaticComplexityChecker" enabled="true">
<parameters>
<parameter name="maximum">10</parameter>
</parameters>
</check>
- id - uppercase.l
- default level - WarningLevel
No Parameters
<check level="warning" class="org.scalastyle.scalariform.UppercaseLChecker" enabled="true"/>
- id - if.brace
- default level - WarningLevel
- singleLineAllowed: The lack of braces is allowed if everything is on one line
- doubleLineAllowed: The lack of braces is allowed if the
if
and theelse
are on different lines. The expressions associated with the if and the else still need to be on the same lines though.
<check level="warning" class="org.scalastyle.scalariform.IfBraceChecker" enabled="true">
<parameters>
<parameter name="singleLineAllowed">true</parameter>
<parameter name="doubleLineAllowed">false</parameter>
</parameters>
</check>
- id - method.length
- default level - WarningLevel
- maxLength: Maximum length allowed
<check level="warning" class="org.scalastyle.scalariform.MethodLengthChecker" enabled="true">
<parameters>
<parameter name="maxLength">50</parameter>
</parameters>
</check>
- id - method.name
- default level - WarningLevel
- regex: Regular expression to match, default
^[A-Za-z]*$
<check level="warning" class="org.scalastyle.scalariform.MethodNamesChecker" enabled="true">
<parameters>
<parameter name="regex">^[A-Za-z]*$</parameter>
</parameters>
</check>
Class org.scalastyle.scalariform.NumberOfMethodsInTypeChecker - Check that there are not too many methods declared in a type
- id - number.of.methods
- default level - WarningLevel
- maxMethods: number of methods per type
<check level="warning" class="org.scalastyle.scalariform.NumberOfMethodsInTypeChecker" enabled="true">
<parameters>
<parameter name="maxMethods">30</parameter>
</parameters>
</check>
Class org.scalastyle.scalariform.PublicMethodsHaveTypeChecker - Check that public methods declare an explicit type
- id - public.methods.have.type
- default level - WarningLevel
No Parameters
<check level="warning" class="org.scalastyle.scalariform.PublicMethodsHaveTypeChecker" enabled="true"/>
- id - newline.at.eof
- default level - WarningLevel
No Parameters
<check level="warning" class="org.scalastyle.file.NewlineAtEofChecker" enabled="true"/>
Class org.scalastyle.file.NoNewlineAtEofChecker - Check that files do not end with a newline character
- id - no.newline.at.eof
- default level - WarningLevel
No Parameters
<check level="warning" class="org.scalastyle.file.NoNewlineAtEofChecker" enabled="true"/>