Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: space-unary-ops uses astUtils.canTokensBeAdjacent (fixes #9907) #9906

Merged
merged 2 commits into from Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion docs/rules/space-unary-ops.md
Expand Up @@ -59,7 +59,7 @@ This rule has three options:

In this case, spacing will be disallowed after a `new` operator and required before/after a `++` operator.

Examples of **incorrect** code for this rule with the `{"words": true, "nonwords": false}` option:
Examples of **incorrect** code for this rule with the default `{"words": true, "nonwords": false}` option:

```js
/*eslint space-unary-ops: "error"*/
Expand Down Expand Up @@ -90,6 +90,14 @@ function *foo() {
}
```

```js
/*eslint space-unary-ops: "error"*/

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

Examples of **correct** code for this rule with the `{"words": true, "nonwords": false}` option:

```js
Expand Down Expand Up @@ -125,3 +133,11 @@ function *foo() {
yield (0)
}
```

```js
/*eslint space-unary-ops: "error"*/

async function foo() {
await (bar);
}
```
11 changes: 1 addition & 10 deletions lib/rules/space-unary-ops.js
Expand Up @@ -66,15 +66,6 @@ module.exports = {
node.argument && node.argument.type === "UnaryExpression" && node.argument.operator === "!";
}

/**
* Check if the node's child argument is an "ObjectExpression"
* @param {ASTnode} node AST node
* @returns {boolean} Whether or not the argument's type is "ObjectExpression"
*/
function isArgumentObjectExpression(node) {
return node.argument && node.argument.type && node.argument.type === "ObjectExpression";
}

/**
* Checks if an override exists for a given operator.
* @param {string} operator Operator
Expand Down Expand Up @@ -125,7 +116,7 @@ module.exports = {
* @returns {void}
*/
function verifyWordDoesntHaveSpaces(node, firstToken, secondToken, word) {
if (isArgumentObjectExpression(node)) {
if (astUtils.canTokensBeAdjacent(firstToken, secondToken)) {
if (secondToken.range[0] > firstToken.range[1]) {
context.report({
node,
Expand Down
122 changes: 120 additions & 2 deletions tests/lib/rules/space-unary-ops.js
Expand Up @@ -41,6 +41,7 @@ ruleTester.run("space-unary-ops", rule, {
code: "foo.bar --",
options: [{ nonwords: true }]
},

{
code: "delete foo.bar",
options: [{ words: true }]
Expand All @@ -49,6 +50,14 @@ ruleTester.run("space-unary-ops", rule, {
code: "delete foo[\"bar\"]",
options: [{ words: true }]
},
{
code: "delete foo.bar",
options: [{ words: false }]
},
{
code: "delete(foo.bar)",
options: [{ words: false }]
},

{
code: "new Foo",
Expand Down Expand Up @@ -79,6 +88,14 @@ ruleTester.run("space-unary-ops", rule, {
code: "typeof {foo:true}",
options: [{ words: true }]
},
{
code: "typeof (foo)",
options: [{ words: true }]
},
{
code: "typeof(foo)",
options: [{ words: false }]
},
{
code: "typeof!foo",
options: [{ words: false }]
Expand All @@ -100,6 +117,14 @@ ruleTester.run("space-unary-ops", rule, {
code: "void foo",
options: [{ words: true }]
},
{
code: "void foo",
options: [{ words: false }]
},
{
code: "void(foo)",
options: [{ words: false }]
},

{
code: "-1",
Expand Down Expand Up @@ -217,12 +242,12 @@ ruleTester.run("space-unary-ops", rule, {
options: [{ words: false, overrides: { new: false } }]
},
{
code: "function *foo () { yield (0) }",
code: "function *foo () { yield(0) }",
options: [{ words: true, overrides: { yield: false } }],
parserOptions: { ecmaVersion: 6 }
},
{
code: "function *foo () { yield (0) }",
code: "function *foo () { yield(0) }",
options: [{ words: false, overrides: { yield: false } }],
parserOptions: { ecmaVersion: 6 }
}
Expand All @@ -247,6 +272,15 @@ ruleTester.run("space-unary-ops", rule, {
type: "UnaryExpression"
}]
},
{
code: "delete (foo.bar)",
output: "delete(foo.bar)",
options: [{ words: false }],
errors: [{
message: "Unexpected space after unary word operator 'delete'.",
type: "UnaryExpression"
}]
},
{
code: "new(Foo)",
output: "new (Foo)",
Expand All @@ -256,6 +290,15 @@ ruleTester.run("space-unary-ops", rule, {
type: "NewExpression"
}]
},
{
code: "new (Foo)",
output: "new(Foo)",
options: [{ words: false }],
errors: [{
message: "Unexpected space after unary word operator 'new'.",
type: "NewExpression"
}]
},
{
code: "new(Foo())",
output: "new (Foo())",
Expand All @@ -265,6 +308,15 @@ ruleTester.run("space-unary-ops", rule, {
type: "NewExpression"
}]
},
{
code: "new [foo][0]",
output: "new[foo][0]",
options: [{ words: false }],
errors: [{
message: "Unexpected space after unary word operator 'new'.",
type: "NewExpression"
}]
},

{
code: "typeof(foo)",
Expand All @@ -275,6 +327,33 @@ ruleTester.run("space-unary-ops", rule, {
type: "UnaryExpression"
}]
},
{
code: "typeof (foo)",
output: "typeof(foo)",
options: [{ words: false }],
errors: [{
message: "Unexpected space after unary word operator 'typeof'.",
type: "UnaryExpression"
}]
},
{
code: "typeof[foo]",
output: "typeof [foo]",
options: [{ words: true }],
errors: [{
message: "Unary word operator 'typeof' must be followed by whitespace.",
type: "UnaryExpression"
}]
},
{
code: "typeof [foo]",
output: "typeof[foo]",
options: [{ words: false }],
errors: [{
message: "Unexpected space after unary word operator 'typeof'.",
type: "UnaryExpression"
}]
},
{
code: "typeof{foo:true}",
output: "typeof {foo:true}",
Expand Down Expand Up @@ -321,6 +400,15 @@ ruleTester.run("space-unary-ops", rule, {
type: "UnaryExpression"
}]
},
{
code: "void[foo];",
output: "void [foo];",
options: [{ words: true }],
errors: [{
message: "Unary word operator 'void' must be followed by whitespace.",
type: "UnaryExpression"
}]
},
{
code: "void{a:0};",
output: "void {a:0};",
Expand All @@ -330,6 +418,24 @@ ruleTester.run("space-unary-ops", rule, {
type: "UnaryExpression"
}]
},
{
code: "void (foo)",
output: "void(foo)",
options: [{ words: false }],
errors: [{
message: "Unexpected space after unary word operator 'void'.",
type: "UnaryExpression"
}]
},
{
code: "void [foo]",
output: "void[foo]",
options: [{ words: false }],
errors: [{
message: "Unexpected space after unary word operator 'void'.",
type: "UnaryExpression"
}]
},

{
code: "! foo",
Expand Down Expand Up @@ -488,6 +594,18 @@ ruleTester.run("space-unary-ops", rule, {
column: 19
}]
},
{
code: "function *foo() { yield (0) }",
output: "function *foo() { yield(0) }",
options: [{ words: false }],
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Unexpected space after unary word operator 'yield'.",
type: "YieldExpression",
line: 1,
column: 19
}]
},
{
code: "function *foo() { yield+0 }",
output: "function *foo() { yield +0 }",
Expand Down