lasselupe33 / eslint-plugin-comment-length

MIT License
13 stars 3 forks source link

Detect early wrapping #4

Closed jaydenseric closed 1 year ago

jaydenseric commented 1 year ago

It's cool that this plugin detects and auto-fixes comments that exceed the max line length, but it would also be cool if it was able to detect and auto-fix comments that wrap too early.

For example, this:

// This is
// the comment.

Should be this, because it fits within the max line length of 80 characters:

// This is the comment.

This might be a harder problem to solve without annoying false positives, but the VS Code extension "Rewrap" manages to achieve this pretty well so maybe inspiration can be glimpsed from it's implementation:

https://github.com/stkb/Rewrap

lasselupe33 commented 1 year ago

Hi @jaydenseric,

Personally I prefer that reflows are only performed "downwards" on overflow, as I often prefer to keep the first line as a 'heading' or similar, e.g.

// This is the primary message of my comment
// NB: This is additional context which should be below the first line

Compacting comments, as you mentioned, often leads to false positives where it is frustrating that lines are being merged into one when it actually was the intention to keep them separated. This is actually one of the primary reasons that I wrote this plugin. :)

However, I do know that many prefer that comments can be reflowed such that they take up the minimal required space, while keeping within the configured maximum limit.

As such I've introduced a new configuration option: mode, which is available from v1.1.0.

This value can be set to either overflow-only (default), compact-on-overflow and compact. e.g.

      "comment-length/limit-single-line-comments": [
        "warn",
        {
          mode: "compact-on-overflow",
        },
      ],
      "comment-length/limit-multi-line-comments": [
        "warn",
        {
          mode: "compact-on-overflow",
        },
      ],

overflow-only

When configured to this setting the wrapping will function as it has done before today, i.e. comments will not be "pulled" up.

As such the comment above would not be transformed.

compact

This mode will allow the automatic fix to function as you desire. In other words, then the comment above would be transformed into the following:

// This is the primary message of my comment NB: This is additional context
// which should be below the first line

compact-on-overflow

Finally, the last mode will perform as the compact mode, however automatic fixes will only be triggered when at least one line in a logical block has overflow.

I believe that this mode brings the best of both worlds, for those that enjoy comments being compacted, as you easily can avoid annoying false positives by fixing the overflow manually.

jaydenseric commented 1 year ago

Fantastic! Looking forward to trying this out these new modes on our projects when I get back to work tomorrow!

It has the potential to significantly improve quality of life when refactoring code that causes prettier to re-indent large blocks of code; previously I was manually searching in changed files for every comment that wraps too early to manually tidy them all up. If you cut some deeply indented code that only has < 40 columns to wrap within the 80 line length, and paste it into it's own module suddenly you have an 4 line comment that could be tidied to just 2 lines and be much more readable.

jaydenseric commented 1 year ago

So, the mode compact-on-overflow doesn't seem to trigger any errors, but compact does:

Screenshot 2023-06-05 at 8 37 35 am Screenshot 2023-06-05 at 8 37 59 am

I have VS Code configured to show a vertical ruler at the 80 char line length.

Is compact-on-overflow not working as expected?

lasselupe33 commented 1 year ago

That is actually the way compact-on-overlow is supposed to work. It only triggers an error when at least one of the lines is overflowing.

So compact attempts to keep comments as short as possible, and will always trigger an error if a comment block can be shortened, whereas compact-on-overflow uses the same strategy in its autofix (i.e. making a comment as compact as possible), but only shows an error if at least one line of a block overflows.

So, to me it sounds like you're actually interested in using the compact mode and not compact-on-overflow.

At least, the functionality is supposed to be as described above. Are you experiencing otherwise?

jaydenseric commented 12 months ago

Sorry that I haven't got back to you on this; I still haven't had time to revisit it.