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