space-unary-ops

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

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

一些样式指南要求或不允许在一元运算符之前或之后有空格。这主要是一个风格问题,然而,一些 JavaScript 表达式可以在没有空格的情况下编写,这使得它更难阅读和维护。

规则详情

此规则强制 words 一元运算符之后和 nonwords 一元运算符之后/之前的空格保持一致。

对于 words 运算符,此规则仅适用于语法上不需要空格的情况。例如,delete obj.foo 需要空格,此规则不会考虑。等效的 delete(obj.foo) 有一个可选空格 (delete (obj.foo)),因此该规则将适用于它。

JMc+J9w/yNXL1FOGWjFACaAL3MtN6sWQq8b+z7jVSONNPep5uCGMWoWtOMtosCha

// new
var joe = new Person();

// delete
var obj = {
    foo: 'bar'
};
delete obj.foo;

// typeof
typeof {} // object

// void
void 0 // undefined

ohiFREK63taCytuJVjKSEX3qXYPI93tjSK55Nmgkmb1mAB2CIE2IbgDhK03Mty+vDnuaNalIwUI+GDTb8McYNA==

if ([1,2,3].indexOf(1) !== -1) {};
foo = --foo;
bar = bar++;
baz = !foo;
qux = !!baz;

选项

wCmaKmMgM0iOMbgkvSbQQRY+7cFcKGbiFK1icPBKa2M=

    kumPxwIs3U1bE4fOuz0rl7EgtmylMVxwkFKonlAswi37o9FxkCuu0SZx0p6g5Qlh1D5alKRtcdwx204wF8khUnrB23xpwKl0FSfGfTec5iKhcgoebH1jh22fZQ0H7nbAPbBi8iGpdlTu22qHhTUHiVRvPmG2E57Rps2iE0rhbF1QZDdhH1YMhK4lBF0lsqdwvQt25JoWAXHh0jnAUCB/U27uoernJ0Vv2EfZUMqG9MJYyUf5EjQKO37nfNvhL7ZocbFseMq3jZWVcwWWRLchxp/dE2raw1SY9UX07zuFwPUeKOJUzduSS+jXwqYKLyYW7kpGSKXsqOWqzu9Wf5f0exbMAcRWDKWHq0jqkH4hlOapN5Wx3IjmWuHQbuWFzSVWB24Q1JNA/XgNzsfvmRajhD4exD56bkDl07X3zTnFLEBYWJqBZaksUJ+rq9GIA98ufvyiiJe435xNTAsJOua2vOpIJs6DC57q7gh1gn9HPKbxmuIYjXVodd51aC++2QJV/QaVuivZ74VKJLerqWfXz3GKsDqtSfJPMzu4/qm63AIZkzfANgf23duf4HPmf8Tq90s1UUbP8O8FKBjlNF+7CwPPxh6lZYnyTKYl2iNhGl7KhLWaFstM8ryNXrXRt4iDE96faU3d4o7DStIRgAZypEiehzT0idnvW6/q85yXrVX2mC1Kh/ag1qXLvNCJAJtuQ9q07y/fOlbrSh/zlJUumA==
    "space-unary-ops": [
        2, {
          "words": true,
          "nonwords": false,
          "overrides": {
            "new": false,
            "++": true
          }
    }]

kaJfdFYKNiNSB1qI8qY7BLimcPRlQYlXts8GRriT0cZpTspASq/EEkZHHXAcY+VqEA8MMx8Cqonmb1yPLCvQZe4s+EBN89gQF6aRkhGyTW6E+w+a2Cjvux654KhDZ8QlbwYNR0+U1WJuccAQbnH3QQ+u4OR9d6f8SZ87tetkTGjLL50J+x5XYL00RxSYN4va

1usau4ITNzPbSW1XK9odV1PLNZIrlFtGayYWruhV4Q0ebMjhnQa5uVuHL83fdcoc/hGb4i+Slg4kF0oSKxLy+JZXRqUTiUcCmWLH91EGm4U1pvqD1HvLQRwzLqWz0k9Ub2dmHie4jQNEl8NqaW8sIw==

/*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"*/
/*eslint-env es6*/

function *foo() {
    yield(0)
}
/*eslint space-unary-ops: "error"*/

async function foo() {
    await(bar);
}

dnC4C79+++7hmkZ7ssvw8+bXW5/8sTKG0t/W7MIzxjw7xtL1t6f5RS5y/m4UGGCuHBdns3cYxyqSLoislRN4veF8e/Xe1p83LD4Z3pnCo1ombbZJ7jIHUIkrnNXZ1n4l

/*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"*/
/*eslint-env es6*/

function *foo() {
    yield (0)
}
/*eslint space-unary-ops: "error"*/

async function foo() {
    await (bar);
}