lasselupe33 / eslint-plugin-comment-length

MIT License
13 stars 3 forks source link

Auto-fix doesn't respect tabs #5

Closed CreativeTechGuy closed 1 year ago

CreativeTechGuy commented 1 year ago

When using the tab character for indentation, the auto-fix messes up formatting in some pretty bad ways. Here's two examples:

single-line comment:

function bigFunction() {
    // This is a very long single-line comment which should be an error and should be broken up. It keeps going all on one line and exceeds the character limit
}

This uses a tab to indent the comment, it is auto-fixed to: (Note that the newly added line is indented with the same number of spaces as there were tabs previously - this holds for any number of tabs)

function bigFunction() {
    // This is a very long single-line comment which should be an error and should be broken up. It keeps going all on one
 // line and exceeds the character limit
}

multi-line comment:

function bigFunction() {
    /**
     * This is my explanation of the test function. It is broken into multiple paragraphs since there is a lot to explain and needs to be written out fully.
     *
     * Here starts the second paragraph. There's a line break between the paragraphs since it needs one for the JSDoc to render with separate paragraphs as intended.
     */
    function innerTestFunction() {}
}

This again uses tabs, when auto-fixed it looks like:

    /**
  * * This is my explanation of the test function. It is broken into multiple paragraphs since there is a lot to
  * explain and needs to be written out fully. *
  * * Here starts the second paragraph. There's a line break between the paragraphs since it needs one for the JSDoc to
  * render with separate paragraphs as intended.
     */
    function innerTestFunction() {}

Here's an online sandbox to play around with the examples I listed above: https://stackblitz.com/edit/stackblitz-starters-fvft8y?file=src%2Ftest.js

Unfortunately as is currently this isn't usable with tabs. The practical problems are:

I think this tool is amazing and I'm so glad you made it. Is it possible that it can be compatible with those who use tabs?

lasselupe33 commented 1 year ago

Hi @CreativeTechGuy

I appreciate the kind words.

Based on your examples it is very clear the this plugin was not suited for codebases indented using tabs. This was the case as this plugin has primarily been used internally, and as such this issue has never been relevant before.

However, it was fairly straightforward to add support and therefore tabs should be supported from v1.4.4. In case you encounter any bugs related to the implementation, then feel free that add a PR that fixes these issues.

NB: For overflow detection to function as expected, then the plugin needs to know your configured tab size. If you are using VSCode, then this option should match the editor.tabSize option. (By default the configured tabSize is 2)

The tabSize can be configured by adding the snippet below to your ESLint rules config.

    "comment-length/limit-single-line-comments": [
      "warn",
      {
        tabSize: 2,
      },
    ],
    "comment-length/limit-multi-line-comments": [
      "warn",
      {
        tabSize: 2,
      },
    ],