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