space-unary-ops
在一元运算符之前或之后强制执行一致的间距
此规则报告的一些问题可通过 --fix 命令行 选项自动修复
This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.
一些风格指南要求或禁止在一元运算符前后加空格。这主要是一个风格问题,然而,一些 JavaScript 表达式可以不加空格书写,这会使阅读和维护变得更加困难。
🌐 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.
规则详情
🌐 Rule Details
此规则强制统一 words 一元运算符后以及 nonwords 一元运算符前/后的空格。
🌐 This rule enforces consistency regarding the spaces after words unary operators and after/before nonwords unary operators.
对于 words 操作符,该规则仅在语法上不需要空格时适用。例如,delete obj.foo 需要空格,因此不会被此规则考虑。等效的 delete(obj.foo) 有一个可选空格(delete (obj.foo)),因此该规则将适用于它。
🌐 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.
一元 words 运算符的例子:
🌐 Examples of unary words operators:
// new
var joe = new Person();
// delete
var obj = {
foo: 'bar'
};
delete obj.foo;
// typeof
typeof {} // object
// void
void 0 // undefined
一元 nonwords 运算符的例子:
🌐 Examples of unary nonwords operators:
if ([1,2,3].indexOf(1) !== -1) {};
foo = --foo;
bar = bar++;
baz = !foo;
qux = !!baz;
选项
🌐 Options
此规则有三个选项:
🌐 This rule has three options:
words- 适用于一元词操作符,如:new、delete、typeof、void、yieldnonwords- 适用于一元运算符,例如:-、+、--、++、!、!!overrides- 指定每个运算符、单词或非单词的空格使用情况。默认情况下为空,但可以用来强制或禁止运算符周围的空格。例如:
"space-unary-ops": [
2, {
"words": true,
"nonwords": false,
"overrides": {
"new": false,
"++": true
}
}]
在这种情况下,new 运算符后不允许有空格,而在 ++ 运算符前后则需要空格。
🌐 In this case, spacing will be disallowed after a new operator and required before/after a ++ operator.
使用默认 {"words": true, "nonwords": false} 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default {"words": true, "nonwords": false} option:
/*eslint space-unary-ops: "error"*/
typeof!foo;
void{foo:0};
new[foo][0];
delete(foo.bar);
++ foo;
foo --;
- foo;
+ "3";
/*eslint space-unary-ops: "error"*/
function *foo() {
yield(0)
}
/*eslint space-unary-ops: "error"*/
async function foo() {
await(bar);
}
使用 {"words": true, "nonwords": false} 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the {"words": true, "nonwords": false} option:
/*eslint space-unary-ops: "error"*/
// Word unary operator "typeof" is followed by a whitespace.
typeof !foo;
// Word unary operator "void" is followed by a whitespace.
void {foo:0};
// Word unary operator "new" is followed by a whitespace.
new [foo][0];
// Word unary operator "delete" is followed by a whitespace.
delete (foo.bar);
// Unary operator "++" is not followed by whitespace.
++foo;
// Unary operator "--" is not preceded by whitespace.
foo--;
// Unary operator "-" is not followed by whitespace.
-foo;
// Unary operator "+" is not followed by whitespace.
+"3";
/*eslint space-unary-ops: "error"*/
function *foo() {
yield (0)
}
/*eslint space-unary-ops: "error"*/
async function foo() {
await (bar);
}
版本
此规则是在 ESLint v0.10.0 中引入。