Skip to content

Commit

Permalink
Issue checkstyle#11734: Add codenarc to CI for groovy files
Browse files Browse the repository at this point in the history
  • Loading branch information
Vyom-Yadav committed Jun 18, 2022
1 parent a5450fc commit 81c0be3
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 61 deletions.
113 changes: 68 additions & 45 deletions .ci/StarterRuleSet-AllRulesByCategory.groovy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ ruleset {
HardCodedWindowsRootDirectory
IntegerGetInteger
MultipleUnaryOperators
ParameterAssignmentInFilterClosure
RandomDoubleCoercedToZero
RemoveAllOnSelf
ReturnFromFinallyBlock
Expand All @@ -57,6 +58,23 @@ ruleset {
IfStatementBraces
WhileStatementBraces

// rulesets/comments.xml
ClassJavadoc
JavadocConsecutiveEmptyLines
JavadocEmptyAuthorTag
JavadocEmptyExceptionTag
JavadocEmptyFirstLine
JavadocEmptyLastLine
JavadocEmptyParamTag
JavadocEmptyReturnTag
JavadocEmptySeeTag
JavadocEmptySinceTag
JavadocEmptyThrowsTag
JavadocEmptyVersionTag
JavadocMissingExceptionDescription
JavadocMissingParamDescription
JavadocMissingThrowsDescription

// rulesets/concurrency.xml
BusyWait
DoubleCheckedLocking
Expand Down Expand Up @@ -86,25 +104,32 @@ ruleset {
WaitOutsideOfWhileLoop

// rulesets/convention.xml
CompileStatic
ConfusingTernary

// Disabled as the Elvis operator is a very weired language structure. It may lead to potential
// bugs and introduce mess in the code. It is better to use traidional if-else block.
// CouldBeElvis

CouldBeElvis
CouldBeSwitchStatement
FieldTypeRequired
HashtableIsObsolete
IfStatementCouldBeTernary
ImplicitClosureParameter
ImplicitReturnStatement
InvertedCondition
InvertedIfElse
LongLiteralWithLowerCaseL

// def is commonly used in Groovy, as it provides greater flexibility and readability.
// The keywoard removes boilerplate explicit type declaration.
// NoDef

MethodParameterTypeRequired
MethodReturnTypeRequired
NoDef
NoDouble
NoFloat
NoJavaUtilDate
NoTabCharacter
ParameterReassignment
PublicMethodsBeforeNonPublicMethods
StaticFieldsBeforeInstanceFields
StaticMethodsBeforeInstanceMethods
TernaryCouldBeElvis
TrailingComma
VariableTypeRequired
VectorIsObsolete

// rulesets/design.xml
Expand All @@ -123,6 +148,9 @@ ruleset {
Instanceof
LocaleSetDefault
NestedForLoop
OptionalCollectionReturnType
OptionalField
OptionalMethodParameter
PrivateFieldCouldBeFinal
PublicInstanceField
ReturnsNullInsteadOfEmptyArray
Expand All @@ -135,11 +163,12 @@ ruleset {
DuplicateListLiteral
DuplicateMapLiteral
DuplicateNumberLiteral
//DuplicateStringLiteral // it is ok to use simple duplication in scripts
DuplicateStringLiteral

// rulesets/enhanced.xml
CloneWithoutCloneable
JUnitAssertEqualsConstantActualValue
MissingOverrideAnnotation
UnsafeImplementationAsMap

// rulesets/exceptions.xml
Expand All @@ -166,35 +195,41 @@ ruleset {

// rulesets/formatting.xml
BlankLineBeforePackage
BlockEndsWithBlankLine
BlockStartsWithBlankLine
BracesForClass
BracesForForLoop
BracesForIfElse
BracesForMethod
BracesForTryCatchFinally
ClassJavadoc
ClassEndsWithBlankLine
ClassStartsWithBlankLine
ClosureStatementOnOpeningLineOfMultipleLineClosure
ConsecutiveBlankLines
FileEndsWithoutNewline
Indentation
LineLength
MissingBlankLineAfterImports
MissingBlankLineAfterPackage
MissingBlankLineBeforeAnnotatedField
SpaceAfterCatch
SpaceAfterClosingBrace
SpaceAfterComma
SpaceAfterFor
SpaceAfterIf
SpaceAfterMethodCallName
SpaceAfterMethodDeclarationName
SpaceAfterNotOperator
SpaceAfterOpeningBrace
SpaceAfterSemicolon
SpaceAfterSwitch
SpaceAfterWhile
SpaceAroundClosureArrow

// The rule makes the Groovy map initializers hard to read.
// SpaceAroundMapEntryColon

SpaceAroundMapEntryColon
SpaceAroundOperator
SpaceBeforeClosingBrace
SpaceBeforeOpeningBrace
SpaceInsideParentheses
TrailingWhitespace

// rulesets/generic.xml
Expand All @@ -209,9 +244,11 @@ ruleset {
StatelessClass

// rulesets/grails.xml
GrailsDomainGormMethods
GrailsDomainHasEquals
GrailsDomainHasToString
GrailsDomainReservedSqlKeywordName
GrailsDomainStringPropertyMaxSize
GrailsDomainWithServiceReference
GrailsDuplicateConstraint
GrailsDuplicateMapping
Expand All @@ -230,11 +267,7 @@ ruleset {
ExplicitCallToAndMethod
ExplicitCallToCompareToMethod
ExplicitCallToDivMethod

// Groovy's `==` operator which is equivalent to 'equals' method from Java makes code weird to
// Java contributors. It is better to use direct call of 'equals' method to compare objects.
// ExplicitCallToEqualsMethod

ExplicitCallToEqualsMethod
ExplicitCallToGetAtMethod
ExplicitCallToLeftShiftMethod
ExplicitCallToMinusMethod
Expand All @@ -243,18 +276,15 @@ ruleset {
ExplicitCallToOrMethod
ExplicitCallToPlusMethod
ExplicitCallToPowerMethod
ExplicitCallToPutAtMethod
ExplicitCallToRightShiftMethod
ExplicitCallToXorMethod

// Suppressed as these rules make the code hard to understand.
// Disabling them helps to have the code more similar to Java.
// ExplicitHashMapInstantiation
// ExplicitHashSetInstantiation
// ExplicitLinkedHashMapInstantiation
// ExplicitLinkedListInstantiation
// ExplicitStackInstantiation
// ExplicitTreeSetInstantiation

ExplicitHashMapInstantiation
ExplicitHashSetInstantiation
ExplicitLinkedHashMapInstantiation
ExplicitLinkedListInstantiation
ExplicitStackInstantiation
ExplicitTreeSetInstantiation
GStringAsMapKey
GStringExpressionWithinString
GetterMethodCouldBeProperty
Expand All @@ -267,7 +297,7 @@ ruleset {
ImportFromSamePackage
ImportFromSunPackages
MisorderedStaticImports
//NoWildcardImports // it is ok to keep scripts as small as possible in header
NoWildcardImports
UnnecessaryGroovyImport
UnusedImport

Expand Down Expand Up @@ -309,7 +339,7 @@ ruleset {
LoggingSwallowsStacktrace
MultipleLoggers
PrintStackTrace
//Println // We use groovy as scripted java, usage of std output is required by design
Println
SystemErrPrint
SystemOutPrint

Expand All @@ -334,7 +364,7 @@ ruleset {
// rulesets/security.xml
FileCreateTempFile
InsecureRandom
//JavaIoPackageAccess // we do not care about EJB container rules
JavaIoPackageAccess
NonFinalPublicField
NonFinalSubclassOfSensitiveInterface
ObjectFinalize
Expand Down Expand Up @@ -381,8 +411,8 @@ ruleset {
UnnecessaryElseStatement
UnnecessaryFinalOnPrivateMethod
UnnecessaryFloatInstantiation
//UnnecessaryGString // just to be the same as in java, as our primary language
//UnnecessaryGetter // we are ok with code to looks like Java
UnnecessaryGString
UnnecessaryGetter
UnnecessaryIfStatement
UnnecessaryInstanceOfCheck
UnnecessaryInstantiationToGetClass
Expand All @@ -396,19 +426,12 @@ ruleset {
UnnecessaryPackageReference
UnnecessaryParenthesesForMethodCallWithClosure
UnnecessaryPublicModifier

// Disabled as it makes the code unclear. It becomes hard to understand what value
// is returned by a method.
// UnnecessaryReturnKeyword

UnnecessaryReturnKeyword
UnnecessarySafeNavigationOperator
UnnecessarySelfAssignment
UnnecessarySemicolon
UnnecessarySetter
UnnecessaryStringInstantiation

// Suppressed to avoid weired code and make the code more similar to Java.
// UnnecessarySubstring

UnnecessaryTernaryExpression
UnnecessaryToString
UnnecessaryTransientModifier
Expand Down
12 changes: 12 additions & 0 deletions .ci/codenarc.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@Grab(group = 'org.codenarc', module = 'CodeNarc-Groovy4', version = '3.1.0')
@Grab(group = 'org.gmetrics', module = 'GMetrics-Groovy4', version = '2.1.0')
@GrabExclude('org.apache.groovy:groovy-xml')
import java.lang.Object

org.codenarc.CodeNarc.main([
"-basedir=${args[0]}",
"-includes=**/${args[1]}",
"-rulesetfiles=./.ci/StarterRuleSet-AllRulesByCategory.groovy.txt",
"-report=console",
] as String[])

13 changes: 0 additions & 13 deletions .ci/codenarc.sh

This file was deleted.

2 changes: 2 additions & 0 deletions .ci/jsoref-spellchecker/whitelist.words
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ googlecloudplatform
googleecommon
googlegroups
googlesource
Gorm
govstrangefolder
gpg
gradle
Expand Down Expand Up @@ -1181,6 +1182,7 @@ scp
screenshot
Scss
sdk
sdkman
selfexplanatory
Selkin
semaphoreci
Expand Down
5 changes: 5 additions & 0 deletions .ci/testFile.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import groovy.xml.XmlParser
import groovy.xml.XmlParser
import groovy.xml.XmlParser

print "Hello"
15 changes: 12 additions & 3 deletions .github/workflows/codenarc-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ jobs:
uses: actions/setup-java@v1
with:
java-version: 11
- name: Install Groovy
- name: Download Groovy 4.0.3
uses: sdkman/sdkman-action@master
id: sdkman
with:
candidate: groovy
version: 4.0.3
- name: Set up Groovy 4.0.3
run: |
sudo apt install groovy
groovy -v
unzip ${{ steps.sdkman.outputs.file }} -d ${{ runner.temp }};
mv ${{ runner.temp }}/groovy-4.0.3 ${{ runner.temp }}/groovy
GROOVY_HOME=${{ runner.temp }}/groovy
echo "GROOVY_HOME=${{ runner.temp }}/groovy" >> $GITHUB_ENV
echo "$GROOVY_HOME/bin" >> $GITHUB_PATH
- name: Checkout
uses: actions/checkout@v2
- name: Run Analysis
Expand Down

0 comments on commit 81c0be3

Please sign in to comment.