]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-irregular-whitespace.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-irregular-whitespace.md
1 ---
2 title: no-irregular-whitespace
3 layout: doc
4 rule_type: problem
5 further_reading:
6 - https://es5.github.io/#x7.2
7 - https://web.archive.org/web/20200414142829/http://timelessrepo.com/json-isnt-a-javascript-subset
8 ---
9
10
11
12 Invalid or irregular whitespace causes issues with ECMAScript 5 parsers and also makes code harder to debug in a similar nature to mixed tabs and spaces.
13
14 Various whitespace characters can be inputted by programmers by mistake for example from copying or keyboard shortcuts. Pressing Alt + Space on macOS adds in a non breaking space character for example.
15
16 A simple fix for this problem could be to rewrite the offending line from scratch. This might also be a problem introduced by the text editor: if rewriting the line does not fix it, try using a different editor.
17
18 Known issues these spaces cause:
19
20 * Zero Width Space
21 * Is NOT considered a separator for tokens and is often parsed as an `Unexpected token ILLEGAL`
22 * Is NOT shown in modern browsers making code repository software expected to resolve the visualization
23 * Line Separator
24 * Is NOT a valid character within JSON which would cause parse errors
25
26 ## Rule Details
27
28 This rule is aimed at catching invalid whitespace that is not a normal tab and space. Some of these characters may cause issues in modern browsers and others will be a debugging issue to spot.
29
30 This rule disallows the following characters except where the options allow:
31
32 ```text
33 \u000B - Line Tabulation (\v) - <VT>
34 \u000C - Form Feed (\f) - <FF>
35 \u00A0 - No-Break Space - <NBSP>
36 \u0085 - Next Line
37 \u1680 - Ogham Space Mark
38 \u180E - Mongolian Vowel Separator - <MVS>
39 \ufeff - Zero Width No-Break Space - <BOM>
40 \u2000 - En Quad
41 \u2001 - Em Quad
42 \u2002 - En Space - <ENSP>
43 \u2003 - Em Space - <EMSP>
44 \u2004 - Three-Per-Em
45 \u2005 - Four-Per-Em
46 \u2006 - Six-Per-Em
47 \u2007 - Figure Space
48 \u2008 - Punctuation Space - <PUNCSP>
49 \u2009 - Thin Space
50 \u200A - Hair Space
51 \u200B - Zero Width Space - <ZWSP>
52 \u2028 - Line Separator
53 \u2029 - Paragraph Separator
54 \u202F - Narrow No-Break Space
55 \u205f - Medium Mathematical Space
56 \u3000 - Ideographic Space
57 ```
58
59 ## Options
60
61 This rule has an object option for exceptions:
62
63 * `"skipStrings": true` (default) allows any whitespace characters in string literals
64 * `"skipComments": true` allows any whitespace characters in comments
65 * `"skipRegExps": true` allows any whitespace characters in regular expression literals
66 * `"skipTemplates": true` allows any whitespace characters in template literals
67
68 ### skipStrings
69
70 Examples of **incorrect** code for this rule with the default `{ "skipStrings": true }` option:
71
72 ::: incorrect
73
74 ```js
75 /*eslint no-irregular-whitespace: "error"*/
76
77 function thing() /*<NBSP>*/{
78 return 'test';
79 }
80
81 function thing( /*<NBSP>*/){
82 return 'test';
83 }
84
85 function thing /*<NBSP>*/(){
86 return 'test';
87 }
88
89 function thing᠎/*<MVS>*/(){
90 return 'test';
91 }
92
93 function thing() {
94 return 'test'; /*<ENSP>*/
95 }
96
97 function thing() {
98 return 'test'; /*<NBSP>*/
99 }
100
101 function thing() {
102 // Description <NBSP>: some descriptive text
103 }
104
105 /*
106 Description <NBSP>: some descriptive text
107 */
108
109 function thing() {
110 return / <NBSP>regexp/;
111 }
112
113 /*eslint-env es6*/
114 function thing() {
115 return `template <NBSP>string`;
116 }
117 ```
118
119 :::
120
121 Examples of **correct** code for this rule with the default `{ "skipStrings": true }` option:
122
123 ::: correct
124
125 ```js
126 /*eslint no-irregular-whitespace: "error"*/
127
128 function thing() {
129 return ' <NBSP>thing';
130 }
131
132 function thing() {
133 return '​<ZWSP>thing';
134 }
135
136 function thing() {
137 return 'th <NBSP>ing';
138 }
139 ```
140
141 :::
142
143 ### skipComments
144
145 Examples of additional **correct** code for this rule with the `{ "skipComments": true }` option:
146
147 ::: correct
148
149 ```js
150 /*eslint no-irregular-whitespace: ["error", { "skipComments": true }]*/
151
152 function thing() {
153 // Description <NBSP>: some descriptive text
154 }
155
156 /*
157 Description <NBSP>: some descriptive text
158 */
159 ```
160
161 :::
162
163 ### skipRegExps
164
165 Examples of additional **correct** code for this rule with the `{ "skipRegExps": true }` option:
166
167 ::: correct
168
169 ```js
170 /*eslint no-irregular-whitespace: ["error", { "skipRegExps": true }]*/
171
172 function thing() {
173 return / <NBSP>regexp/;
174 }
175 ```
176
177 :::
178
179 ### skipTemplates
180
181 Examples of additional **correct** code for this rule with the `{ "skipTemplates": true }` option:
182
183 ::: correct
184
185 ```js
186 /*eslint no-irregular-whitespace: ["error", { "skipTemplates": true }]*/
187 /*eslint-env es6*/
188
189 function thing() {
190 return `template <NBSP>string`;
191 }
192 ```
193
194 :::
195
196 ## When Not To Use It
197
198 If you decide that you wish to use whitespace other than tabs and spaces outside of strings in your application.