]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: space-unary-ops | |
8f9d1d4d DC |
3 | rule_type: layout |
4 | --- | |
5 | ||
6 | ||
eb39fafa DC |
7 | |
8 | 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. | |
9 | ||
10 | ## Rule Details | |
11 | ||
12 | This rule enforces consistency regarding the spaces after `words` unary operators and after/before `nonwords` unary operators. | |
13 | ||
6f036462 TL |
14 | 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. |
15 | ||
eb39fafa DC |
16 | Examples of unary `words` operators: |
17 | ||
18 | ```js | |
19 | // new | |
20 | var joe = new Person(); | |
21 | ||
22 | // delete | |
23 | var obj = { | |
24 | foo: 'bar' | |
25 | }; | |
26 | delete obj.foo; | |
27 | ||
28 | // typeof | |
29 | typeof {} // object | |
30 | ||
31 | // void | |
32 | void 0 // undefined | |
33 | ``` | |
34 | ||
35 | Examples of unary `nonwords` operators: | |
36 | ||
37 | ```js | |
38 | if ([1,2,3].indexOf(1) !== -1) {}; | |
39 | foo = --foo; | |
40 | bar = bar++; | |
41 | baz = !foo; | |
42 | qux = !!baz; | |
43 | ``` | |
44 | ||
45 | ## Options | |
46 | ||
47 | This 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 | ||
67 | In this case, spacing will be disallowed after a `new` operator and required before/after a `++` operator. | |
68 | ||
69 | Examples 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 | ||
76 | typeof!foo; | |
77 | ||
78 | void{foo:0}; | |
79 | ||
80 | new[foo][0]; | |
81 | ||
82 | delete(foo.bar); | |
83 | ||
84 | ++ foo; | |
85 | ||
86 | foo --; | |
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 | ||
101 | function *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 | ||
113 | async function foo() { | |
114 | await(bar); | |
115 | } | |
116 | ``` | |
117 | ||
8f9d1d4d DC |
118 | ::: |
119 | ||
eb39fafa DC |
120 | Examples 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. |
128 | typeof !foo; | |
129 | ||
130 | // Word unary operator "void" is followed by a whitespace. | |
131 | void {foo:0}; | |
eb39fafa DC |
132 | |
133 | // Word unary operator "new" is followed by a whitespace. | |
6f036462 | 134 | new [foo][0]; |
eb39fafa | 135 | |
6f036462 TL |
136 | // Word unary operator "delete" is followed by a whitespace. |
137 | delete (foo.bar); | |
eb39fafa DC |
138 | |
139 | // Unary operator "++" is not followed by whitespace. | |
140 | ++foo; | |
141 | ||
142 | // Unary operator "--" is not preceded by whitespace. | |
143 | foo--; | |
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 | ||
160 | function *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 | ||
172 | async function foo() { | |
173 | await (bar); | |
174 | } | |
175 | ``` | |
8f9d1d4d DC |
176 | |
177 | ::: |