space-unary-ops

在一元运算符之前或之后强制执行一致的间距

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行选项自动修复

此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js 中使用 相应的规则

¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js.

一些风格指南要求或不允许在一元运算符之前或之后有空格。这主要是一个风格问题,然而,一些 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, yield

    ¥words - applies to unary word operators such as: new, delete, typeof, void, yield

  • nonwords - 适用于一元运算符,例如:-, +, --, ++, !, !!

    ¥nonwords - applies to unary operators such as: -, +, --, ++, !, !!

  • overrides - 指定覆盖每个运算符、单词或非单词的间距用法。默认情况下为空,但可用于强制或禁止运算符周围的间距。例如:

    ¥overrides - specifies overwriting usage of spacing for each operator, word or non word. This is empty by default, but can be used to enforce or disallow spacing around operators. For example:

    "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 中引入。

资源

ESLint 中文网
粤ICP备13048890号