From 213b179946468d7919bb26ef58b737a3974668e0 Mon Sep 17 00:00:00 2001 From: Che Fisher Date: Sat, 24 Nov 2018 18:53:15 +0100 Subject: [PATCH] enforce consistent spacing inside all parenthesis update tests to respect new rule logic add more specific linting messages --- lib/rules/space-in-parens.js | 148 ++++++++------ tests/lib/rules/space-in-parens.js | 300 +++++++++++++++++++---------- 2 files changed, 287 insertions(+), 161 deletions(-) diff --git a/lib/rules/space-in-parens.js b/lib/rules/space-in-parens.js index 88f4f0b50e15..4520f2555a2b 100644 --- a/lib/rules/space-in-parens.js +++ b/lib/rules/space-in-parens.js @@ -1,6 +1,6 @@ /** * @fileoverview Disallows or enforces spaces inside of parentheses. - * @author Jonathan Rajavuori + * @author Jonathan Rajavuori and Che Fisher */ "use strict"; @@ -45,18 +45,29 @@ module.exports = { create(context) { - const MISSING_SPACE_MESSAGE = "There must be a space inside this paren.", - REJECTED_SPACE_MESSAGE = "There should be no spaces inside this paren.", + const MISSING_OPENING_SPACE_MESSAGE = "There must be a space after this paren", + MISSING_CLOSING_SPACE_MESSAGE = "There must be a space before this paren", + REJECTED_OPENING_SPACE_MESSAGE = "There should be no space after this paren", + REJECTED_CLOSING_SPACE_MESSAGE = "There should be no space before this paren", ALWAYS = context.options[0] === "always", exceptionsArrayOptions = (context.options[1] && context.options[1].exceptions) || [], options = {}; + let exceptions; if (exceptionsArrayOptions.length) { - options.braceException = exceptionsArrayOptions.indexOf("{}") !== -1; - options.bracketException = exceptionsArrayOptions.indexOf("[]") !== -1; - options.parenException = exceptionsArrayOptions.indexOf("()") !== -1; - options.empty = exceptionsArrayOptions.indexOf("empty") !== -1; + options.braceException = exceptionsArrayOptions.includes("{}"); + options.bracketException = exceptionsArrayOptions.includes("[]"); + options.parenException = exceptionsArrayOptions.includes("()"); + options.empty = exceptionsArrayOptions.includes("empty"); + } + + /* + * workaround to prevent empty paren false positives + * if we never allow a space inside parentheses this should include empty parentheses too + */ + if (ALWAYS && options.parenException) { + options.empty = true; } /** @@ -106,7 +117,7 @@ module.exports = { * @returns {boolean} True if the token is one of the exceptions for the opener paren */ function isOpenerException(token) { - return token.type === "Punctuator" && exceptions.openers.indexOf(token.value) >= 0; + return exceptions.openers.includes(token.value); } /** @@ -115,38 +126,17 @@ module.exports = { * @returns {boolean} True if the token is one of the exceptions for the closer paren */ function isCloserException(token) { - return token.type === "Punctuator" && exceptions.closers.indexOf(token.value) >= 0; + return exceptions.closers.includes(token.value); } /** - * Determines if an opener paren should have a missing space after it + * Determines if an opening paren has a space immediatley after it * @param {Object} left The paren token * @param {Object} right The token after it - * @returns {boolean} True if the paren should have a space + * @returns {boolean} True if the opening paren is missing a required space */ - function shouldOpenerHaveSpace(left, right) { - if (sourceCode.isSpaceBetweenTokens(left, right)) { - return false; - } - - if (ALWAYS) { - if (astUtils.isClosingParenToken(right)) { - return false; - } - return !isOpenerException(right); - } - return isOpenerException(right); - - } - - /** - * Determines if an closer paren should have a missing space after it - * @param {Object} left The token before the paren - * @param {Object} right The paren token - * @returns {boolean} True if the paren should have a space - */ - function shouldCloserHaveSpace(left, right) { - if (astUtils.isOpeningParenToken(left)) { + function openerMissingSpace(left, right) { + if (!astUtils.isTokenOnSameLine(left, right)) { return false; } @@ -155,24 +145,30 @@ module.exports = { } if (ALWAYS) { - return !isCloserException(left); + if (right.type !== "Punctuator") { + return !isOpenerException(left); + } + return !isOpenerException(right); } - return isCloserException(left); + if (right.type !== "Punctuator") { + return isOpenerException(left); + } + return isOpenerException(right); } /** - * Determines if an opener paren should not have an existing space after it + * Determines if an opening paren has a space immediately after it * @param {Object} left The paren token * @param {Object} right The token after it - * @returns {boolean} True if the paren should reject the space + * @returns {boolean} True if the opening paren has a disallowed space following it */ - function shouldOpenerRejectSpace(left, right) { - if (right.type === "Line") { + function openerRejectsSpace(left, right) { + if (!astUtils.isTokenOnSameLine(left, right)) { return false; } - if (!astUtils.isTokenOnSameLine(left, right)) { + if (right.type === "Line") { return false; } @@ -181,23 +177,49 @@ module.exports = { } if (ALWAYS) { + if (right.type !== "Punctuator") { + return isOpenerException(left); + } return isOpenerException(right); } - return !isOpenerException(right); + if (right.type !== "Punctuator") { + return !isOpenerException(left); + } + return !isOpenerException(right); } /** - * Determines if an closer paren should not have an existing space after it + * Determines if a closing paren has a space immediately before it * @param {Object} left The token before the paren * @param {Object} right The paren token - * @returns {boolean} True if the paren should reject the space + * @returns {boolean} True if the paren is missing the required space */ - function shouldCloserRejectSpace(left, right) { - if (astUtils.isOpeningParenToken(left)) { + function closerMissingSpace(left, right) { + if (sourceCode.isSpaceBetweenTokens(left, right)) { return false; } + if (ALWAYS) { + if (left.type !== "Punctuator") { + return !isCloserException(right); + } + return !isCloserException(left); + } + + if (left.type !== "Punctuator") { + return isCloserException(right); + } + return isCloserException(left); + } + + /** + * Determines if a closer paren has a space immediately before it + * @param {Object} left The token before the paren + * @param {Object} right The paren token + * @returns {boolean} True if the paren has a space before it + */ + function closerRejectsSpace(left, right) { if (!astUtils.isTokenOnSameLine(left, right)) { return false; } @@ -207,8 +229,14 @@ module.exports = { } if (ALWAYS) { + if (left.type !== "Punctuator") { + return isCloserException(right); + } return isCloserException(left); } + if (left.type !== "Punctuator") { + return !isCloserException(right); + } return !isCloserException(left); } @@ -226,44 +254,53 @@ module.exports = { const prevToken = tokens[i - 1]; const nextToken = tokens[i + 1]; + // if token is not an opening or closing paren token, do nothing if (!astUtils.isOpeningParenToken(token) && !astUtils.isClosingParenToken(token)) { return; } - if (token.value === "(" && shouldOpenerHaveSpace(token, nextToken)) { + // if token is an opening paren and is not followed by a required space + if (token.value === "(" && openerMissingSpace(token, nextToken)) { context.report({ node, loc: token.loc.start, - message: MISSING_SPACE_MESSAGE, + message: MISSING_OPENING_SPACE_MESSAGE, fix(fixer) { return fixer.insertTextAfter(token, " "); } }); - } else if (token.value === "(" && shouldOpenerRejectSpace(token, nextToken)) { + } + + // if token is an opening paren and is followed by a disallowed space + if (token.value === "(" && openerRejectsSpace(token, nextToken)) { context.report({ node, loc: token.loc.start, - message: REJECTED_SPACE_MESSAGE, + message: REJECTED_OPENING_SPACE_MESSAGE, fix(fixer) { return fixer.removeRange([token.range[1], nextToken.range[0]]); } }); - } else if (token.value === ")" && shouldCloserHaveSpace(prevToken, token)) { + } - // context.report(node, token.loc.start, MISSING_SPACE_MESSAGE); + // if token is a closing paren and is not preceded by a required space + if (token.value === ")" && closerMissingSpace(prevToken, token)) { context.report({ node, loc: token.loc.start, - message: MISSING_SPACE_MESSAGE, + message: MISSING_CLOSING_SPACE_MESSAGE, fix(fixer) { return fixer.insertTextBefore(token, " "); } }); - } else if (token.value === ")" && shouldCloserRejectSpace(prevToken, token)) { + } + + // if token is a closing paren and is preceded by a disallowed space + if (token.value === ")" && closerRejectsSpace(prevToken, token)) { context.report({ node, loc: token.loc.start, - message: REJECTED_SPACE_MESSAGE, + message: REJECTED_CLOSING_SPACE_MESSAGE, fix(fixer) { return fixer.removeRange([prevToken.range[1], token.range[0]]); } @@ -272,6 +309,5 @@ module.exports = { }); } }; - } }; diff --git a/tests/lib/rules/space-in-parens.js b/tests/lib/rules/space-in-parens.js index 6e66a8fbd34b..4ae68761e8da 100644 --- a/tests/lib/rules/space-in-parens.js +++ b/tests/lib/rules/space-in-parens.js @@ -11,8 +11,10 @@ const rule = require("../../../lib/rules/space-in-parens"), RuleTester = require("../../../lib/testers/rule-tester"); -const MISSING_SPACE_ERROR = "There must be a space inside this paren.", - REJECTED_SPACE_ERROR = "There should be no spaces inside this paren."; +const MISSING_OPENING_SPACE_ERROR = "There must be a space after this paren", + MISSING_CLOSING_SPACE_ERROR = "There must be a space before this paren", + REJECTED_OPENING_SPACE_ERROR = "There should be no space after this paren", + REJECTED_CLOSING_SPACE_ERROR = "There should be no space before this paren"; //------------------------------------------------------------------------------ // Tests @@ -23,7 +25,8 @@ const ruleTester = new RuleTester(); ruleTester.run("space-in-parens", rule, { valid: [ - { code: "foo()", options: ["always"] }, + { code: "foo()", options: ["never"] }, + { code: "foo( )", options: ["always"] }, { code: "foo( bar )", options: ["always"] }, { code: "foo\n(\nbar\n)\n", options: ["always"] }, { code: "foo\n( \nbar\n )\n", options: ["always"] }, @@ -84,15 +87,15 @@ ruleTester.run("space-in-parens", rule, { { code: "foo( [ 1, 2 ], 1)", options: ["never", { exceptions: ["[]"] }] }, { code: "foo( [\n1,\n2\n] )", options: ["never", { exceptions: ["[]"] }] }, - { code: "foo(( 1 + 2 ))", options: ["always", { exceptions: ["()"] }] }, + { code: "foo((1 + 2))", options: ["always", { exceptions: ["()"] }] }, { code: "foo( ( 1 + 2 ) )", options: ["always", { exceptions: ["{}"] }] }, - { code: "foo( 1 / ( 1 + 2 ))", options: ["always", { exceptions: ["()"] }] }, - { code: "foo(( 1 + 2 ) / 1 )", options: ["always", { exceptions: ["()"] }] }, + { code: "foo(1 / (1 + 2))", options: ["always", { exceptions: ["()"] }] }, + { code: "foo((1 + 2) / 1)", options: ["always", { exceptions: ["()"] }] }, { code: "foo((\n1 + 2\n))", options: ["always", { exceptions: ["()"] }] }, { code: "foo((1 + 2))", options: ["never", { exceptions: ["{}"] }] }, - { code: "foo( (1 + 2) )", options: ["never", { exceptions: ["()"] }] }, - { code: "foo(1 / (1 + 2) )", options: ["never", { exceptions: ["()"] }] }, - { code: "foo( (1 + 2) / 1)", options: ["never", { exceptions: ["()"] }] }, + { code: "foo( ( 1 + 2 ) )", options: ["never", { exceptions: ["()"] }] }, + { code: "foo( 1 / ( 1 + 2 ) )", options: ["never", { exceptions: ["()"] }] }, + { code: "foo( ( 1 + 2 ) / 1 )", options: ["never", { exceptions: ["()"] }] }, { code: "foo( (\n1 + 2\n) )", options: ["never", { exceptions: ["()"] }] }, { code: "foo()", options: ["always", { exceptions: ["empty"] }] }, @@ -115,71 +118,90 @@ ruleTester.run("space-in-parens", rule, { ], invalid: [ + + // methods and functions + { + code: "bar(baz )", + output: "bar(baz)", + options: ["never"], + errors: [REJECTED_CLOSING_SPACE_ERROR] + }, + { + code: "bar( baz )", + output: "bar(baz)", + options: ["never"], + errors: [ + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 4 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 10 } + ] + }, { code: "foo( )", output: "foo()", options: ["never"], - errors: [{ message: REJECTED_SPACE_ERROR, line: 1, column: 4 }] + errors: [ + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 4 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 6 } + ] + }, + { + code: "foo(bar() )", + output: "foo(bar())", + options: ["never"], + errors: [REJECTED_CLOSING_SPACE_ERROR] + }, + { + code: "foo\n(\nbar )", + output: "foo\n(\nbar)", // actual error + options: ["never"], + errors: [{ message: REJECTED_CLOSING_SPACE_ERROR, line: 3, column: 5 }] + }, + { + code: "foo\n(bar\n)\n", + output: "foo\n( bar\n)\n", + options: ["always"], + errors: [{ message: MISSING_OPENING_SPACE_ERROR, line: 2, column: 1 }] }, { code: "foo( bar)", output: "foo( bar )", options: ["always"], - errors: [{ message: MISSING_SPACE_ERROR, line: 1, column: 9 }] + errors: [{ message: MISSING_CLOSING_SPACE_ERROR, line: 1, column: 9 }] }, { code: "foo(bar)", output: "foo( bar )", options: ["always"], errors: [ - { message: MISSING_SPACE_ERROR, line: 1, column: 4 }, - { message: MISSING_SPACE_ERROR, line: 1, column: 8 } + { message: MISSING_OPENING_SPACE_ERROR, line: 1, column: 4 }, + { message: MISSING_CLOSING_SPACE_ERROR, line: 1, column: 8 } ] }, + + // variable declaration and formulas { code: "var x = ( 1 + 2) * 3", output: "var x = ( 1 + 2 ) * 3", options: ["always"], - errors: [{ message: MISSING_SPACE_ERROR, line: 1, column: 16 }] + errors: [{ message: MISSING_CLOSING_SPACE_ERROR, line: 1, column: 16 }] }, { code: "var x = (1 + 2 ) * 3", output: "var x = ( 1 + 2 ) * 3", options: ["always"], - errors: [{ message: MISSING_SPACE_ERROR, line: 1, column: 9 }] - }, - { - code: "foo\n(bar\n)\n", - output: "foo\n( bar\n)\n", - options: ["always"], - errors: [{ message: MISSING_SPACE_ERROR, line: 2, column: 1 }] - }, - { - code: "bar(baz )", - output: "bar(baz)", - options: ["never"], - errors: [REJECTED_SPACE_ERROR] - }, - { - code: "bar( baz )", - output: "bar(baz)", - options: ["never"], - errors: [ - { message: REJECTED_SPACE_ERROR, line: 1, column: 4 }, - { message: REJECTED_SPACE_ERROR, line: 1, column: 10 } - ] + errors: [{ message: MISSING_OPENING_SPACE_ERROR, line: 1, column: 9 }] }, { code: "var x = ( 4 + 5) * 6", output: "var x = (4 + 5) * 6", options: ["never"], - errors: [REJECTED_SPACE_ERROR] + errors: [REJECTED_OPENING_SPACE_ERROR] }, { code: "var x = (4 + 5 ) * 6", output: "var x = (4 + 5) * 6", options: ["never"], - errors: [REJECTED_SPACE_ERROR] + errors: [REJECTED_CLOSING_SPACE_ERROR] }, // comments @@ -187,155 +209,202 @@ ruleTester.run("space-in-parens", rule, { code: "foo(/* bar */)", output: "foo( /* bar */ )", options: ["always"], - errors: [MISSING_SPACE_ERROR, MISSING_SPACE_ERROR] + errors: [MISSING_OPENING_SPACE_ERROR, MISSING_CLOSING_SPACE_ERROR] }, { code: "foo(/* bar */baz )", output: "foo( /* bar */baz )", options: ["always"], - errors: [MISSING_SPACE_ERROR] + errors: [MISSING_OPENING_SPACE_ERROR] }, { code: "foo(/* bar */ baz )", output: "foo( /* bar */ baz )", options: ["always"], - errors: [MISSING_SPACE_ERROR] + errors: [MISSING_OPENING_SPACE_ERROR] }, { code: "foo( baz/* bar */)", output: "foo( baz/* bar */ )", options: ["always"], - errors: [MISSING_SPACE_ERROR] + errors: [MISSING_CLOSING_SPACE_ERROR] }, { code: "foo( baz /* bar */)", output: "foo( baz /* bar */ )", options: ["always"], - errors: [MISSING_SPACE_ERROR] + errors: [MISSING_CLOSING_SPACE_ERROR] }, { code: "foo( /* bar */ )", output: "foo(/* bar */)", options: ["never"], - errors: [REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR] + errors: [REJECTED_OPENING_SPACE_ERROR, REJECTED_CLOSING_SPACE_ERROR] }, { code: "foo( /* bar */ baz)", output: "foo(/* bar */ baz)", options: ["never"], - errors: [{ message: REJECTED_SPACE_ERROR, line: 1, column: 4 }] + errors: [{ message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 4 }] }, // exceptions + { + code: "foo()", + output: "foo( )", + options: ["never", { exceptions: ["empty"] }], + errors: [ + { message: MISSING_OPENING_SPACE_ERROR, line: 1, column: 4 }, + { message: MISSING_CLOSING_SPACE_ERROR, line: 1, column: 5 } + ] + }, + { + code: "foo( )", + output: "foo()", + options: ["always", { exceptions: ["empty"] }], + errors: [ + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 4 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 6 } + ] + }, + { + code: "foo( bar() )", + output: "foo(bar())", + options: ["always", { exceptions: ["()"] }], + errors: [ + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 4 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 12 } + ] + }, + { + code: "foo(bar())", + output: "foo( bar() )", + options: ["never", { exceptions: ["()"] }], + errors: [ + { message: MISSING_OPENING_SPACE_ERROR, line: 1, column: 4 }, + { message: MISSING_CLOSING_SPACE_ERROR, line: 1, column: 10 } + ] + }, + { + code: "foo([1,2], bar() )", + output: "foo( [1,2], bar())", + options: ["never", { exceptions: ["[]"] }], + errors: [ + { message: MISSING_OPENING_SPACE_ERROR, line: 1, column: 4 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 18 } + ] + }, { code: "foo({ bar: 'baz' })", output: "foo( { bar: 'baz' } )", options: ["always", { exceptions: ["[]"] }], - errors: [MISSING_SPACE_ERROR, MISSING_SPACE_ERROR] + errors: [MISSING_OPENING_SPACE_ERROR, MISSING_CLOSING_SPACE_ERROR] }, { code: "foo( { bar: 'baz' } )", output: "foo({ bar: 'baz' })", options: ["always", { exceptions: ["{}"] }], - errors: [REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR] + errors: [REJECTED_OPENING_SPACE_ERROR, REJECTED_CLOSING_SPACE_ERROR] }, { code: "foo({ bar: 'baz' })", output: "foo( { bar: 'baz' } )", options: ["never", { exceptions: ["{}"] }], - errors: [MISSING_SPACE_ERROR, MISSING_SPACE_ERROR] + errors: [MISSING_OPENING_SPACE_ERROR, MISSING_CLOSING_SPACE_ERROR] }, { code: "foo( { bar: 'baz' } )", output: "foo({ bar: 'baz' })", options: ["never", { exceptions: ["[]"] }], - errors: [REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR] + errors: [REJECTED_OPENING_SPACE_ERROR, REJECTED_CLOSING_SPACE_ERROR] }, { code: "foo( { bar: 'baz' })", output: "foo({ bar: 'baz' })", options: ["always", { exceptions: ["{}"] }], - errors: [REJECTED_SPACE_ERROR] + errors: [REJECTED_OPENING_SPACE_ERROR] }, { code: "foo( { bar: 'baz' })", output: "foo( { bar: 'baz' } )", options: ["never", { exceptions: ["{}"] }], - errors: [MISSING_SPACE_ERROR] + errors: [MISSING_CLOSING_SPACE_ERROR] }, { code: "foo({ bar: 'baz' } )", output: "foo({ bar: 'baz' })", options: ["always", { exceptions: ["{}"] }], - errors: [REJECTED_SPACE_ERROR] + errors: [REJECTED_CLOSING_SPACE_ERROR] }, { code: "foo({ bar: 'baz' } )", output: "foo( { bar: 'baz' } )", options: ["never", { exceptions: ["{}"] }], - errors: [MISSING_SPACE_ERROR] + errors: [MISSING_OPENING_SPACE_ERROR] }, { code: "foo([ 1, 2 ])", output: "foo( [ 1, 2 ] )", options: ["always", { exceptions: ["empty"] }], - errors: [MISSING_SPACE_ERROR, MISSING_SPACE_ERROR] + errors: [MISSING_OPENING_SPACE_ERROR, MISSING_CLOSING_SPACE_ERROR] }, { code: "foo( [ 1, 2 ] )", output: "foo([ 1, 2 ])", options: ["always", { exceptions: ["[]"] }], - errors: [REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR] + errors: [REJECTED_OPENING_SPACE_ERROR, REJECTED_CLOSING_SPACE_ERROR] }, { code: "foo([ 1, 2 ])", output: "foo( [ 1, 2 ] )", options: ["never", { exceptions: ["[]"] }], - errors: [MISSING_SPACE_ERROR, MISSING_SPACE_ERROR] + errors: [MISSING_OPENING_SPACE_ERROR, MISSING_CLOSING_SPACE_ERROR] }, { code: "foo( [ 1, 2 ] )", output: "foo([ 1, 2 ])", options: ["never", { exceptions: ["()"] }], - errors: [REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR] + errors: [REJECTED_OPENING_SPACE_ERROR, REJECTED_CLOSING_SPACE_ERROR] }, { code: "foo([ 1, 2 ] )", output: "foo([ 1, 2 ])", options: ["always", { exceptions: ["[]"] }], - errors: [REJECTED_SPACE_ERROR] + errors: [REJECTED_CLOSING_SPACE_ERROR] }, { code: "foo([ 1, 2 ] )", output: "foo( [ 1, 2 ] )", options: ["never", { exceptions: ["[]"] }], - errors: [MISSING_SPACE_ERROR] + errors: [MISSING_OPENING_SPACE_ERROR] }, { code: "foo( [ 1, 2 ])", output: "foo([ 1, 2 ])", options: ["always", { exceptions: ["[]"] }], - errors: [REJECTED_SPACE_ERROR] + errors: [REJECTED_OPENING_SPACE_ERROR] }, { code: "foo( [ 1, 2 ])", output: "foo( [ 1, 2 ] )", options: ["never", { exceptions: ["[]"] }], - errors: [MISSING_SPACE_ERROR] + errors: [MISSING_CLOSING_SPACE_ERROR] }, { code: "(( 1 + 2 ))", output: "( ( 1 + 2 ) )", options: ["always", { exceptions: ["[]"] }], - errors: [MISSING_SPACE_ERROR, MISSING_SPACE_ERROR] + errors: [MISSING_OPENING_SPACE_ERROR, MISSING_CLOSING_SPACE_ERROR] }, { code: "( ( 1 + 2 ) )", - output: "(( 1 + 2 ))", + output: "((1 + 2))", options: ["always", { exceptions: ["()"] }], errors: [ - { message: REJECTED_SPACE_ERROR, line: 1, column: 1 }, - { message: REJECTED_SPACE_ERROR, line: 1, column: 13 } + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 1 }, + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 3 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 11 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 13 } ] }, { @@ -343,91 +412,112 @@ ruleTester.run("space-in-parens", rule, { output: "((1 + 2))", options: ["never"], errors: [ - { message: REJECTED_SPACE_ERROR, line: 1, column: 1 }, - { message: REJECTED_SPACE_ERROR, line: 1, column: 3 }, - { message: REJECTED_SPACE_ERROR, line: 1, column: 11 }, - { message: REJECTED_SPACE_ERROR, line: 1, column: 13 } + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 1 }, + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 3 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 11 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 13 } ] }, { code: "( ( 1 + 2 ) )", output: "((1 + 2))", options: ["never", { exceptions: ["[]"] }], - errors: [REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR] + errors: [ + REJECTED_OPENING_SPACE_ERROR, + REJECTED_OPENING_SPACE_ERROR, + REJECTED_CLOSING_SPACE_ERROR, + REJECTED_CLOSING_SPACE_ERROR + ] }, { code: "( ( 1 + 2 ))", - output: "(( 1 + 2 ))", + output: "((1 + 2))", options: ["always", { exceptions: ["()"] }], - errors: [{ message: REJECTED_SPACE_ERROR, line: 1, column: 1 }] + errors: [ + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 1 }, + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 3 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 11 } + ] }, { code: "( (1 + 2))", - output: "( (1 + 2) )", + output: "( ( 1 + 2 ) )", options: ["never", { exceptions: ["()"] }], - errors: [MISSING_SPACE_ERROR] + errors: [ + { message: MISSING_OPENING_SPACE_ERROR, line: 1, column: 3 }, + { message: MISSING_CLOSING_SPACE_ERROR, line: 1, column: 9 }, + { message: MISSING_CLOSING_SPACE_ERROR, line: 1, column: 10 } + ] }, { code: "(( 1 + 2 ) )", - output: "(( 1 + 2 ))", + output: "((1 + 2))", options: ["always", { exceptions: ["()"] }], - errors: [REJECTED_SPACE_ERROR] + errors: [ + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 2 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 10 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 12 } + ] }, { code: "((1 + 2) )", - output: "( (1 + 2) )", + output: "( ( 1 + 2 ) )", options: ["never", { exceptions: ["()"] }], - errors: [MISSING_SPACE_ERROR] + errors: [ + { message: MISSING_OPENING_SPACE_ERROR, line: 1, column: 1 }, + { message: MISSING_OPENING_SPACE_ERROR, line: 1, column: 2 }, + { message: MISSING_CLOSING_SPACE_ERROR, line: 1, column: 8 } + ] }, { code: "var result = ( 1 / ( 1 + 2 ) ) + 3", - output: "var result = ( 1 / ( 1 + 2 )) + 3", + output: "var result = (1 / (1 + 2)) + 3", options: ["always", { exceptions: ["()"] }], - errors: [REJECTED_SPACE_ERROR] + errors: [ + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 14 }, + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 20 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 28 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 30 } + ] }, { code: "var result = (1 / (1 + 2)) + 3", - output: "var result = (1 / (1 + 2) ) + 3", + output: "var result = ( 1 / ( 1 + 2 ) ) + 3", options: ["never", { exceptions: ["()"] }], - errors: [MISSING_SPACE_ERROR] + errors: [ + { message: MISSING_OPENING_SPACE_ERROR, line: 1, column: 14 }, + { message: MISSING_OPENING_SPACE_ERROR, line: 1, column: 19 }, + { message: MISSING_CLOSING_SPACE_ERROR, line: 1, column: 25 }, + { message: MISSING_CLOSING_SPACE_ERROR, line: 1, column: 26 } + ] }, { code: "var result = ( 1 / ( 1 + 2)) + 3", - output: "var result = ( 1 / ( 1 + 2 )) + 3", + output: "var result = (1 / (1 + 2)) + 3", options: ["always", { exceptions: ["()"] }], - errors: [MISSING_SPACE_ERROR] - }, - { - code: "foo( )", - output: "foo()", - options: ["always", { exceptions: ["empty"] }], - errors: [{ message: REJECTED_SPACE_ERROR, line: 1, column: 4 }] - }, - { - code: "foo()", - output: "foo( )", - options: ["never", { exceptions: ["empty"] }], - errors: [{ message: MISSING_SPACE_ERROR, line: 1, column: 4 }] - }, - { - code: "foo\n(\nbar )\n", - output: "foo\n(\nbar)\n", - options: ["never"], - errors: [{ message: REJECTED_SPACE_ERROR, line: 3, column: 5 }] + errors: [ + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 14 }, + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 20 } + ] }, + + // ES6 { - code: "var foo = `(bar ${(1 + 2 )})`;", + code: "var foo = `(bar ${( 1 + 2 )})`;", output: "var foo = `(bar ${(1 + 2)})`;", options: ["never"], parserOptions: { ecmaVersion: 6 }, - errors: [{ message: REJECTED_SPACE_ERROR, line: 1, column: 26 }] + errors: [ + { message: REJECTED_OPENING_SPACE_ERROR, line: 1, column: 19 }, + { message: REJECTED_CLOSING_SPACE_ERROR, line: 1, column: 27 } + ] }, { code: "var foo = `(bar ${(1 + 2 )})`;", output: "var foo = `(bar ${( 1 + 2 )})`;", options: ["always"], parserOptions: { ecmaVersion: 6 }, - errors: [{ message: MISSING_SPACE_ERROR, line: 1, column: 19 }] + errors: [{ message: MISSING_OPENING_SPACE_ERROR, line: 1, column: 19 }] } ] });