nvuillam / npm-groovy-lint

Lint, format and auto-fix your Groovy / Jenkinsfile / Gradle files using command line
https://nvuillam.github.io/npm-groovy-lint/
MIT License
205 stars 63 forks source link

Make a warning when enabling non-disabled rule #359

Closed alexanderbazhenoff closed 7 months ago

alexanderbazhenoff commented 7 months ago

Hi, I have a working Jenkins scripted style pipeline with node selection before node(){} and the whole pipeline:

pipeline.groovy:

#!/usr/bin/env groovy

/**
 * This part of code performs node selection by Jenkins node tag (NODE_TAG_TO_EXECUTE) or node name (NODE_TO_EXECUTE).
 * Until nodeToExecute can be Map or String it was declared as an Object to avoid dynamic type warning when linting.
 */
Object nodeToExecute = null
// groovylint-disable-next-line UnnecessaryGetter
nodeToExecute = (env.getEnvironment().containsKey('NODE_TAG_TO_EXECUTE') && env['NODE_TAG_TO_EXECUTE']?.trim()) ?
        [label: env['NODE_TAG_TO_EXECUTE']] : nodeToExecute
// groovylint-disable-next-line UnnecessaryGetter
nodeToExecute = (!env.getEnvironment().containsKey('NODE_TO_EXECUTE') && env.getEnvironment()
        .containsKey('NODE_TO_EXECUTE') && env['NODE_TO_EXECUTE']?.trim()) ? env['NODE_TO_EXECUTE'] : nodeToExecute

/** After a jenkins node selection pipeline code itself. */
node(nodeToExecute) {
    wrap([$class: 'TimestamperBuildWrapper']) {
        println String.format('Hello, I am a %s node', nodeToExecute)
        // When I edit my code I have removed disable rule line and this one was forgotten:
        /* groovylint-enable UnnecessaryGetter */
    }
}

but npm-groovy-lint fails:

npm-groovy-lint: GroovyLint: Successfully processed CodeNarc: 
CodeNarc version 3.3.0
npm-groovy-lint version 14.2.1
Embeds:
CodeNarc version 3.3.0
- Groovy version 3.0.9 (superlite)

...

2024-02-17 23:04:16 [INFO]   Linting GROOVY items...
2024-02-17 23:04:36 [ERROR]   Found errors when linting GROOVY. Exit code: 1.
2024-02-17 23:04:36 [INFO]   Stderr contents for GROOVY:
------
Unexpected error: console.warning is not a function
TypeError: console.warning is not a function
    at collectDisabledBlocks (/node_modules/npm-groovy-lint/lib/filter.js:26:25)
    at parseCodeNarcResult (/node_modules/npm-groovy-lint/lib/codenarc-factory.js:303:36)
    at async NpmGroovyLint.postProcess (/node_modules/npm-groovy-lint/lib/groovy-lint.js:298:31)
    at async NpmGroovyLint.run (/node_modules/npm-groovy-lint/lib/groovy-lint.js:69:13)
    at async /node_modules/npm-groovy-lint/lib/index.js:12:9

My settings are:

{
  "extends": "recommended",
  "rules": {
    "comments.ClassJavadoc": "off",
    "formatting.Indentation": {
      "spacesPerIndentLevel": 4,
      "severity": "info"
    },
    "braces.ElseBlockBraces": {
      "enabled": false
    },
    "braces.ForStatementBraces": {
      "enabled": false
    },
    "braces.IfStatementBraces": {
      "enabled": false
    },
    "braces.WhileStatementBraces": {
      "enabled": false
    },
    "unused.UnusedVariable": {
      "ignoreVariableNames": "__"
    },
    "size.NestedBlockDepth": {
      "maxNestedBlockDepth": 8
    },
    "size.ParameterCount": {
      "enabled": false
    },
    "naming.VariableName": {
      "enabled": false
    },
    "ConsecutiveBlankLines": {
      "enabled": false
    },
    "CatchException": {
      "enabled": false
    },
    "Instanceof": {
      "enabled": false
    },
    "ImplicitReturnStatement": {
      "enabled": false
    },
    "ImplicitClosureParameter": {
      "enabled": false
    },
    "convention.CompileStatic": {
      "enabled": false
    },
    "DuplicateStringLiteral": {
      "enabled": false
    },
    "DuplicateNumberLiteral": {
      "enabled": false
    },
    "UnnecessaryReturnKeyword": "info"
  }
}

It's funny how I tried to changed node selection part thinking the bug is here, but I have removed forgotten line /* groovylint-enable UnnecessaryGetter */ and it's pass...

Can you please make an ignore of enabling non-disabled rule in the future (instead of fail)? :) Or better add some warning that the rule is not disabled to enable them.

nvuillam commented 7 months ago

It seems console.warning , which was deprecated, has finally been removed :)

PR on the way :)

nvuillam commented 7 months ago

@alexanderbazhenoff please can you check it's now ok with v14.2.2 https://github.com/nvuillam/npm-groovy-lint/releases/tag/v14.2.2 ? :)

alexanderbazhenoff commented 7 months ago

please can you check it's now ok with v14.2.2

I'll try to do next week, need to finish my code refactoring first on super-linter, than replace it in CI. Thank you.

alexanderbazhenoff commented 7 months ago

@nvuillam Hi, sorry for long waiting reply. I din't swtich to MegaLinter yet, but Super-Linter was updated and now includes npm-groovy-lint version 14.2.2 and CodeNarc version 3.3.0. The code pattern mentioned by me sucesfully pass, looks like you can close a bug. Thanks once again for quick reaction.

nvuillam commented 7 months ago

@alexanderbazhenoff thanks for your feedback :)