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