1 # enforce a maximum line length (max-len)
3 Very long lines of code in any language can be difficult to read. In order to aid in readability and maintainability many coders have developed a convention to limit lines of code to X number of characters (traditionally 80 characters).
6 var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" }; // very long
11 This rule enforces a maximum line length to increase code readability and maintainability. The length of a line is defined as the number of Unicode characters in the line.
15 This rule has a number or object option:
17 * `"code"` (default `80`) enforces a maximum line length
18 * `"tabWidth"` (default `4`) specifies the character width for tab characters
19 * `"comments"` enforces a maximum line length for comments; defaults to value of `code`
20 * `"ignorePattern"` ignores lines matching a regular expression; can only match a single line and need to be double escaped when written in YAML or JSON
21 * `"ignoreComments": true` ignores all trailing comments and comments on their own line
22 * `"ignoreTrailingComments": true` ignores only trailing comments
23 * `"ignoreUrls": true` ignores lines that contain a URL
24 * `"ignoreStrings": true` ignores lines that contain a double-quoted or single-quoted string
25 * `"ignoreTemplateLiterals": true` ignores lines that contain a template literal
26 * `"ignoreRegExpLiterals": true` ignores lines that contain a RegExp literal
30 Examples of **incorrect** code for this rule with the default `{ "code": 80 }` option:
33 /*eslint max-len: ["error", { "code": 80 }]*/
35 var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" };
38 Examples of **correct** code for this rule with the default `{ "code": 80 }` option:
41 /*eslint max-len: ["error", { "code": 80 }]*/
44 "bar": "This is a bar.",
45 "baz": { "qux": "This is a qux" },
52 Examples of **incorrect** code for this rule with the default `{ "tabWidth": 4 }` option:
55 /*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/
57 \t \t var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" } };
60 Examples of **correct** code for this rule with the default `{ "tabWidth": 4 }` option:
63 /*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/
66 \t \t \t \t "bar": "This is a bar.",
67 \t \t \t \t "baz": { "qux": "This is a qux" }
73 Examples of **incorrect** code for this rule with the `{ "comments": 65 }` option:
76 /*eslint max-len: ["error", { "comments": 65 }]*/
79 * This is a comment that violates the maximum line length we have specified
85 Examples of **correct** code for this rule with the `{ "ignoreComments": true }` option:
88 /*eslint max-len: ["error", { "ignoreComments": true }]*/
91 * This is a really really really really really really really really really long comment
95 ### ignoreTrailingComments
97 Examples of **correct** code for this rule with the `{ "ignoreTrailingComments": true }` option:
100 /*eslint max-len: ["error", { "ignoreTrailingComments": true }]*/
102 var foo = 'bar'; // This is a really really really really really really really long comment
107 Examples of **correct** code for this rule with the `{ "ignoreUrls": true }` option:
110 /*eslint max-len: ["error", { "ignoreUrls": true }]*/
112 var url = 'https://www.example.com/really/really/really/really/really/really/really/long';
117 Examples of **correct** code for this rule with the `{ "ignoreStrings": true }` option:
120 /*eslint max-len: ["error", { "ignoreStrings": true }]*/
122 var longString = 'this is a really really really really really long string!';
125 ### ignoreTemplateLiterals
127 Examples of **correct** code for this rule with the `{ "ignoreTemplateLiterals": true }` option:
130 /*eslint max-len: ["error", { "ignoreTemplateLiterals": true }]*/
132 var longTemplateLiteral = `this is a really really really really really long template literal!`;
135 ### ignoreRegExpLiterals
137 Examples of **correct** code for this rule with the `{ "ignoreRegExpLiterals": true }` option:
140 /*eslint max-len: ["error", { "ignoreRegExpLiterals": true }]*/
142 var longRegExpLiteral = /this is a really really really really really long regular expression!/;
147 Examples of **correct** code for this rule with the `ignorePattern` option:
150 /*eslint max-len: ["error", { "ignorePattern": "^\\s*var\\s.+=\\s*require\\s*\\(" }]*/
152 var dep = require('really/really/really/really/really/really/really/really/long/module');
157 * [complexity](complexity.md)
158 * [max-depth](max-depth.md)
159 * [max-nested-callbacks](max-nested-callbacks.md)
160 * [max-params](max-params.md)
161 * [max-statements](max-statements.md)