Skip to content
Permalink
Browse files

Update: improve report location for no-space-in-parens (#12364)

* Update: improve report location for no-space-in-parens

* Add test cases with multiple spaces
  • Loading branch information
golopot authored and platinumazure committed Oct 18, 2019
1 parent d61c8a5 commit 18a0b0e3df927428a22b5b5295f9faee4bd57246
Showing with 48 additions and 30 deletions.
  1. +4 −4 lib/rules/space-in-parens.js
  2. +44 −26 tests/lib/rules/space-in-parens.js
@@ -232,7 +232,7 @@ module.exports = {
if (token.value === "(" && openerMissingSpace(token, nextToken)) {
context.report({
node,
loc: token.loc.start,
loc: token.loc,
messageId: "missingOpeningSpace",
fix(fixer) {
return fixer.insertTextAfter(token, " ");
@@ -244,7 +244,7 @@ module.exports = {
if (token.value === "(" && openerRejectsSpace(token, nextToken)) {
context.report({
node,
loc: token.loc.start,
loc: { start: token.loc.end, end: nextToken.loc.start },
messageId: "rejectedOpeningSpace",
fix(fixer) {
return fixer.removeRange([token.range[1], nextToken.range[0]]);
@@ -256,7 +256,7 @@ module.exports = {
if (token.value === ")" && closerMissingSpace(prevToken, token)) {
context.report({
node,
loc: token.loc.start,
loc: token.loc,
messageId: "missingClosingSpace",
fix(fixer) {
return fixer.insertTextBefore(token, " ");
@@ -268,7 +268,7 @@ module.exports = {
if (token.value === ")" && closerRejectsSpace(prevToken, token)) {
context.report({
node,
loc: token.loc.start,
loc: { start: prevToken.loc.end, end: token.loc.start },
messageId: "rejectedClosingSpace",
fix(fixer) {
return fixer.removeRange([prevToken.range[1], token.range[0]]);
@@ -128,17 +128,35 @@ ruleTester.run("space-in-parens", rule, {
output: "bar(baz)",
options: ["never"],
errors: [
{ messageId: "rejectedOpeningSpace", line: 1, column: 4 },
{ messageId: "rejectedClosingSpace", line: 1, column: 10 }
{ messageId: "rejectedOpeningSpace", line: 1, column: 5, endColumn: 6 },
{ messageId: "rejectedClosingSpace", line: 1, column: 9, endColumn: 10 }
]
},
{
code: "bar( baz )",
output: "bar(baz)",
options: ["never"],
errors: [
{ messageId: "rejectedOpeningSpace", line: 1, column: 5, endColumn: 7 },
{ messageId: "rejectedClosingSpace", line: 1, column: 10, endColumn: 12 }
]
},
{
code: "foo( )",
output: "foo()",
options: ["never"],
errors: [
{ messageId: "rejectedOpeningSpace", line: 1, column: 4 },
{ messageId: "rejectedClosingSpace", line: 1, column: 6 }
{ messageId: "rejectedOpeningSpace", line: 1, column: 5, endColumn: 6 },
{ messageId: "rejectedClosingSpace", line: 1, column: 5, endColumn: 6 }
]
},
{
code: "foo( )",
output: "foo()",
options: ["never"],
errors: [
{ messageId: "rejectedOpeningSpace", line: 1, column: 5, endColumn: 7 },
{ messageId: "rejectedClosingSpace", line: 1, column: 5, endColumn: 7 }
]
},
{
@@ -151,7 +169,7 @@ ruleTester.run("space-in-parens", rule, {
code: "foo\n(\nbar )",
output: "foo\n(\nbar)",
options: ["never"],
errors: [{ messageId: "rejectedClosingSpace", line: 3, column: 5 }]
errors: [{ messageId: "rejectedClosingSpace", line: 3, column: 4 }]
},
{
code: "foo\n(bar\n)\n",
@@ -170,8 +188,8 @@ ruleTester.run("space-in-parens", rule, {
output: "foo( bar )",
options: ["always"],
errors: [
{ messageId: "missingOpeningSpace", line: 1, column: 4 },
{ messageId: "missingClosingSpace", line: 1, column: 8 }
{ messageId: "missingOpeningSpace", line: 1, column: 4, endColumn: 5 },
{ messageId: "missingClosingSpace", line: 1, column: 8, endColumn: 9 }
]
},

@@ -248,7 +266,7 @@ ruleTester.run("space-in-parens", rule, {
code: "foo( /* bar */ baz)",
output: "foo(/* bar */ baz)",
options: ["never"],
errors: [{ messageId: "rejectedOpeningSpace", line: 1, column: 4 }]
errors: [{ messageId: "rejectedOpeningSpace", line: 1, column: 5 }]
},

// exceptions
@@ -266,25 +284,25 @@ ruleTester.run("space-in-parens", rule, {
output: "foo()",
options: ["always", { exceptions: ["()", "empty"] }],
errors: [
{ messageId: "rejectedOpeningSpace", line: 1, column: 4 },
{ messageId: "rejectedClosingSpace", line: 1, column: 6 }
{ messageId: "rejectedOpeningSpace", line: 1, column: 5 },
{ messageId: "rejectedClosingSpace", line: 1, column: 5 }
]
},
{
code: "foo( )",
output: "foo()",
options: ["always", { exceptions: ["empty"] }],
errors: [
{ messageId: "rejectedOpeningSpace", line: 1, column: 4 },
{ messageId: "rejectedClosingSpace", line: 1, column: 6 }
{ messageId: "rejectedOpeningSpace", line: 1, column: 5 },
{ messageId: "rejectedClosingSpace", line: 1, column: 5 }
]
},
{
code: "foo( bar() )",
output: "foo( bar())",
options: ["always", { exceptions: ["()", "empty"] }],
errors: [
{ messageId: "rejectedClosingSpace", line: 1, column: 12 }
{ messageId: "rejectedClosingSpace", line: 1, column: 11 }
]
},
{
@@ -300,10 +318,10 @@ ruleTester.run("space-in-parens", rule, {
output: "foo(bar( ))",
options: ["never", { exceptions: ["empty"] }],
errors: [
{ messageId: "rejectedOpeningSpace", line: 1, column: 4 },
{ messageId: "rejectedOpeningSpace", line: 1, column: 5 },
{ messageId: "missingOpeningSpace", line: 1, column: 9 },
{ messageId: "missingClosingSpace", line: 1, column: 10 },
{ messageId: "rejectedClosingSpace", line: 1, column: 12 }
{ messageId: "rejectedClosingSpace", line: 1, column: 11 }
]
},
{
@@ -312,7 +330,7 @@ ruleTester.run("space-in-parens", rule, {
options: ["never", { exceptions: ["[]"] }],
errors: [
{ messageId: "missingOpeningSpace", line: 1, column: 4 },
{ messageId: "rejectedClosingSpace", line: 1, column: 18 }
{ messageId: "rejectedClosingSpace", line: 1, column: 17 }
]
},
{
@@ -449,19 +467,19 @@ ruleTester.run("space-in-parens", rule, {
output: "(( 1 + 2 ))",
options: ["always", { exceptions: ["()"] }],
errors: [
{ messageId: "rejectedOpeningSpace", line: 1, column: 1 },
{ messageId: "rejectedClosingSpace", line: 1, column: 13 }
{ messageId: "rejectedOpeningSpace", line: 1, column: 2 },
{ messageId: "rejectedClosingSpace", line: 1, column: 12 }
]
},
{
code: "( ( 1 + 2 ) )",
output: "((1 + 2))",
options: ["never"],
errors: [
{ messageId: "rejectedOpeningSpace", line: 1, column: 1 },
{ messageId: "rejectedOpeningSpace", line: 1, column: 3 },
{ messageId: "rejectedClosingSpace", line: 1, column: 11 },
{ messageId: "rejectedClosingSpace", line: 1, column: 13 }
{ messageId: "rejectedOpeningSpace", line: 1, column: 2 },
{ messageId: "rejectedOpeningSpace", line: 1, column: 4 },
{ messageId: "rejectedClosingSpace", line: 1, column: 10 },
{ messageId: "rejectedClosingSpace", line: 1, column: 12 }
]
},
{
@@ -506,7 +524,7 @@ ruleTester.run("space-in-parens", rule, {
output: "var result = ( 1 / ( 1 + 2 )) + 3",
options: ["always", { exceptions: ["()"] }],
errors: [
{ messageId: "rejectedClosingSpace", line: 1, column: 30 }
{ messageId: "rejectedClosingSpace", line: 1, column: 29 }
]
},
{
@@ -524,7 +542,7 @@ ruleTester.run("space-in-parens", rule, {
errors: [
{ messageId: "missingOpeningSpace", line: 1, column: 14 },
{ messageId: "missingClosingSpace", line: 1, column: 26 },
{ messageId: "rejectedClosingSpace", line: 1, column: 28 }
{ messageId: "rejectedClosingSpace", line: 1, column: 27 }
]
},

@@ -535,8 +553,8 @@ ruleTester.run("space-in-parens", rule, {
options: ["never"],
parserOptions: { ecmaVersion: 6 },
errors: [
{ messageId: "rejectedOpeningSpace", line: 1, column: 19 },
{ messageId: "rejectedClosingSpace", line: 1, column: 27 }
{ messageId: "rejectedOpeningSpace", line: 1, column: 20 },
{ messageId: "rejectedClosingSpace", line: 1, column: 26 }
]
},
{

0 comments on commit 18a0b0e

Please sign in to comment.
You can’t perform that action at this time.