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