Skip to content

Commit

Permalink
Update: Use astUtils.canTokensBeAdjacent in space-unary-ops
Browse files Browse the repository at this point in the history
  • Loading branch information
platinumazure committed Jan 28, 2018
1 parent 084351b commit 1ea47cc
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 12 deletions.
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

0 comments on commit 1ea47cc

Please sign in to comment.