0
# Line Breaks and Newlines
1
2
Rules that control line breaks, empty lines, and multiline formatting for better code organization and readability.
3
4
## eol-last
5
6
Requires or disallows newline at the end of files.
7
8
```javascript { .api }
9
const eolLast: Rule.RuleModule;
10
11
// Rule options
12
type EolLastOptions = 'always' | 'never' | 'unix' | 'windows';
13
```
14
15
**Usage Examples:**
16
17
```javascript
18
// ✓ Good with "always" (default)
19
const name = 'John';
20
const age = 30;
21
↵ // newline at end of file
22
23
// ✓ Good with "never"
24
const name = 'John';
25
const age = 30; // no newline at end
26
27
// ✓ Good with "unix"
28
const name = 'John';↵ // LF line ending
29
30
// ✓ Good with "windows"
31
const name = 'John';↵↩ // CRLF line ending
32
```
33
34
**Configuration:**
35
36
```javascript
37
rules: {
38
'@stylistic/js/eol-last': ['error', 'always']
39
}
40
```
41
42
## no-multiple-empty-lines
43
44
Disallows multiple empty lines.
45
46
```javascript { .api }
47
const noMultipleEmptyLines: Rule.RuleModule;
48
49
// Rule options
50
type NoMultipleEmptyLinesOptions = {
51
max: number;
52
maxEOF?: number;
53
maxBOF?: number;
54
};
55
```
56
57
**Usage Examples:**
58
59
```javascript
60
// ✓ Good with { max: 1 }
61
const first = 'value';
62
63
const second = 'value'; // one empty line OK
64
65
// ✗ Bad with { max: 1 }
66
const first = 'value';
67
68
69
const second = 'value'; // two empty lines not allowed
70
71
// ✓ Good with { max: 2, maxEOF: 0 }
72
const code = 'here';
73
74
75
const more = 'content'; // up to 2 empty lines in code
76
77
// end of file with no empty lines (maxEOF: 0)
78
```
79
80
**Configuration:**
81
82
```javascript
83
rules: {
84
'@stylistic/js/no-multiple-empty-lines': ['error', { max: 1, maxEOF: 1 }]
85
}
86
```
87
88
## padding-line-between-statements
89
90
Requires or disallows padding lines between statements.
91
92
```javascript { .api }
93
const paddingLineBetweenStatements: Rule.RuleModule;
94
95
// Rule options
96
type PaddingLineBetweenStatementsOptions = Array<{
97
blankLine: 'always' | 'never' | 'any';
98
prev: StatementType | StatementType[];
99
next: StatementType | StatementType[];
100
}>;
101
102
type StatementType =
103
| 'block-like'
104
| 'exports'
105
| 'require'
106
| 'directive'
107
| 'expression'
108
| 'iife'
109
| 'multiline-block-like'
110
| 'multiline-expression'
111
| 'multiline-const'
112
| 'multiline-let'
113
| 'multiline-var'
114
| 'singleline-const'
115
| 'singleline-let'
116
| 'singleline-var'
117
| 'const'
118
| 'let'
119
| 'var'
120
| 'any'
121
| '*';
122
```
123
124
**Usage Examples:**
125
126
```javascript
127
// ✓ Good with padding between var declarations and other statements
128
const name = 'John';
129
const age = 30;
130
131
if (condition) {
132
doSomething();
133
}
134
135
return result;
136
137
// ✓ Good with no padding between similar statement types
138
const first = 'value';
139
const second = 'value';
140
const third = 'value';
141
```
142
143
**Configuration:**
144
145
```javascript
146
rules: {
147
'@stylistic/js/padding-line-between-statements': ['error',
148
{ blankLine: 'always', prev: ['const', 'let', 'var'], next: '*' },
149
{ blankLine: 'any', prev: ['const', 'let', 'var'], next: ['const', 'let', 'var'] }
150
]
151
}
152
```
153
154
## lines-around-comment
155
156
Requires empty lines around comments.
157
158
```javascript { .api }
159
const linesAroundComment: Rule.RuleModule;
160
161
// Rule options
162
type LinesAroundCommentOptions = {
163
beforeBlockComment?: boolean;
164
afterBlockComment?: boolean;
165
beforeLineComment?: boolean;
166
afterLineComment?: boolean;
167
allowBlockStart?: boolean;
168
allowBlockEnd?: boolean;
169
allowObjectStart?: boolean;
170
allowObjectEnd?: boolean;
171
allowArrayStart?: boolean;
172
allowArrayEnd?: boolean;
173
allowClassStart?: boolean;
174
allowClassEnd?: boolean;
175
applyDefaultIgnorePatterns?: boolean;
176
ignorePattern?: string;
177
};
178
```
179
180
**Usage Examples:**
181
182
```javascript
183
// ✓ Good with beforeBlockComment: true, afterBlockComment: true
184
const value = 'test';
185
186
/*
187
* Block comment
188
*/
189
190
const other = 'value';
191
192
// ✓ Good with beforeLineComment: true
193
const first = 'value';
194
195
// Line comment
196
const second = 'value';
197
198
// ✓ Good with allowBlockStart: true
199
function example() {
200
// Comment allowed at block start
201
const value = 'test';
202
}
203
```
204
205
**Configuration:**
206
207
```javascript
208
rules: {
209
'@stylistic/js/lines-around-comment': ['error', {
210
beforeBlockComment: true,
211
afterBlockComment: true,
212
beforeLineComment: true,
213
allowBlockStart: true,
214
allowObjectStart: true
215
}]
216
}
217
```
218
219
## lines-between-class-members
220
221
Requires or disallows an empty line between class members.
222
223
```javascript { .api }
224
const linesBetweenClassMembers: Rule.RuleModule;
225
226
// Rule options
227
type LinesBetweenClassMembersOptions =
228
| 'always'
229
| 'never'
230
| ['always' | 'never', {
231
exceptAfterSingleLine?: boolean;
232
}];
233
```
234
235
**Usage Examples:**
236
237
```javascript
238
// ✓ Good with "always" (default)
239
class Example {
240
constructor() {
241
this.value = 0;
242
}
243
244
method1() {
245
return this.value;
246
}
247
248
method2() {
249
return this.value * 2;
250
}
251
}
252
253
// ✓ Good with { exceptAfterSingleLine: true }
254
class Example {
255
value = 0; // single line
256
name = 'test'; // single line, no padding needed
257
258
method() { // padding required before multiline members
259
return this.value;
260
}
261
}
262
```
263
264
**Configuration:**
265
266
```javascript
267
rules: {
268
'@stylistic/js/lines-between-class-members': ['error', 'always', {
269
exceptAfterSingleLine: true
270
}]
271
}
272
```
273
274
## linebreak-style
275
276
Enforces consistent linebreak style.
277
278
```javascript { .api }
279
const linebreakStyle: Rule.RuleModule;
280
281
// Rule options
282
type LinebreakStyleOptions = 'unix' | 'windows';
283
```
284
285
**Usage Examples:**
286
287
```javascript
288
// ✓ Good with "unix" (LF)
289
const name = 'John';↵
290
const age = 30;↵
291
292
// ✓ Good with "windows" (CRLF)
293
const name = 'John';↵↩
294
const age = 30;↵↩
295
```
296
297
**Configuration:**
298
299
```javascript
300
rules: {
301
'@stylistic/js/linebreak-style': ['error', 'unix']
302
}
303
```
304
305
## newline-per-chained-call
306
307
Requires a newline after each call in a method chain.
308
309
```javascript { .api }
310
const newlinePerChainedCall: Rule.RuleModule;
311
312
// Rule options
313
type NewlinePerChainedCallOptions = {
314
ignoreChainWithDepth?: number;
315
};
316
```
317
318
**Usage Examples:**
319
320
```javascript
321
// ✓ Good with default settings
322
api
323
.get('/users')
324
.then(response => response.json())
325
.then(data => process(data))
326
.catch(handleError);
327
328
// ✓ Good with short chains
329
api.get('/users').then(process); // short chain OK
330
331
// ✓ Good with { ignoreChainWithDepth: 3 }
332
api.get('/users').then(process).catch(handleError); // under threshold
333
api
334
.get('/users')
335
.then(process)
336
.then(validate)
337
.catch(handleError); // meets threshold
338
```
339
340
**Configuration:**
341
342
```javascript
343
rules: {
344
'@stylistic/js/newline-per-chained-call': ['error', { ignoreChainWithDepth: 2 }]
345
}
346
```
347
348
## multiline-comment-style
349
350
Enforces a particular style for multiline comments.
351
352
```javascript { .api }
353
const multilineCommentStyle: Rule.RuleModule;
354
355
// Rule options
356
type MultilineCommentStyleOptions =
357
| 'starred-block'
358
| 'separate-lines'
359
| 'bare-block';
360
```
361
362
**Usage Examples:**
363
364
```javascript
365
// ✓ Good with "starred-block" (default)
366
/*
367
* This is a multiline comment
368
* with stars on each line
369
*/
370
371
// ✓ Good with "separate-lines"
372
// This is a multiline comment
373
// using separate line comments
374
375
// ✓ Good with "bare-block"
376
/*
377
This is a multiline comment
378
without stars on each line
379
*/
380
```
381
382
**Configuration:**
383
384
```javascript
385
rules: {
386
'@stylistic/js/multiline-comment-style': ['error', 'starred-block']
387
}
388
```
389
390
## spaced-comment
391
392
Enforces consistent spacing after the // or /* in a comment.
393
394
```javascript { .api }
395
const spacedComment: Rule.RuleModule;
396
397
// Rule options
398
type SpacedCommentOptions =
399
| 'always'
400
| 'never'
401
| ['always' | 'never', {
402
line?: {
403
markers?: string[];
404
exceptions?: string[];
405
};
406
block?: {
407
markers?: string[];
408
exceptions?: string[];
409
balanced?: boolean;
410
};
411
}];
412
```
413
414
**Usage Examples:**
415
416
```javascript
417
// ✓ Good with "always" (default)
418
// This is a line comment
419
/* This is a block comment */
420
421
// ✓ Good with "never"
422
//This is a line comment
423
/*This is a block comment*/
424
425
// ✓ Good with exceptions
426
//----- Section Header ----- (exception for dashes)
427
//TODO: Fix this (marker allowed)
428
```
429
430
**Configuration:**
431
432
```javascript
433
rules: {
434
'@stylistic/js/spaced-comment': ['error', 'always', {
435
markers: ['TODO', 'FIXME'],
436
exceptions: ['-', '=', '*']
437
}]
438
}
439
```
440
441
## padded-blocks
442
443
Requires or disallows padding within blocks.
444
445
```javascript { .api }
446
const paddedBlocks: Rule.RuleModule;
447
448
// Rule options
449
type PaddedBlocksOptions =
450
| 'always'
451
| 'never'
452
| {
453
blocks?: 'always' | 'never';
454
classes?: 'always' | 'never';
455
switches?: 'always' | 'never';
456
};
457
```
458
459
**Usage Examples:**
460
461
```javascript
462
// ✓ Good with "always"
463
function example() {
464
465
const value = 'test';
466
return value;
467
468
}
469
470
// ✓ Good with "never" (default)
471
function example() {
472
const value = 'test';
473
return value;
474
}
475
476
// ✓ Good with custom config
477
if (condition) {
478
479
doSomething(); // blocks: 'always'
480
481
}
482
483
class Example { // classes: 'never'
484
method() {
485
return true;
486
}
487
}
488
```
489
490
**Configuration:**
491
492
```javascript
493
rules: {
494
'@stylistic/js/padded-blocks': ['error', 'never']
495
}
496
```
497
498
## multiline-ternary
499
500
Enforces newlines between operands of ternary expressions.
501
502
```javascript { .api }
503
const multilineTernary: Rule.RuleModule;
504
505
// Rule options
506
type MultilineTernaryOptions = 'always' | 'always-multiline' | 'never';
507
```
508
509
**Usage Examples:**
510
511
```javascript
512
// ✓ Good with "always-multiline"
513
const result = condition ? 'yes' : 'no'; // single line OK
514
515
const result = longConditionExpression
516
? 'yes'
517
: 'no'; // multiline requires newlines
518
519
// ✓ Good with "always"
520
const result = condition
521
? 'yes'
522
: 'no';
523
524
// ✓ Good with "never"
525
const result = condition ? 'yes' : 'no';
526
const result = longConditionExpression ? 'yes' : 'no'; // no newlines
527
```
528
529
**Configuration:**
530
531
```javascript
532
rules: {
533
'@stylistic/js/multiline-ternary': ['error', 'always-multiline']
534
}
535
```
536
537
## Common Line Break Combinations
538
539
### Clean Code Style
540
```javascript
541
rules: {
542
'@stylistic/js/eol-last': ['error', 'always'],
543
'@stylistic/js/no-multiple-empty-lines': ['error', { max: 1, maxEOF: 0 }],
544
'@stylistic/js/padding-line-between-statements': ['error',
545
{ blankLine: 'always', prev: ['const', 'let', 'var'], next: '*' },
546
{ blankLine: 'any', prev: ['const', 'let', 'var'], next: ['const', 'let', 'var'] }
547
],
548
'@stylistic/js/lines-between-class-members': ['error', 'always']
549
}
550
```
551
552
### Compact Style
553
```javascript
554
rules: {
555
'@stylistic/js/eol-last': ['error', 'never'],
556
'@stylistic/js/no-multiple-empty-lines': ['error', { max: 0 }],
557
'@stylistic/js/lines-around-comment': ['error', {
558
beforeBlockComment: false,
559
beforeLineComment: false
560
}],
561
'@stylistic/js/lines-between-class-members': ['error', 'never']
562
}
563
```
564
565
## Type Definitions
566
567
```typescript { .api }
568
interface LineBreakRules {
569
'eol-last': Rule.RuleModule;
570
'no-multiple-empty-lines': Rule.RuleModule;
571
'padding-line-between-statements': Rule.RuleModule;
572
'lines-around-comment': Rule.RuleModule;
573
'lines-between-class-members': Rule.RuleModule;
574
'linebreak-style': Rule.RuleModule;
575
'newline-per-chained-call': Rule.RuleModule;
576
'multiline-ternary': Rule.RuleModule;
577
}
578
579
interface PaddingRule {
580
blankLine: 'always' | 'never' | 'any';
581
prev: StatementType | StatementType[];
582
next: StatementType | StatementType[];
583
}
584
585
interface LinesAroundCommentConfig {
586
beforeBlockComment?: boolean;
587
afterBlockComment?: boolean;
588
beforeLineComment?: boolean;
589
afterLineComment?: boolean;
590
allowBlockStart?: boolean;
591
allowBlockEnd?: boolean;
592
allowObjectStart?: boolean;
593
allowObjectEnd?: boolean;
594
allowArrayStart?: boolean;
595
allowArrayEnd?: boolean;
596
allowClassStart?: boolean;
597
allowClassEnd?: boolean;
598
applyDefaultIgnorePatterns?: boolean;
599
ignorePattern?: string;
600
}
601
```