]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/max-len.md
7aa755a34b2d542b7a3ad4b7f11096f91324a156
[pve-eslint.git] / eslint / docs / rules / max-len.md
1 # enforce a maximum line length (max-len)
2
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).
4
5 ```js
6 var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" }; // very long
7 ```
8
9 ## Rule Details
10
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.
12
13 ## Options
14
15 This rule has a number or object option:
16
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
27
28 ### code
29
30 Examples of **incorrect** code for this rule with the default `{ "code": 80 }` option:
31
32 ```js
33 /*eslint max-len: ["error", { "code": 80 }]*/
34
35 var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" };
36 ```
37
38 Examples of **correct** code for this rule with the default `{ "code": 80 }` option:
39
40 ```js
41 /*eslint max-len: ["error", { "code": 80 }]*/
42
43 var foo = {
44 "bar": "This is a bar.",
45 "baz": { "qux": "This is a qux" },
46 "easier": "to read"
47 };
48 ```
49
50 ### tabWidth
51
52 Examples of **incorrect** code for this rule with the default `{ "tabWidth": 4 }` option:
53
54 ```js
55 /*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/
56
57 \t \t var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" } };
58 ```
59
60 Examples of **correct** code for this rule with the default `{ "tabWidth": 4 }` option:
61
62 ```js
63 /*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/
64
65 \t \t var foo = {
66 \t \t \t \t "bar": "This is a bar.",
67 \t \t \t \t "baz": { "qux": "This is a qux" }
68 \t \t };
69 ```
70
71 ### comments
72
73 Examples of **incorrect** code for this rule with the `{ "comments": 65 }` option:
74
75 ```js
76 /*eslint max-len: ["error", { "comments": 65 }]*/
77
78 /**
79 * This is a comment that violates the maximum line length we have specified
80 **/
81 ```
82
83 ### ignoreComments
84
85 Examples of **correct** code for this rule with the `{ "ignoreComments": true }` option:
86
87 ```js
88 /*eslint max-len: ["error", { "ignoreComments": true }]*/
89
90 /**
91 * This is a really really really really really really really really really long comment
92 **/
93 ```
94
95 ### ignoreTrailingComments
96
97 Examples of **correct** code for this rule with the `{ "ignoreTrailingComments": true }` option:
98
99 ```js
100 /*eslint max-len: ["error", { "ignoreTrailingComments": true }]*/
101
102 var foo = 'bar'; // This is a really really really really really really really long comment
103 ```
104
105 ### ignoreUrls
106
107 Examples of **correct** code for this rule with the `{ "ignoreUrls": true }` option:
108
109 ```js
110 /*eslint max-len: ["error", { "ignoreUrls": true }]*/
111
112 var url = 'https://www.example.com/really/really/really/really/really/really/really/long';
113 ```
114
115 ### ignoreStrings
116
117 Examples of **correct** code for this rule with the `{ "ignoreStrings": true }` option:
118
119 ```js
120 /*eslint max-len: ["error", { "ignoreStrings": true }]*/
121
122 var longString = 'this is a really really really really really long string!';
123 ```
124
125 ### ignoreTemplateLiterals
126
127 Examples of **correct** code for this rule with the `{ "ignoreTemplateLiterals": true }` option:
128
129 ```js
130 /*eslint max-len: ["error", { "ignoreTemplateLiterals": true }]*/
131
132 var longTemplateLiteral = `this is a really really really really really long template literal!`;
133 ```
134
135 ### ignoreRegExpLiterals
136
137 Examples of **correct** code for this rule with the `{ "ignoreRegExpLiterals": true }` option:
138
139 ```js
140 /*eslint max-len: ["error", { "ignoreRegExpLiterals": true }]*/
141
142 var longRegExpLiteral = /this is a really really really really really long regular expression!/;
143 ```
144
145 ### ignorePattern
146
147 Examples of **correct** code for this rule with the `ignorePattern` option:
148
149 ```js
150 /*eslint max-len: ["error", { "ignorePattern": "^\\s*var\\s.+=\\s*require\\s*\\(" }]*/
151
152 var dep = require('really/really/really/really/really/really/really/really/long/module');
153 ```
154
155 ## Related Rules
156
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)