This plugin provides ESLint rules that limit the line length of your comments. Furthermore, an automatic fix is included such that you can save time manually formatting your comments. As such it is recommended to apply this rule every time a file is saved in order to avoid the hassle of manually formatting comments.

This project aims to ease the process of writing long comments where each line needs to be cropped to a specific line length. This is similar to the max-len
ESLint rule. The primary difference is that this plugin can automatically fix violations.
NB: There are several cases wherein the rules will not attempt to automatically format comments. This is to accomodate cases where it is not desired to break a comment into multiple lines. Examples include comments that:
- are not on their own lines.
- include
eslint-[enable|disable]-*
, stylelint-[enable|disable]-*
, tslint:[enable|disable]
. - are wrapped within backticks, as it normally indicate code-snippets within comments. (e.g. ```some-comment```)
- are part of JSDoc-like comments. (i.e. multi-line comment lines that starts with '@')
Specific cases will be expanded upon in the example
sections below.

Installation
Yarn:
yarn add --dev eslint-plugin-comment-length
NPM:
npm i --save-dev eslint-plugin-comment-length
Usage
Add the following to your .eslint.config.js
configuration:
import eslintPluginCommentLength from "eslint-plugin-comment-length";
const eslintConfig = [
eslintPluginCommentLength.configs["flat/recommended"],
];
export default eslintConfig;
OR add the following to your .eslintrc
configuration:
{
"extends": ["plugin:comment-length/recommended"]
}
Tagged Template Literals

In case you want to detect and fix overflow of JavaScript comments within tagged template literals
, e.g. when using CSS-in-JS, you need to add the following rule to your configuration object:
{
"rules": {
"comment-length/limit-tagged-template-literal-comments": [
"warn",
{
"tags": ["css"], // will perform comment analysis on all tagged template literals named `css`
},
],
},
}
See comment-length/limit-tagged-template-literal-comments for additional information.
Configuration
Both rules accept the same set of configuration options, as described by the
provided TypeScript type declaration.
Please refer to the individual rules for examples of how to apply these
configurations in your ESLint config.
type Options = {
mode: "overflow-only" | "compact-on-overflow" | "compact";
maxLength: number;
logicalWrap: boolean;
tabSize: number;
ignoreUrls: boolean;
ignoreCommentsWithCode: boolean;
};
Rules
Locates single-line commments, i.e. // comment
, and ensures that each line never exceeds the configured length.
If a line violates this rule, the auto-fixer will attempt to combine logical groups of single-line comments and reformat those to ensure that each line is below the configured max length.
Which will be transformed into:
Options
{
"comment-length/limit-single-line-comments": [
"warn",
{
"mode": "overflow-only" | "compact-on-overflow" | "compact",
"maxLength": 80,
"logicalWrap": true,
"ignoreUrls": true,
"ignoreCommentsWithCode": true,
"tabSize": 2
}
]
}
Examples
Basic
Will be fixed into:
Indentation
When fixing this comment-block, then the leading whitespace on the following line will currently be discarded. This may change in the future.
As an example:
Will be fixed into:
The comments below will NOT be automatically fixable (as this will break functionality).
Must be on own line
The comment below will NOT be automatically fixable (as it is not on its own line).
This has been done as there is rarely much space available when a comment shares a line with actual code.
const myVariable = Math.max(0, Math.min(1, window.someValue));
The comment below will NOT be automatically fixable as it includes a comment within itself.
The rationale behind this decision is that developers at times will have to out-comment snippets of code when debugging. When this happens comments may also be commented out (perhaps accidentally). In this case we would like to preserve the structure of the original comment, such that when it is commented back in it follows the original layout.
Includes a code snippet
The comment below will NOT be automatically fixable as it includes a comment within itself.
The rationaly is essentially the same as above. In particular we wish to avoid breaking lines when code is out-commented during debugging.
Locates multi-line comments, i.e. /* comment */
and ensures that each line in the comment never exceeds the configured length.
If a line violates this rule, the auto-fixer will attempt to combine logical groups of lines inside the comment and reformat those to ensure that each line is below the configured max length.
As an example the comment below, which combines several comments from the ESLint
source code, is perfectly valid:
But the following would be considered as a violation:
Which will be transformed into the snippet below when applying the automatic fix:
Options
{
"comment-length/limit-multi-line-comments": [
"warn",
{
"mode": "overflow-only" | "compact-on-overflow" | "compact",
"maxLength": 80,
"logicalWrap": true,
"ignoreUrls": true,
"ignoreCommentsWithCode": true,
"tabSize": 2
}
]
}
Examples
Basic
JSDoc
In order to preserve semantics of JSDoc-like comments the automatic fix will not apply to lines that seems to be part of a JSDoc comment (i.e. starting with the character "@").
At times, when JSDoc declarations (e.g. @example) span multiple lines, then it may be desired to combine with the backtick
escape-hatch described below.
Backticks
Backticks inside a multi-line comment acts as an escape hatch for the automatic fix. In other words, all content within backticks will never be considered as a block that can be automatically fixed.
Indentation
When capturing logical blocks within a multi-line comment the rule will consider indentation levels. If two lines do not share the same indentation level, then they will never be considered as part of the same block.
This is illustrated with the following example:
Will be fixed into:
Single-line
Will be fixed into:
The comments below will NOT be automatically fixable (as this will break functionality).
The comment below will NOT be automatically fixable as it includes a comment within itself.
The rationale behind this decision is that developers at times will have to out-comment snippets of code when debugging. When this happens comments may also be commented out (perhaps accidentally). In this case we would like to preserve the structure of the original comment, such that when it is commented back in it follows the original layout.
Includes a code snippet
The comment below will NOT be automatically fixable as it includes a comment within itself.
The rationaly is essentially the same as above. In particular we wish to avoid breaking lines when code is out-commented during debugging.
Locates javascript comments, i.e. /* comment */
or // comment
within tagged template literals
and ensures that each line in the comment never exceeds the configured length.
If a line violates this rule, the auto-fixer will attempt to combine logical groups of lines inside the comment and reformat those to ensure that each line is below the configured max length.
NB This rule is a basic wrapper on top of the rules comment-length/limit-single-line-comments
and comment-length/limit-multi-line-comments
that parses comments within tagged template literals and then forwards these comments to the other rules.
This rule is especially intended for CSS-in-JS
wherein comments may be used. As an example consider the snippet below:
const myCss = css`
color: green;
`;
Normally the comment within the tagged template literal will not be automatically fixed. However, when this rule is included, then the snippet above will be transformed into the following:
const myCss = css`
color: green;
`;
Options
{
"comment-length/limit-multi-line-comments": [
"warn",
{
"tags": ["css"], // include names of all tags that comment detection should be performed on.
// the configurations below will be propagated to the other rules that this rule extends.
"mode": "overflow-only" | "compact-on-overflow" | "compact",
"maxLength": 80,
"logicalWrap": true,
"ignoreUrls": true,
"ignoreCommentsWithCode": true,
"tabSize": 2
}
]
}