]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/linebreak-style.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / linebreak-style.md
1 ---
2 title: linebreak-style
3 layout: doc
4 rule_type: layout
5 ---
6
7
8
9 When developing with a lot of people all having different editors, VCS applications and operating systems it may occur that
10 different line endings are written by either of the mentioned (might especially happen when using the windows and mac versions of SourceTree together).
11
12 The linebreaks (new lines) used in windows operating system are usually _carriage returns_ (CR) followed by a _line feed_ (LF) making it a _carriage return line feed_ (CRLF)
13 whereas Linux and Unix use a simple _line feed_ (LF). The corresponding _control sequences_ are `"\n"` (for LF) and `"\r\n"` for (CRLF).
14
15 Many versioning systems (like git and subversion) can automatically ensure the correct ending. However to cover all contingencies, you can activate this rule.
16
17 ## Rule Details
18
19 This rule enforces consistent line endings independent of operating system, VCS, or editor used across your codebase.
20
21 ### Options
22
23 This rule has a string option:
24
25 * `"unix"` (default) enforces the usage of Unix line endings: `\n` for LF.
26 * `"windows"` enforces the usage of Windows line endings: `\r\n` for CRLF.
27
28 ### unix
29
30 Examples of **incorrect** code for this rule with the default `"unix"` option:
31
32 ::: incorrect
33
34 ```js
35 /*eslint linebreak-style: ["error", "unix"]*/
36
37 var a = 'a'; // \r\n
38
39 ```
40
41 :::
42
43 Examples of **correct** code for this rule with the default `"unix"` option:
44
45 ::: correct
46
47 ```js
48 /*eslint linebreak-style: ["error", "unix"]*/
49
50 var a = 'a', // \n
51 b = 'b'; // \n
52 // \n
53 function foo(params) { // \n
54 // do stuff \n
55 }// \n
56 ```
57
58 :::
59
60 ### windows
61
62 Examples of **incorrect** code for this rule with the `"windows"` option:
63
64 ::: incorrect
65
66 ```js
67 /*eslint linebreak-style: ["error", "windows"]*/
68
69 var a = 'a'; // \n
70 ```
71
72 :::
73
74 Examples of **correct** code for this rule with the `"windows"` option:
75
76 ::: correct
77
78 ```js
79 /*eslint linebreak-style: ["error", "windows"]*/
80
81 var a = 'a', // \r\n
82 b = 'b'; // \r\n
83 // \r\n
84 function foo(params) { // \r\n
85 // do stuff \r\n
86 } // \r\n
87 ```
88
89 :::
90
91 ### Using this rule with version control systems
92
93 Version control systems sometimes have special behavior for linebreaks. To make it easy for developers to contribute to your codebase from different platforms, you may want to configure your VCS to handle linebreaks appropriately.
94
95 For example, the default behavior of [git](https://git-scm.com/) on Windows systems is to convert LF linebreaks to CRLF when checking out files, but to store the linebreaks as LF when committing a change. This will cause the `linebreak-style` rule to report errors if configured with the `"unix"` setting, because the files that ESLint sees will have CRLF linebreaks. If you use git, you may want to add a line to your [`.gitattributes` file](https://git-scm.com/docs/gitattributes) to prevent git from converting linebreaks in `.js` files:
96
97 ```txt
98 *.js text eol=lf
99 ```
100
101 ## When Not To Use It
102
103 If you aren't concerned about having different line endings within your code, then you can safely turn this rule off.
104
105 ## Compatibility
106
107 * **JSCS**: [validateLineBreaks](https://jscs-dev.github.io/rule/validateLineBreaks)