]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/max-len.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / max-len.md
1 ---
2 title: max-len
3 layout: doc
4 rule_type: layout
5 related_rules:
6 - complexity
7 - max-depth
8 - max-nested-callbacks
9 - max-params
10 - max-statements
11 ---
12
13
14 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).
15
16 ```js
17 var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" }; // very long
18 ```
19
20 ## Rule Details
21
22 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.
23
24 ## Options
25
26 This rule has a number or object option:
27
28 * `"code"` (default `80`) enforces a maximum line length
29 * `"tabWidth"` (default `4`) specifies the character width for tab characters
30 * `"comments"` enforces a maximum line length for comments; defaults to value of `code`
31 * `"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
32 * `"ignoreComments": true` ignores all trailing comments and comments on their own line
33 * `"ignoreTrailingComments": true` ignores only trailing comments
34 * `"ignoreUrls": true` ignores lines that contain a URL
35 * `"ignoreStrings": true` ignores lines that contain a double-quoted or single-quoted string
36 * `"ignoreTemplateLiterals": true` ignores lines that contain a template literal
37 * `"ignoreRegExpLiterals": true` ignores lines that contain a RegExp literal
38
39 ### code
40
41 Examples of **incorrect** code for this rule with the default `{ "code": 80 }` option:
42
43 ::: incorrect
44
45 ```js
46 /*eslint max-len: ["error", { "code": 80 }]*/
47
48 var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" };
49 ```
50
51 :::
52
53 Examples of **correct** code for this rule with the default `{ "code": 80 }` option:
54
55 ::: correct
56
57 ```js
58 /*eslint max-len: ["error", { "code": 80 }]*/
59
60 var foo = {
61 "bar": "This is a bar.",
62 "baz": { "qux": "This is a qux" },
63 "easier": "to read"
64 };
65 ```
66
67 :::
68
69 ### tabWidth
70
71 Examples of **incorrect** code for this rule with the default `{ "tabWidth": 4 }` option:
72
73 ::: incorrect
74
75 ```js
76 /*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/
77
78 \t \t var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" } };
79 ```
80
81 :::
82
83 Examples of **correct** code for this rule with the default `{ "tabWidth": 4 }` option:
84
85 ::: correct
86
87 ```js
88 /*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/
89
90 \t \t var foo = {
91 \t \t \t \t "bar": "This is a bar.",
92 \t \t \t \t "baz": { "qux": "This is a qux" }
93 \t \t };
94 ```
95
96 :::
97
98 ### comments
99
100 Examples of **incorrect** code for this rule with the `{ "comments": 65 }` option:
101
102 ::: incorrect
103
104 ```js
105 /*eslint max-len: ["error", { "comments": 65 }]*/
106
107 /**
108 * This is a comment that violates the maximum line length we have specified
109 **/
110 ```
111
112 :::
113
114 ### ignoreComments
115
116 Examples of **correct** code for this rule with the `{ "ignoreComments": true }` option:
117
118 ::: correct
119
120 ```js
121 /*eslint max-len: ["error", { "ignoreComments": true }]*/
122
123 /**
124 * This is a really really really really really really really really really long comment
125 **/
126 ```
127
128 :::
129
130 ### ignoreTrailingComments
131
132 Examples of **correct** code for this rule with the `{ "ignoreTrailingComments": true }` option:
133
134 ::: correct
135
136 ```js
137 /*eslint max-len: ["error", { "ignoreTrailingComments": true }]*/
138
139 var foo = 'bar'; // This is a really really really really really really really long comment
140 ```
141
142 :::
143
144 ### ignoreUrls
145
146 Examples of **correct** code for this rule with the `{ "ignoreUrls": true }` option:
147
148 ::: correct
149
150 ```js
151 /*eslint max-len: ["error", { "ignoreUrls": true }]*/
152
153 var url = 'https://www.example.com/really/really/really/really/really/really/really/long';
154 ```
155
156 :::
157
158 ### ignoreStrings
159
160 Examples of **correct** code for this rule with the `{ "ignoreStrings": true }` option:
161
162 ::: correct
163
164 ```js
165 /*eslint max-len: ["error", { "ignoreStrings": true }]*/
166
167 var longString = 'this is a really really really really really long string!';
168 ```
169
170 :::
171
172 ### ignoreTemplateLiterals
173
174 Examples of **correct** code for this rule with the `{ "ignoreTemplateLiterals": true }` option:
175
176 ::: correct
177
178 ```js
179 /*eslint max-len: ["error", { "ignoreTemplateLiterals": true }]*/
180
181 var longTemplateLiteral = `this is a really really really really really long template literal!`;
182 ```
183
184 :::
185
186 ### ignoreRegExpLiterals
187
188 Examples of **correct** code for this rule with the `{ "ignoreRegExpLiterals": true }` option:
189
190 ::: correct
191
192 ```js
193 /*eslint max-len: ["error", { "ignoreRegExpLiterals": true }]*/
194
195 var longRegExpLiteral = /this is a really really really really really long regular expression!/;
196 ```
197
198 :::
199
200 ### ignorePattern
201
202 Examples of **correct** code for this rule with the `ignorePattern` option:
203
204 ::: correct
205
206 ```js
207 /*eslint max-len: ["error", { "ignorePattern": "^\\s*var\\s.+=\\s*require\\s*\\(" }]*/
208
209 var dep = require('really/really/really/really/really/really/really/really/long/module');
210 ```
211
212 :::