Skip to content

Commit

Permalink
Update: space-unary-ops uses astUtils.canTokensBeAdjacent (fixes #9907)…
Browse files Browse the repository at this point in the history
… (#9906)

* Update: Use astUtils.canTokensBeAdjacent in space-unary-ops (fixes #9907)

* Docs: Added some await examples to space-unary-ops
  • Loading branch information
platinumazure authored Feb 1, 2018
1 parent 084351b commit dc80487
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 13 deletions.
18 changes: 17 additions & 1 deletion docs/rules/space-unary-ops.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit dc80487

Please sign in to comment.