Skip to content
Permalink
Browse files

Update: Implement auto fix for space-in-parens (fixes #5050)

  • Loading branch information...
alberto committed Jan 24, 2016
1 parent 11435af commit efeef422defeee65347830bb3695218c15c4156a
Showing with 87 additions and 5 deletions.
  1. +1 −1 docs/rules/README.md
  2. +2 −0 docs/rules/space-in-parens.md
  3. +33 −4 lib/rules/space-in-parens.js
  4. +51 −0 tests/lib/rules/space-in-parens.js
@@ -208,7 +208,7 @@ These rules are purely matters of style and are quite subjective.
* [sort-imports](sort-imports.md) - sort import declarations within module
* [space-before-blocks](space-before-blocks.md) - require or disallow a space before blocks (fixable)
* [space-before-function-paren](space-before-function-paren.md) - require or disallow a space before function opening parenthesis (fixable)
* [space-in-parens](space-in-parens.md) - require or disallow spaces inside parentheses
* [space-in-parens](space-in-parens.md) - require or disallow spaces inside parentheses (fixable)
* [space-infix-ops](space-infix-ops.md) - require spaces around operators (fixable)
* [space-unary-ops](space-unary-ops.md) - require or disallow spaces before/after unary operators (fixable)
* [spaced-comment](spaced-comment.md) - require or disallow a space immediately following the `//` or `/*` in a comment
@@ -10,6 +10,8 @@ foo('bar');
var x = (1 + 2) * 3;
```

**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.

## Rule Details

This rule will enforce consistency of spacing directly inside of parentheses, by disallowing or requiring one or more spaces to the right of `(` and to the left of `)`. In either case, `()` will still be allowed.
@@ -205,13 +205,42 @@ module.exports = function(context) {
}

if (token.value === "(" && shouldOpenerHaveSpace(token, nextToken)) {
context.report(node, token.loc.start, MISSING_SPACE_MESSAGE);
context.report({
node: node,
loc: token.loc.start,
message: MISSING_SPACE_MESSAGE,
fix: function(fixer) {
return fixer.insertTextAfter(token, " ");
}
});
} else if (token.value === "(" && shouldOpenerRejectSpace(token, nextToken)) {
context.report(node, token.loc.start, REJECTED_SPACE_MESSAGE);
context.report({
node: node,
loc: token.loc.start,
message: REJECTED_SPACE_MESSAGE,
fix: function(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);
// context.report(node, token.loc.start, MISSING_SPACE_MESSAGE);
context.report({
node: node,
loc: token.loc.start,
message: MISSING_SPACE_MESSAGE,
fix: function(fixer) {
return fixer.insertTextBefore(token, " ");
}
});
} else if (token.value === ")" && shouldCloserRejectSpace(prevToken, token)) {
context.report(node, token.loc.start, REJECTED_SPACE_MESSAGE);
context.report({
node: node,
loc: token.loc.start,
message: REJECTED_SPACE_MESSAGE,
fix: function(fixer) {
return fixer.removeRange([prevToken.range[1], token.range[0]]);
}
});
}
});
}
@@ -117,16 +117,19 @@ ruleTester.run("space-in-parens", rule, {
invalid: [
{
code: "foo( )",
output: "foo()",
options: ["never"],
errors: [{message: REJECTED_SPACE_ERROR, line: 1, column: 4}]
},
{
code: "foo( bar)",
output: "foo( bar )",
options: ["always"],
errors: [{message: MISSING_SPACE_ERROR, line: 1, column: 9}]
},
{
code: "foo(bar)",
output: "foo( bar )",
options: ["always"],
errors: [
{message: MISSING_SPACE_ERROR, line: 1, column: 4},
@@ -135,26 +138,31 @@ ruleTester.run("space-in-parens", rule, {
},
{
code: "var x = ( 1 + 2) * 3",
output: "var x = ( 1 + 2 ) * 3",
options: ["always"],
errors: [{message: MISSING_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},
@@ -163,140 +171,167 @@ ruleTester.run("space-in-parens", rule, {
},
{
code: "var x = ( 4 + 5) * 6",
output: "var x = (4 + 5) * 6",
options: ["never"],
errors: [REJECTED_SPACE_ERROR]
},
{
code: "var x = (4 + 5 ) * 6",
output: "var x = (4 + 5) * 6",
options: ["never"],
errors: [REJECTED_SPACE_ERROR]
},

// comments
{
code: "foo(/* bar */)",
output: "foo( /* bar */ )",
options: ["always"],
errors: [MISSING_SPACE_ERROR, MISSING_SPACE_ERROR]
},
{
code: "foo(/* bar */baz )",
output: "foo( /* bar */baz )",
options: ["always"],
errors: [MISSING_SPACE_ERROR]
},
{
code: "foo(/* bar */ baz )",
output: "foo( /* bar */ baz )",
options: ["always"],
errors: [MISSING_SPACE_ERROR]
},
{
code: "foo( baz/* bar */)",
output: "foo( baz/* bar */ )",
options: ["always"],
errors: [MISSING_SPACE_ERROR]
},
{
code: "foo( baz /* bar */)",
output: "foo( baz /* bar */ )",
options: ["always"],
errors: [MISSING_SPACE_ERROR]
},
{
code: "foo( /* bar */ )",
output: "foo(/* bar */)",
options: ["never"],
errors: [REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR]
},
{
code: "foo( /* bar */ baz)",
output: "foo(/* bar */ baz)",
options: ["never"],
errors: [{message: REJECTED_SPACE_ERROR, line: 1, column: 4}]
},

// exceptions
{
code: "foo({ bar: 'baz' })",
output: "foo( { bar: 'baz' } )",
options: ["always", { exceptions: ["[]"] }],
errors: [MISSING_SPACE_ERROR, MISSING_SPACE_ERROR]
},
{
code: "foo( { bar: 'baz' } )",
output: "foo({ bar: 'baz' })",
options: ["always", { exceptions: ["{}"] }],
errors: [REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR]
},
{
code: "foo({ bar: 'baz' })",
output: "foo( { bar: 'baz' } )",
options: ["never", { exceptions: ["{}"] }],
errors: [MISSING_SPACE_ERROR, MISSING_SPACE_ERROR]
},
{
code: "foo( { bar: 'baz' } )",
output: "foo({ bar: 'baz' })",
options: ["never", { exceptions: ["[]"] }],
errors: [REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR]
},
{
code: "foo( { bar: 'baz' })",
output: "foo({ bar: 'baz' })",
options: ["always", { exceptions: ["{}"] }],
errors: [REJECTED_SPACE_ERROR]
},
{
code: "foo( { bar: 'baz' })",
output: "foo( { bar: 'baz' } )",
options: ["never", { exceptions: ["{}"] }],
errors: [MISSING_SPACE_ERROR]
},
{
code: "foo({ bar: 'baz' } )",
output: "foo({ bar: 'baz' })",
options: ["always", { exceptions: ["{}"] }],
errors: [REJECTED_SPACE_ERROR]
},
{
code: "foo({ bar: 'baz' } )",
output: "foo( { bar: 'baz' } )",
options: ["never", { exceptions: ["{}"] }],
errors: [MISSING_SPACE_ERROR]
},
{
code: "foo([ 1, 2 ])",
output: "foo( [ 1, 2 ] )",
options: ["always", { exceptions: ["empty"] }],
errors: [MISSING_SPACE_ERROR, MISSING_SPACE_ERROR]
},
{
code: "foo( [ 1, 2 ] )",
output: "foo([ 1, 2 ])",
options: ["always", { exceptions: ["[]"] }],
errors: [REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR]
},
{
code: "foo([ 1, 2 ])",
output: "foo( [ 1, 2 ] )",
options: ["never", { exceptions: ["[]"] }],
errors: [MISSING_SPACE_ERROR, MISSING_SPACE_ERROR]
},
{
code: "foo( [ 1, 2 ] )",
output: "foo([ 1, 2 ])",
options: ["never", { exceptions: ["()"] }],
errors: [REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR]
},
{
code: "foo([ 1, 2 ] )",
output: "foo([ 1, 2 ])",
options: ["always", { exceptions: ["[]"] }],
errors: [REJECTED_SPACE_ERROR]
},
{
code: "foo([ 1, 2 ] )",
output: "foo( [ 1, 2 ] )",
options: ["never", { exceptions: ["[]"] }],
errors: [MISSING_SPACE_ERROR]
},
{
code: "foo( [ 1, 2 ])",
output: "foo([ 1, 2 ])",
options: ["always", { exceptions: ["[]"] }],
errors: [REJECTED_SPACE_ERROR]
},
{
code: "foo( [ 1, 2 ])",
output: "foo( [ 1, 2 ] )",
options: ["never", { exceptions: ["[]"] }],
errors: [MISSING_SPACE_ERROR]
},
{
code: "(( 1 + 2 ))",
output: "( ( 1 + 2 ) )",
options: ["always", { exceptions: ["[]"] }],
errors: [MISSING_SPACE_ERROR, MISSING_SPACE_ERROR]
},
{
code: "( ( 1 + 2 ) )",
output: "(( 1 + 2 ))",
options: ["always", { exceptions: ["()"] }],
errors: [
{message: REJECTED_SPACE_ERROR, line: 1, column: 1},
@@ -305,11 +340,13 @@ ruleTester.run("space-in-parens", rule, {
},
{
code: "(( 1 + 2 ))",
output: "( ( 1 + 2 ) )",
options: ["always", { exceptions: ["[]"] }],
errors: [MISSING_SPACE_ERROR, MISSING_SPACE_ERROR]
},
{
code: "( ( 1 + 2 ) )",
output: "((1 + 2))",
options: ["never"],
errors: [
{message: REJECTED_SPACE_ERROR, line: 1, column: 1},
@@ -320,72 +357,86 @@ ruleTester.run("space-in-parens", rule, {
},
{
code: "( ( 1 + 2 ) )",
output: "((1 + 2))",
options: ["never", { exceptions: ["[]"] }],
errors: [REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR, REJECTED_SPACE_ERROR]
},
{
code: "( ( 1 + 2 ))",
output: "(( 1 + 2 ))",
options: ["always", { exceptions: ["()"] }],
errors: [{message: REJECTED_SPACE_ERROR, line: 1, column: 1}]
},
{
code: "( (1 + 2))",
output: "( (1 + 2) )",
options: ["never", { exceptions: ["()"] }],
errors: [MISSING_SPACE_ERROR]
},
{
code: "(( 1 + 2 ) )",
output: "(( 1 + 2 ))",
options: ["always", { exceptions: ["()"] }],
errors: [REJECTED_SPACE_ERROR]
},
{
code: "((1 + 2) )",
output: "( (1 + 2) )",
options: ["never", { exceptions: ["()"] }],
errors: [MISSING_SPACE_ERROR]
},
{
code: "var result = ( 1 / ( 1 + 2 ) ) + 3",
output: "var result = ( 1 / ( 1 + 2 )) + 3",
options: ["always", { exceptions: ["()"] }],
errors: [REJECTED_SPACE_ERROR]
},
{
code: "var result = (1 / (1 + 2)) + 3",
output: "var result = (1 / (1 + 2) ) + 3",
options: ["never", { exceptions: ["()"] }],
errors: [MISSING_SPACE_ERROR]
},
{
code: "var result = ( 1 / ( 1 + 2)) + 3",
output: "var result = ( 1 / ( 1 + 2 )) + 3",
options: ["always", { exceptions: ["()"] }],
errors: [MISSING_SPACE_ERROR]
},
{
code: "var result = (1 / (1 + 2)) + 3",
output: "var result = (1 / (1 + 2) ) + 3",
options: ["never", { 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}]
},
{
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}]
},
{
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}]

0 comments on commit efeef42

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