1 # Require or disallow spaces before/after unary operators (space-unary-ops)
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.
7 This rule enforces consistency regarding the spaces after `words` unary operators and after/before `nonwords` unary operators.
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.
11 Examples of unary `words` operators:
15 var joe = new Person();
30 Examples of unary `nonwords` operators:
33 if ([1,2,3].indexOf(1) !== -1) {};
42 This rule has three options:
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:
62 In this case, spacing will be disallowed after a `new` operator and required before/after a `++` operator.
64 Examples of **incorrect** code for this rule with the default `{"words": true, "nonwords": false}` option:
67 /*eslint space-unary-ops: "error"*/
87 /*eslint space-unary-ops: "error"*/
96 /*eslint space-unary-ops: "error"*/
98 async function foo() {
103 Examples of **correct** code for this rule with the `{"words": true, "nonwords": false}` option:
106 /*eslint space-unary-ops: "error"*/
108 // Word unary operator "typeof" is followed by a whitespace.
111 // Word unary operator "void" is followed by a whitespace.
114 // Word unary operator "new" is followed by a whitespace.
117 // Word unary operator "delete" is followed by a whitespace.
120 // Unary operator "++" is not followed by whitespace.
123 // Unary operator "--" is not preceded by whitespace.
126 // Unary operator "-" is not followed by whitespace.
129 // Unary operator "+" is not followed by whitespace.
134 /*eslint space-unary-ops: "error"*/
143 /*eslint space-unary-ops: "error"*/
145 async function foo() {