14.4.1 • Published 4 days ago

npm-groovy-lint v14.4.1

Weekly downloads
3,608
License
GPL-3.0-only
Repository
github
Last release
4 days ago

NPM GROOVY LINT (+ Format & Auto-fix)

Version Downloads/week Downloads/total Test codecov Mega-Linter GitHub contributors GitHub stars Docker Pulls Docker Stars License PRs Welcome

Groovy & Jenkinsfile Linter, Formatter and Auto-fixer

New: The article about the story of npm-groovy-lint, and why you should dive in open-source community !

Based on CodeNarc , this out of the box package allows to track groovy errors and correct a part of them

  • Use option --format to format & prettify source code
  • Use option --fix to activate autofixing of fixable rules

Easy to integrate in a CI/CD process (Jenkins Pipeline,CircleCI...) to lint your groovy or Jenkinsfile at each build :)

You can also use this package in :

image

See CHANGELOG

Any question, problem or enhancement request ? Ask here :)

Usage

    npm-groovy-lint [OPTIONS] [FILES|PATH|PATTERN]
ParameterTypeDescription
-o --outputStringOutput format (txt,json,sarif,html,xml), or path to a file with one of these extensions Default: txt Examples: - "txt" - "json" - "./logs/myLintResults.txt" - "./logs/myLintResults.sarif" - "./logs/myLintResults.html" - "./logs/myLintResults.xml"Note: HTML and XML are directly from CodeNarc so using these formats will disable many npm-groovy-lint features
-l --loglevelStringLog level (error,warning or info)Default: info
--failonStringDefines the error level where CLI will fail (return code = 1). error,warning,info or none. Each failure level includes the more critical ones.
-c --configStringCustom path to GroovyLint config file, or preset config recommended\|recommended-jenkinsfile\|all Default: Browse current directory to find .groovylintrc.json\|js\|yml\|package.json config file, or default npm-groovy-lint config if not defined.Note: command-line arguments have priority on config file properties
--parseBooleanTry to compile the source code and return parse errors (since v5.7.0, default to true, use --no-parse to deactivate)
--formatBooleanFormat source code
--fixBooleanAutomatically fix problems when possible See Auto-fixable rules
-x --fixrulesStringOption for --fix argument: List of rule identifiers to fix (if not specified, all available fixes will be applied). See Auto-fixable rules Examples: - "SpaceBeforeClosingBrace,SpaceAfterClosingBrace,UnusedImport" - "Indentation"
--nolintafterBooleanWhen format or fix is called, a new lint is performed after the fixes to update the returned error list. If you just want the updated source code and do not care about the error logs, use this parameter to improve performances
-r --rulesetsStringRuleSet file(s) to use for linting, if you do not want to use recommended rules or .groovylintrc.js defined rules.If list of comma separated strings corresponding to CodeNarc rules, a RuleSet file will be dynamically generated Examples: - "./config/codenarc/RuleSet-Custom.groovy" - "./path/to/my/ruleset/files"- Indentation{"spacesPerIndentLevel":2,"severity":"warning"},UnnecessarySemicolon,UnnecessaryGString
--rulesetsoverridetypeStringIf list of rules sent in rulesets option, defines if they replace rules defined in .groovylintrc.json, or if they are appended Values: replaceConfig (default), appendConfig
-s --sourceStringIf path and files are not set, you can directly send the source code string to analyze
--verboseBooleanMore outputs in console, including performed fixes
-i --ignorepatternStringComma-separated list of Ant-style file patterns specifying files that must be ignored Default: none Example: "**/test/*""
--noserverBooleannpm-groovy-lint launches a microservice to avoid performance issues caused by loading java/groovy each time,that auto kills itself after 1h idle. Use this argument if you do not want to use this feature
--returnrulesBooleanReturn rules descriptions and URL if set
--javaexecutableStringOverride java executable to use Default: javaExample: C:\Program Files\Java\jdk1.8.0_144\bin\java.exe
--javaoptionsStringOverride java options to use Default: "-Xms256m,-Xmx2048m"
--insightBooleannpm-groovy-lint collects anonymous usage statistics using amplitude, in order to make new improvements based on how users use this package. Summary charts are available at https://tinyurl.com/groovy-stats. Analytics obviously does not receive sensitive information like your code, as you can see in analytics.js. If you want to enable anonymous usage statistics, use --insight option.
--codenarcargsStringUse core CodeNarc arguments (all npm-groovy-lint arguments will be ignored) Doc: http://codenarc.github.io/CodeNarc/codenarc-command-line.html Example: npm-groovy-lint --codenarcargs -basedir="lib/example" -rulesetfiles="file:lib/example/RuleSet-Groovy.groovy" -maxPriority1Violations=0 -report="xml:ReportTestCodenarc.xml
-h --helpBooleanShow help (npm-groovy-lint -h OPTIONNAME to see option detail with examples)
-v --versionBooleanShow npm-groovy-lint version (with CodeNarc version)
-p --pathString(DEPRECATED) Directory containing the files to lint Example: ./path/to/my/groovy/files
-f --filesString(DEPRECATED) Comma-separated list of Ant-style file patterns specifying files that must be included. Default: "**/*.groovy,**/Jenkinsfile,**/*.gradle"Examples: - "**/Jenkinsfile" - "**/*.groovy" - "**/*.gradle" - "**/mySingleFile.groovy"

Example calls

  • Lint a file
    npm-groovy-lint path/to/my/groovy/file.groovy
  • Lint multiple files
    npm-groovy-lint path/to/my/groovy/file.groovy path/to/my/groovy/file2.groovy path/to/my/groovy/file3.groovy
  • Lint directory
    npm-groovy-lint path/to/my/groovy
  • Lint pattern
    npm-groovy-lint path/to/my/groovy/*.groovy
  • Lint groovy with JSON output
    npm-groovy-lint --output json
  • Format files
    npm-groovy-lint --format my/path/to/file.groovy my/path/to/file2.groovy
  • Format and fix files
    npm-groovy-lint --fix my/path/to/file.groovy my/path/to/file2.groovy
  • Get formatted sources in stdout from stdin
    cat path/to/my/Jenkinsfile | npm-groovy-lint --format -
  • Advanced config
    npm-groovy-lint --path "./path/to/my/groovy/files" --files "**/*.groovy" --config "./config/codenarc/.groovylintrcCustom.js" --loglevel warning --output txt
  • Lint using core CodeNarc parameters and generate HTML report file
    npm-groovy-lint --codenarcargs -basedir="lib/example" -rulesetfiles="file:lib/example/RuleSet-Groovy.groovy" -title="TestTitleCodenarc" -maxPriority1Violations=0' -report="html:ReportTestCodenarc.html"

Installation

    npm install -g npm-groovy-lint
  • If you have issues with v9, install previous version with npm install -g npm-groovy-lint@8.2.0
  • Node.js >= 12 is required to run this package. If you can't upgrade, you can use nvm to have different node versions on your computer
  • If you do not have java 17 installed on your computer, npm-groovy-lint will install them for you, so the first run may be long.

Configuration

Default rules definition (recommended, based on all tracks a lot of errors, do not hesitate to ignore some of them (like NoDef ou RequiredVariableType) if they are too mean for your project.

Create a file named .groovylintrc.json in the current or any parent directory of where your files to analyze are located

  • your-repo-root-folder
    • src
      • groovy
        • mygroovyfile.groovy
    • Jenkinsfile
    • .groovylintrc.json (do not forget the dot at the beginning of the file name)

If you are using VsCode Groovy Lint extension, just use QuickFix Ignore in all files and it will generate groovylintrc.json file.

Format

  • extends: Name of a base configuration (recommended, recommended-jenkinsfile, all)
  • rules: List of rules definition, following format "RuleSection.RuleName": ruleParameters or "RuleName": ruleParameters
    • RuleName: any of the CodeNarc rules
    • ruleParameters: can be just a severity override ( "off", "error", "warning", "info" ) , or a property list :

OR

  • codenarcRulesets: Comma-separated string containing the list of .xml or .groovy CodeNarc RuleSet files (in case you already are a CodeNarc user and do not wish to switch to npm-groovy-lint config format)

Examples

{
    "extends": "recommended",
    "rules": {
        "comments.ClassJavadoc": "off",
        "formatting.Indentation": {
            "spacesPerIndentLevel": 4,
            "severity": "info"
        },
        "UnnecessaryReturnKeyword": "error"
    }
}
{
    "extends": "recommended-jenkinsfile",
    "rules": {
        "CouldBeElvis": "off",
        "CouldBeSwitchStatement": "off",
        "VariableName": {
            "severity": "info"
        }
    }
}
{
    "codenarcRulesets": "RuleSet-1.groovy,RuleSet-2.groovy"
}

Disabling rules in source

You can disable rules directly by adding comment in file, using eslint style

To temporarily disable rule warnings in your file, use block comments in the following format:

/* groovylint-disable */

def variable = 1;

/* groovylint-enable */

You can also disable or enable warnings for specific rules:

/* groovylint-disable NoDef, UnnecessarySemicolon */

def variable = 1;

/* groovylint-enable NoDef, UnnecessarySemicolon */

To disable rule warnings in an entire file, put a /* groovylint-disable */ block comment at the top of the file:

/* groovylint-disable */

def variable = 1;

You can also disable or enable specific rules for an entire file:

/* groovylint-disable NoDef */

def variable = 1;

To disable all rules on a specific line, use a line or block comment in one of the following formats:

def variable = 1; // groovylint-disable-line

// groovylint-disable-next-line
def variable = 1;

/* groovylint-disable-next-line */
def variable = 1;

def variable = 1; /* groovylint-disable-line */

To disable a specific rule on a specific line:

def variable = 1; // groovylint-disable-line NoDef

// groovylint-disable-next-line NoDef
def variable = 1;

def variable = 1; /* groovylint-disable-line NoDef */

/* groovylint-disable-next-line NoDef */
def variable = 1;

To disable multiple rules on a specific line:

def variable = 1; // groovylint-disable-line NoDef, UnnecessarySemicolon

// groovylint-disable-next-line NoDef, UnnecessarySemicolon
def variable = 1;

def variable = 1; /* groovylint-disable-line NoDef, UnnecessarySemicolon */

/* groovylint-disable-next-line NoDef, UnnecessarySemicolon */
def variable = 1;

Auto-Fixable rules

  • AssignmentInConditional
  • BlankLineBeforePackage
  • BlockEndsWithBlankLine
  • BlockStartsWithBlankLine
  • BracesForClass
  • BracesForForLoop
  • BracesForIfElse
  • BracesForMethod
  • BracesForTryCatchFinally
  • ClassEndsWithBlankLine
  • ClassStartsWithBlankLine
  • ClosingBraceNotAlone
  • ConsecutiveBlankLines
  • DuplicateImport
  • ElseBlockBraces
  • ExplicitArrayListInstantiation
  • ExplicitLinkedListInstantiation
  • FileEndsWithoutNewline
  • IfStatementBraces
  • Indentation
  • IndentationClosingBraces
  • IndentationComments
  • InsecureRandom
  • MisorderedStaticImports
  • MissingBlankLineAfterImports
  • MissingBlankLineAfterPackage
  • NoTabCharacter
  • SpaceAfterCatch
  • SpaceAfterComma
  • SpaceAfterFor
  • SpaceAfterIf
  • SpaceAfterOpeningBrace
  • SpaceAfterSemicolon
  • SpaceAfterSwitch
  • SpaceAfterWhile
  • SpaceAroundOperator
  • SpaceBeforeClosingBrace
  • SpaceBeforeOpeningBrace
  • TrailingWhitespace
  • UnnecessaryDefInFieldDeclaration
  • UnnecessaryDefInMethodDeclaration
  • UnnecessaryDefInVariableDeclaration
  • UnnecessaryDotClass
  • UnnecessaryFinalOnPrivateMethod
  • UnnecessaryGString
  • UnnecessaryGroovyImport
  • UnnecessaryPackageReference
  • UnnecessaryParenthesesForMethodCallWithClosure
  • UnnecessarySemicolon
  • UnnecessaryToString
  • UnusedImport

Contribute to add more rules fixes :)

CI/CD

Mega-Linter

Latest npm-groovy-lint version is natively integrated in Mega-Linter, that you can use as GitHub action or in other CI tools This tool can also automatically apply fixes on Pull Request branches

CircleCI

# .circleci/config.yml
version: 2.1
jobs:
  lint:
    docker:
      - image: nvuillam/npm-groovy-lint
    steps:
      - checkout

      - run: |
          npm-groovy-lint

workflows:
  version: 2
  "lint":
    jobs:
       - lint

Jenkins

node {
    checkout scm
    docker.image('nvuillam/npm-groovy-lint').inside {
        sh 'npm-groovy-lint'
    }
}

Shell

Run with default settings

docker run --rm -u "$(id -u):$(id -g)" -w=/tmp -v "$PWD":/tmp nvuillam/npm-groovy-lint

Run with additional flags by simply appending them at after docker image name:

docker run --rm -u "$(id -u):$(id -g)" -w=/tmp -v "$PWD":/tmp nvuillam/npm-groovy-lint --failon warning --verbose

Other

You can run npm-groovy-lint using its official docker image

Use as module

You can import npm-groovy-lint into your NPM package and call lint & fix via module, using the same options than from npm-groovy-lint command line

Example

    npm install npm-groovy-lint --save
    const NpmGroovyLint = require("npm-groovy-lint/lib/groovy-lint.js");
    const fse = require("fs-extra");

    const npmGroovyLintConfig = {
        source: fse.readFileSync('./lib/example/SampleFile.groovy').toString(),
        fix: true,
        loglevel: 'warning',
        output: 'none'
    };
    const linter = new NpmGroovyLint(npmGroovyLintConfig, {});
    await linter.run();
    console.log(JSON.stringify(linter.lintResult));

Contribute

Contributions are very welcome !

Please follow Contribution instructions

Thanks

Other packages used

  • CodeNarc: groovy lint
  • java-caller: Easy call Java commands from Node
  • slf4j: logging for CodeNarc
  • log4j: logging for CodeNarc
  • GMetrics: Code measures for CodeNarc
  • Inspiration from eslint about configuration and run patterns

Contributors

Nicolas VuillamyDave GallantHoward LoPawel KopkadocwhatCatSue

Release notes

See complete CHANGELOG

14.4.1

4 days ago

14.3.0

24 days ago

14.4.0

23 days ago

14.2.4

1 month ago

14.2.3

2 months ago

14.2.2

2 months ago

14.2.1

2 months ago

14.2.0

3 months ago

14.0.1

4 months ago

14.0.0

4 months ago

13.0.2

5 months ago

13.0.1

5 months ago

12.0.0

6 months ago

12.0.1

6 months ago

13.0.0

5 months ago

12.2.0

5 months ago

12.1.2

5 months ago

12.1.0

5 months ago

12.1.1

5 months ago

11.1.1

1 year ago

11.1.0

1 year ago

11.0.0

2 years ago

10.0.0

2 years ago

10.0.1

2 years ago

10.0.2

2 years ago

10.0.3

2 years ago

10.1.0

2 years ago

9.5.0

2 years ago

9.4.1

2 years ago

9.4.0

2 years ago

9.1.0

2 years ago

9.3.2

2 years ago

9.3.1

2 years ago

9.3.0

2 years ago

9.2.0

2 years ago

9.0.0

3 years ago

8.1.1

3 years ago

8.2.0

3 years ago

8.1.0

3 years ago

8.0.2

3 years ago

8.0.1

3 years ago

8.0.0

3 years ago

8.0.0-beta.5

3 years ago

8.0.0-beta.4

3 years ago

8.0.0-beta.2

3 years ago

8.0.0-beta.3

3 years ago

8.0.0-beta.1

3 years ago

7.6.4

3 years ago

7.6.2

4 years ago

7.6.3

4 years ago

7.6.1

4 years ago

7.6.0

4 years ago

7.5.5

4 years ago

7.5.4

4 years ago

7.5.3

4 years ago

7.5.2

4 years ago

7.5.1

4 years ago

7.5.0

4 years ago

7.4.4

4 years ago

7.4.3

4 years ago

7.4.2

4 years ago

7.4.1

4 years ago

7.4.0

4 years ago

7.3.1

4 years ago

7.3.0

4 years ago

7.2.0

4 years ago

7.1.0

4 years ago

7.0.0

4 years ago

6.1.1

4 years ago

6.1.0

4 years ago

6.0.0

4 years ago

5.8.0

4 years ago

6.0.0-beta.3

4 years ago

6.0.0-beta.2

4 years ago

6.0.0-beta.1

4 years ago

5.7.0

4 years ago

5.6.1

4 years ago

5.6.0

4 years ago

5.5.1

4 years ago

5.5.0

4 years ago

5.4.2

4 years ago

5.4.1

4 years ago

5.4.0

4 years ago

5.3.0

4 years ago

5.2.0-beta.0

4 years ago

5.1.1

4 years ago

5.1.0

4 years ago

5.0.3

4 years ago

5.0.2

4 years ago

5.0.0-beta.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.14.0

4 years ago

4.13.0

4 years ago

4.12.0

4 years ago

4.11.1

4 years ago

4.11.0

4 years ago

4.10.2

4 years ago

4.10.1

4 years ago

4.10.0

4 years ago

4.9.0

4 years ago

4.8.0

4 years ago

4.7.0

4 years ago

4.6.0

4 years ago

4.5.5

4 years ago

4.5.4

4 years ago

4.5.3

4 years ago

4.5.2

4 years ago

4.5.1

4 years ago

4.5.0

4 years ago

4.4.1

4 years ago

4.4.0

4 years ago

4.3.1

4 years ago

4.3.0

4 years ago

4.2.0

4 years ago

4.1.0

4 years ago

4.0.0

4 years ago

3.3.2

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.4

4 years ago

3.2.3

4 years ago

3.2.3-beta.2

4 years ago

3.2.3-beta.1

4 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.2.1-beta.2

4 years ago

3.2.1-beta.1

4 years ago

3.2.0

4 years ago

3.2.0-beta.1

4 years ago

3.1.3

4 years ago

3.1.2

4 years ago

3.1.2-beta.0

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.1-beta.4

4 years ago

3.0.1-beta.3

4 years ago

3.0.1-beta.2

4 years ago

3.0.1-beta.1

4 years ago

3.0.0

4 years ago

3.0.0-beta.8

4 years ago

3.0.0-beta.7

4 years ago

3.0.0-beta.6

4 years ago

3.0.0-beta.5

4 years ago

3.0.0-beta.4

4 years ago

3.0.0-beta.3

4 years ago

3.0.0-beta.2

4 years ago

2.2.1-beta.2

4 years ago

3.0.0-beta.1

4 years ago

2.2.1-beta.1

4 years ago

2.2.0-beta.9

4 years ago

2.2.0

4 years ago

2.2.0-beta.8

4 years ago

2.2.0-beta.7

4 years ago

2.2.0-beta.6

4 years ago

2.2.0-beta.5

4 years ago

2.2.0-beta.4

4 years ago

2.2.0-beta.1

4 years ago

2.2.0-beta.2

4 years ago

2.2.0-beta.3

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

2.0.0-beta3

4 years ago

2.0.0-beta4

4 years ago

2.0.0-beta.2

4 years ago

2.0.0-beta.1

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.2.0-beta.10

4 years ago

1.2.0-beta.8

4 years ago

1.2.0-beta.7

4 years ago

1.2.0-beta.6

4 years ago

1.2.0-beta.1

4 years ago

1.2.0-beta.0

4 years ago

1.2.0-beta.2

4 years ago

1.2.0-beta.5

4 years ago

1.1.0

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago