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
193 stars 61 forks source link

bug: Wrong indentation for comments #377

Open berthin opened 2 weeks ago

berthin commented 2 weeks ago

Hi,

using the pre-commit hook format-npm-groovy-lint, comments are not formatted correctly. See for instance the following example:

class MyClass {
      /**
      * Some documentation
      *
      * This bla bla bla bla
      */
  static String helloWorld() {
    "Hello World"
  }
}

the comments are indented with 6 spaces, the groovy linter rc file specifies a 2 space indentation, but the pre-commit hook doesn't complain.

I am using this pre-commit configuration

repos:                                                                                
  - repo: https://github.com/nvuillam/npm-groovy-lint                                 
    rev: v14.6.0                                                                      
    hooks:                                                          
      - id: format-npm-groovy-lint                                                    
        name: Format Lint groovy findings                                             
        description: Groovy & Jenkinsfile Formatter                                   
        entry: npm-groovy-lint --format                                               
        language: node  

Another thing I found is if I add comments at the end of the function:

class MyClass {
      /**
      * Some documentation
      *
      * This bla bla bla bla
      */
  static String helloWorld() {
    "Hello World"

    // Note: this is another comment
    // bla bla bla
  }
}

then, the formatter decreases the alignment by 1

class MyClass {
      /**
      * Some documentation
      *
      * This bla bla bla bla
      */
  static String helloWorld() {
    "Hello World"

  // Note: this is another comment
  // bla bla bla
  }
}

Not sure if this by design, or a bug.

berthin commented 2 weeks ago

After looking at https://github.com/nvuillam/npm-groovy-lint/blob/main/lib/rules/IndentationComments.js, I think the heuristic rule to indent comments is wrong. It's purely based on the indentation of the next line, which means:

def function() {
    // some comment
}

will be indented as:

def function() {
// some comment
}

what I'd expect is

def function() {
<N spaces>// some comment
}

where N is specified by the RC file.