]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/space-unary-ops.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / space-unary-ops.md
1 ---
2 title: space-unary-ops
3 layout: doc
4 rule_type: layout
5 ---
6
7
8
9 Some style guides require or disallow spaces before or after unary operators. This is mainly a stylistic issue, however, some JavaScript expressions can be written without spacing which makes it harder to read and maintain.
10
11 ## Rule Details
12
13 This rule enforces consistency regarding the spaces after `words` unary operators and after/before `nonwords` unary operators.
14
15 For `words` operators, this rule only applies when a space is not syntactically required. For instance, `delete obj.foo` requires the space and will not be considered by this rule. The equivalent `delete(obj.foo)` has an optional space (`delete (obj.foo)`), therefore this rule will apply to it.
16
17 Examples of unary `words` operators:
18
19 ```js
20 // new
21 var joe = new Person();
22
23 // delete
24 var obj = {
25 foo: 'bar'
26 };
27 delete obj.foo;
28
29 // typeof
30 typeof {} // object
31
32 // void
33 void 0 // undefined
34 ```
35
36 Examples of unary `nonwords` operators:
37
38 ```js
39 if ([1,2,3].indexOf(1) !== -1) {};
40 foo = --foo;
41 bar = bar++;
42 baz = !foo;
43 qux = !!baz;
44 ```
45
46 ## Options
47
48 This rule has three options:
49
50 * `words` - applies to unary word operators such as: `new`, `delete`, `typeof`, `void`, `yield`
51 * `nonwords` - applies to unary operators such as: `-`, `+`, `--`, `++`, `!`, `!!`
52 * `overrides` - specifies overwriting usage of spacing for each
53 operator, word or non word. This is empty by default, but can be used
54 to enforce or disallow spacing around operators. For example:
55
56 ```js
57 "space-unary-ops": [
58 2, {
59 "words": true,
60 "nonwords": false,
61 "overrides": {
62 "new": false,
63 "++": true
64 }
65 }]
66 ```
67
68 In this case, spacing will be disallowed after a `new` operator and required before/after a `++` operator.
69
70 Examples of **incorrect** code for this rule with the default `{"words": true, "nonwords": false}` option:
71
72 ::: incorrect
73
74 ```js
75 /*eslint space-unary-ops: "error"*/
76
77 typeof!foo;
78
79 void{foo:0};
80
81 new[foo][0];
82
83 delete(foo.bar);
84
85 ++ foo;
86
87 foo --;
88
89 - foo;
90
91 + "3";
92 ```
93
94 :::
95
96 ::: incorrect
97
98 ```js
99 /*eslint space-unary-ops: "error"*/
100 /*eslint-env es6*/
101
102 function *foo() {
103 yield(0)
104 }
105 ```
106
107 :::
108
109 ::: incorrect
110
111 ```js
112 /*eslint space-unary-ops: "error"*/
113
114 async function foo() {
115 await(bar);
116 }
117 ```
118
119 :::
120
121 Examples of **correct** code for this rule with the `{"words": true, "nonwords": false}` option:
122
123 ::: correct
124
125 ```js
126 /*eslint space-unary-ops: "error"*/
127
128 // Word unary operator "typeof" is followed by a whitespace.
129 typeof !foo;
130
131 // Word unary operator "void" is followed by a whitespace.
132 void {foo:0};
133
134 // Word unary operator "new" is followed by a whitespace.
135 new [foo][0];
136
137 // Word unary operator "delete" is followed by a whitespace.
138 delete (foo.bar);
139
140 // Unary operator "++" is not followed by whitespace.
141 ++foo;
142
143 // Unary operator "--" is not preceded by whitespace.
144 foo--;
145
146 // Unary operator "-" is not followed by whitespace.
147 -foo;
148
149 // Unary operator "+" is not followed by whitespace.
150 +"3";
151 ```
152
153 :::
154
155 ::: correct
156
157 ```js
158 /*eslint space-unary-ops: "error"*/
159 /*eslint-env es6*/
160
161 function *foo() {
162 yield (0)
163 }
164 ```
165
166 :::
167
168 ::: correct
169
170 ```js
171 /*eslint space-unary-ops: "error"*/
172
173 async function foo() {
174 await (bar);
175 }
176 ```
177
178 :::