From dc804875fcac38dc4c89b7631007d534815c6078 Mon Sep 17 00:00:00 2001 From: Kevin Partington Date: Thu, 1 Feb 2018 16:01:50 -0600 Subject: [PATCH] Update: space-unary-ops uses astUtils.canTokensBeAdjacent (fixes #9907) (#9906) * Update: Use astUtils.canTokensBeAdjacent in space-unary-ops (fixes #9907) * Docs: Added some await examples to space-unary-ops --- docs/rules/space-unary-ops.md | 18 ++++- lib/rules/space-unary-ops.js | 11 +-- tests/lib/rules/space-unary-ops.js | 122 ++++++++++++++++++++++++++++- 3 files changed, 138 insertions(+), 13 deletions(-) diff --git a/docs/rules/space-unary-ops.md b/docs/rules/space-unary-ops.md index 8853d9239ad..d04bd4a4561 100644 --- a/docs/rules/space-unary-ops.md +++ b/docs/rules/space-unary-ops.md @@ -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"*/ @@ -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 @@ -125,3 +133,11 @@ function *foo() { yield (0) } ``` + +```js +/*eslint space-unary-ops: "error"*/ + +async function foo() { + await (bar); +} +``` diff --git a/lib/rules/space-unary-ops.js b/lib/rules/space-unary-ops.js index 4d122836ad6..601d705e673 100644 --- a/lib/rules/space-unary-ops.js +++ b/lib/rules/space-unary-ops.js @@ -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 @@ -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, diff --git a/tests/lib/rules/space-unary-ops.js b/tests/lib/rules/space-unary-ops.js index 22efa208854..11b2c96bfc1 100644 --- a/tests/lib/rules/space-unary-ops.js +++ b/tests/lib/rules/space-unary-ops.js @@ -41,6 +41,7 @@ ruleTester.run("space-unary-ops", rule, { code: "foo.bar --", options: [{ nonwords: true }] }, + { code: "delete foo.bar", options: [{ words: true }] @@ -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", @@ -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 }] @@ -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", @@ -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 } } @@ -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)", @@ -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())", @@ -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)", @@ -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}", @@ -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};", @@ -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", @@ -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 }",