Skip to content

Commit

Permalink
Update: improve report location for no-space-in-parens (#12364)
Browse files Browse the repository at this point in the history
* 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 18a0b0e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
8 changes: 4 additions & 4 deletions lib/rules/space-in-parens.js
Expand Up @@ -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, " ");
Expand All @@ -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]]);
Expand All @@ -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, " ");
Expand All @@ -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]]);
Expand Down
70 changes: 44 additions & 26 deletions tests/lib/rules/space-in-parens.js
Expand Up @@ -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 }
]
},
{
Expand All @@ -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",
Expand All @@ -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 }
]
},

Expand Down Expand Up @@ -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
Expand All @@ -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 }
]
},
{
Expand All @@ -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 }
]
},
{
Expand All @@ -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 }
]
},
{
Expand Down Expand Up @@ -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 }
]
},
{
Expand Down Expand Up @@ -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 }
]
},
{
Expand All @@ -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 }
]
},

Expand All @@ -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 }
]
},
{
Expand Down

0 comments on commit 18a0b0e

Please sign in to comment.