]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-inline-comments | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | --- | |
5 | ||
eb39fafa DC |
6 | |
7 | Some style guides disallow comments on the same line as code. Code can become difficult to read if comments immediately follow the code on the same line. | |
8 | On the other hand, it is sometimes faster and more obvious to put comments immediately following code. | |
9 | ||
10 | ## Rule Details | |
11 | ||
12 | This rule disallows comments on the same line as code. | |
13 | ||
14 | Examples of **incorrect** code for this rule: | |
15 | ||
8f9d1d4d DC |
16 | ::: incorrect |
17 | ||
eb39fafa DC |
18 | ```js |
19 | /*eslint no-inline-comments: "error"*/ | |
20 | ||
21 | var a = 1; // declaring a to 1 | |
22 | ||
23 | function getRandomNumber(){ | |
24 | return 4; // chosen by fair dice roll. | |
25 | // guaranteed to be random. | |
26 | } | |
27 | ||
28 | /* A block comment before code */ var b = 2; | |
29 | ||
30 | var c = 3; /* A block comment after code */ | |
31 | ``` | |
32 | ||
8f9d1d4d DC |
33 | ::: |
34 | ||
eb39fafa DC |
35 | Examples of **correct** code for this rule: |
36 | ||
8f9d1d4d DC |
37 | ::: correct |
38 | ||
eb39fafa DC |
39 | ```js |
40 | /*eslint no-inline-comments: "error"*/ | |
41 | ||
42 | // This is a comment above a line of code | |
43 | var foo = 5; | |
44 | ||
45 | var bar = 5; | |
46 | //This is a comment below a line of code | |
47 | ``` | |
48 | ||
8f9d1d4d DC |
49 | ::: |
50 | ||
eb39fafa DC |
51 | ### JSX exception |
52 | ||
53 | Comments inside the curly braces in JSX are allowed to be on the same line as the braces, but only if they are not on the same line with other code, and the braces do not enclose an actual expression. | |
54 | ||
55 | Examples of **incorrect** code for this rule: | |
56 | ||
8f9d1d4d DC |
57 | ::: incorrect |
58 | ||
eb39fafa DC |
59 | ```js |
60 | /*eslint no-inline-comments: "error"*/ | |
61 | ||
62 | var foo = <div>{ /* On the same line with other code */ }<h1>Some heading</h1></div>; | |
63 | ||
64 | var bar = ( | |
65 | <div> | |
66 | { // These braces are not just for the comment, so it can't be on the same line | |
67 | baz | |
68 | } | |
69 | </div> | |
70 | ); | |
71 | ``` | |
72 | ||
8f9d1d4d DC |
73 | ::: |
74 | ||
eb39fafa DC |
75 | Examples of **correct** code for this rule: |
76 | ||
8f9d1d4d DC |
77 | ::: correct |
78 | ||
eb39fafa DC |
79 | ```js |
80 | /*eslint no-inline-comments: "error"*/ | |
81 | ||
82 | var foo = ( | |
83 | <div> | |
84 | {/* These braces are just for this comment and there is nothing else on this line */} | |
85 | <h1>Some heading</h1> | |
86 | </div> | |
87 | ) | |
88 | ||
89 | var bar = ( | |
90 | <div> | |
91 | { | |
92 | // There is nothing else on this line | |
93 | baz | |
94 | } | |
95 | </div> | |
96 | ); | |
97 | ||
98 | var quux = ( | |
99 | <div> | |
100 | {/* | |
101 | Multiline | |
102 | comment | |
103 | */} | |
104 | <h1>Some heading</h1> | |
105 | </div> | |
106 | ) | |
107 | ``` | |
6f036462 | 108 | |
8f9d1d4d DC |
109 | ::: |
110 | ||
6f036462 TL |
111 | ## Options |
112 | ||
113 | ### ignorePattern | |
114 | ||
115 | To make this rule ignore specific comments, set the `ignorePattern` option to a string pattern that will be passed to the [`RegExp` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp). | |
116 | ||
117 | Examples of **correct** code for the `ignorePattern` option: | |
118 | ||
8f9d1d4d DC |
119 | ::: correct |
120 | ||
6f036462 TL |
121 | ```js |
122 | /*eslint no-inline-comments: ["error", { "ignorePattern": "webpackChunkName:\\s.+" }]*/ | |
123 | ||
124 | import(/* webpackChunkName: "my-chunk-name" */ './locale/en'); | |
125 | ``` | |
126 | ||
8f9d1d4d DC |
127 | ::: |
128 | ||
6f036462 TL |
129 | Examples of **incorrect** code for the `ignorePattern` option: |
130 | ||
8f9d1d4d DC |
131 | ::: incorrect |
132 | ||
6f036462 TL |
133 | ```js |
134 | /*eslint no-inline-comments: ["error", { "ignorePattern": "something" }] */ | |
135 | ||
136 | var foo = 4; // other thing | |
137 | ``` | |
8f9d1d4d DC |
138 | |
139 | ::: |