]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Require or disallow spaces before/after unary operators (space-unary-ops) |
2 | ||
3 | 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. | |
4 | ||
5 | ## Rule Details | |
6 | ||
7 | This rule enforces consistency regarding the spaces after `words` unary operators and after/before `nonwords` unary operators. | |
8 | ||
9 | Examples of unary `words` operators: | |
10 | ||
11 | ```js | |
12 | // new | |
13 | var joe = new Person(); | |
14 | ||
15 | // delete | |
16 | var obj = { | |
17 | foo: 'bar' | |
18 | }; | |
19 | delete obj.foo; | |
20 | ||
21 | // typeof | |
22 | typeof {} // object | |
23 | ||
24 | // void | |
25 | void 0 // undefined | |
26 | ``` | |
27 | ||
28 | Examples of unary `nonwords` operators: | |
29 | ||
30 | ```js | |
31 | if ([1,2,3].indexOf(1) !== -1) {}; | |
32 | foo = --foo; | |
33 | bar = bar++; | |
34 | baz = !foo; | |
35 | qux = !!baz; | |
36 | ``` | |
37 | ||
38 | ## Options | |
39 | ||
40 | This 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 | ||
60 | In this case, spacing will be disallowed after a `new` operator and required before/after a `++` operator. | |
61 | ||
62 | Examples of **incorrect** code for this rule with the default `{"words": true, "nonwords": false}` option: | |
63 | ||
64 | ```js | |
65 | /*eslint space-unary-ops: "error"*/ | |
66 | ||
67 | typeof!foo; | |
68 | ||
69 | void{foo:0}; | |
70 | ||
71 | new[foo][0]; | |
72 | ||
73 | delete(foo.bar); | |
74 | ||
75 | ++ foo; | |
76 | ||
77 | foo --; | |
78 | ||
79 | - foo; | |
80 | ||
81 | + "3"; | |
82 | ``` | |
83 | ||
84 | ```js | |
85 | /*eslint space-unary-ops: "error"*/ | |
86 | /*eslint-env es6*/ | |
87 | ||
88 | function *foo() { | |
89 | yield(0) | |
90 | } | |
91 | ``` | |
92 | ||
93 | ```js | |
94 | /*eslint space-unary-ops: "error"*/ | |
95 | ||
96 | async function foo() { | |
97 | await(bar); | |
98 | } | |
99 | ``` | |
100 | ||
101 | Examples 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. | |
107 | delete foo.bar; | |
108 | ||
109 | // Word unary operator "new" is followed by a whitespace. | |
110 | new Foo; | |
111 | ||
112 | // Word unary operator "void" is followed by a whitespace. | |
113 | void 0; | |
114 | ||
115 | // Unary operator "++" is not followed by whitespace. | |
116 | ++foo; | |
117 | ||
118 | // Unary operator "--" is not preceded by whitespace. | |
119 | foo--; | |
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 | ||
132 | function *foo() { | |
133 | yield (0) | |
134 | } | |
135 | ``` | |
136 | ||
137 | ```js | |
138 | /*eslint space-unary-ops: "error"*/ | |
139 | ||
140 | async function foo() { | |
141 | await (bar); | |
142 | } | |
143 | ``` |