Skip to content

Commit

Permalink
Update: Implement auto fix for block-spacing rule (fixes #3859)
Browse files Browse the repository at this point in the history
  • Loading branch information
alberto committed Sep 22, 2015
1 parent 11114c8 commit fbbd3b0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/rules/README.md
Expand Up @@ -138,7 +138,7 @@ These rules are specific to JavaScript running on Node.js or using CommonJS in t
These rules are purely matters of style and are quite subjective.

* [array-bracket-spacing](array-bracket-spacing.md) - enforce spacing inside array brackets
* [block-spacing](block-spacing.md) - disallow or enforce spaces inside of single line blocks
* [block-spacing](block-spacing.md) - disallow or enforce spaces inside of single line blocks (fixable)
* [brace-style](brace-style.md) - enforce one true brace style
* [camelcase](camelcase.md) - require camel case names
* [comma-spacing](comma-spacing.md) - enforce spacing before and after comma
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/block-spacing.md
Expand Up @@ -2,6 +2,8 @@

This rule is for spacing style within single line blocks.

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

## Rule Details

This rule is aimed to flag usage of spacing inside of blocks.
Expand Down
26 changes: 24 additions & 2 deletions lib/rules/block-spacing.js
Expand Up @@ -74,10 +74,32 @@ module.exports = function(context) {

// Check.
if (!isValid(openBrace, firstToken)) {
context.report(node, openBrace.loc.start, message + " after \"{\".");
context.report({
node: node,
loc: openBrace.loc.start,
message: message + " after \"{\".",
fix: function(fixer) {
if (always) {
return fixer.insertTextBefore(firstToken, " ");
}

return fixer.removeRange([openBrace.range[1], firstToken.range[0]]);
}
});
}
if (!isValid(lastToken, closeBrace)) {
context.report(node, closeBrace.loc.start, message + " before \"}\".");
context.report({
node: node,
loc: closeBrace.loc.start,
message: message + " before \"}\".",
fix: function(fixer) {
if (always) {
return fixer.insertTextAfter(lastToken, " ");
}

return fixer.removeRange([lastToken.range[1], closeBrace.range[0]]);
}
});
}
}

Expand Down
35 changes: 35 additions & 0 deletions tests/lib/rules/block-spacing.js
Expand Up @@ -63,6 +63,7 @@ ruleTester.run("block-spacing", rule, {
// default/always
{
code: "{foo();}",
output: "{ foo(); }",
options: ["always"],
errors: [
{type: "BlockStatement", line: 1, column: 1, message: "Requires a space after \"{\"."},
Expand All @@ -71,86 +72,99 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "{foo();}",
output: "{ foo(); }",
errors: [
{type: "BlockStatement", line: 1, column: 1, message: "Requires a space after \"{\"."},
{type: "BlockStatement", line: 1, column: 8, message: "Requires a space before \"}\"."}
]
},
{
code: "{ foo();}",
output: "{ foo(); }",
errors: [
{type: "BlockStatement", line: 1, column: 9, message: "Requires a space before \"}\"."}
]
},
{
code: "{foo(); }",
output: "{ foo(); }",
errors: [
{type: "BlockStatement", line: 1, column: 1, message: "Requires a space after \"{\"."}
]
},
{
code: "{\nfoo();}",
output: "{\nfoo(); }",
errors: [
{type: "BlockStatement", line: 2, column: 7, message: "Requires a space before \"}\"."}
]
},
{
code: "{foo();\n}",
output: "{ foo();\n}",
errors: [
{type: "BlockStatement", line: 1, column: 1, message: "Requires a space after \"{\"."}
]
},
{
code: "if (a) {foo();}",
output: "if (a) { foo(); }",
errors: [
{type: "BlockStatement", line: 1, column: 8, message: "Requires a space after \"{\"."},
{type: "BlockStatement", line: 1, column: 15, message: "Requires a space before \"}\"."}
]
},
{
code: "if (a) {} else {foo();}",
output: "if (a) {} else { foo(); }",
errors: [
{type: "BlockStatement", line: 1, column: 16, message: "Requires a space after \"{\"."},
{type: "BlockStatement", line: 1, column: 23, message: "Requires a space before \"}\"."}
]
},
{
code: "switch (a) {case 0: foo();}",
output: "switch (a) { case 0: foo(); }",
errors: [
{type: "SwitchStatement", line: 1, column: 12, message: "Requires a space after \"{\"."},
{type: "SwitchStatement", line: 1, column: 27, message: "Requires a space before \"}\"."}
]
},
{
code: "while (a) {foo();}",
output: "while (a) { foo(); }",
errors: [
{type: "BlockStatement", line: 1, column: 11, message: "Requires a space after \"{\"."},
{type: "BlockStatement", line: 1, column: 18, message: "Requires a space before \"}\"."}
]
},
{
code: "do {foo();} while (a);",
output: "do { foo(); } while (a);",
errors: [
{type: "BlockStatement", line: 1, column: 4, message: "Requires a space after \"{\"."},
{type: "BlockStatement", line: 1, column: 11, message: "Requires a space before \"}\"."}
]
},
{
code: "for (;;) {foo();}",
output: "for (;;) { foo(); }",
errors: [
{type: "BlockStatement", line: 1, column: 10, message: "Requires a space after \"{\"."},
{type: "BlockStatement", line: 1, column: 17, message: "Requires a space before \"}\"."}
]
},
{
code: "for (var a in b) {foo();}",
output: "for (var a in b) { foo(); }",
errors: [
{type: "BlockStatement", line: 1, column: 18, message: "Requires a space after \"{\"."},
{type: "BlockStatement", line: 1, column: 25, message: "Requires a space before \"}\"."}
]
},
{
code: "for (var a of b) {foo();}",
output: "for (var a of b) { foo(); }",
ecmaFeatures: {forOf: true},
errors: [
{type: "BlockStatement", line: 1, column: 18, message: "Requires a space after \"{\"."},
Expand All @@ -159,6 +173,7 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "try {foo();} catch (e) {foo();} finally {foo();}",
output: "try { foo(); } catch (e) { foo(); } finally { foo(); }",
errors: [
{type: "BlockStatement", line: 1, column: 5, message: "Requires a space after \"{\"."},
{type: "BlockStatement", line: 1, column: 12, message: "Requires a space before \"}\"."},
Expand All @@ -170,20 +185,23 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "function foo() {bar();}",
output: "function foo() { bar(); }",
errors: [
{type: "BlockStatement", line: 1, column: 16, message: "Requires a space after \"{\"."},
{type: "BlockStatement", line: 1, column: 23, message: "Requires a space before \"}\"."}
]
},
{
code: "(function() {bar();});",
output: "(function() { bar(); });",
errors: [
{type: "BlockStatement", line: 1, column: 13, message: "Requires a space after \"{\"."},
{type: "BlockStatement", line: 1, column: 20, message: "Requires a space before \"}\"."}
]
},
{
code: "(() => {bar();});",
output: "(() => { bar(); });",
ecmaFeatures: {arrowFunctions: true},
errors: [
{type: "BlockStatement", line: 1, column: 8, message: "Requires a space after \"{\"."},
Expand All @@ -195,6 +213,7 @@ ruleTester.run("block-spacing", rule, {
// never
{
code: "{ foo(); }",
output: "{foo();}",
options: ["never"],
errors: [
{type: "BlockStatement", line: 1, column: 1, message: "Unexpected space(s) after \"{\"."},
Expand All @@ -203,34 +222,39 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "{ foo();}",
output: "{foo();}",
options: ["never"],
errors: [
{type: "BlockStatement", line: 1, column: 1, message: "Unexpected space(s) after \"{\"."}
]
},
{
code: "{foo(); }",
output: "{foo();}",
options: ["never"],
errors: [
{type: "BlockStatement", line: 1, column: 9, message: "Unexpected space(s) before \"}\"."}
]
},
{
code: "{\nfoo(); }",
output: "{\nfoo();}",
options: ["never"],
errors: [
{type: "BlockStatement", line: 2, column: 8, message: "Unexpected space(s) before \"}\"."}
]
},
{
code: "{ foo();\n}",
output: "{foo();\n}",
options: ["never"],
errors: [
{type: "BlockStatement", line: 1, column: 1, message: "Unexpected space(s) after \"{\"."}
]
},
{
code: "if (a) { foo(); }",
output: "if (a) {foo();}",
options: ["never"],
errors: [
{type: "BlockStatement", line: 1, column: 8, message: "Unexpected space(s) after \"{\"."},
Expand All @@ -239,6 +263,7 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "if (a) {} else { foo(); }",
output: "if (a) {} else {foo();}",
options: ["never"],
errors: [
{type: "BlockStatement", line: 1, column: 16, message: "Unexpected space(s) after \"{\"."},
Expand All @@ -247,6 +272,7 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "switch (a) { case 0: foo(); }",
output: "switch (a) {case 0: foo();}",
options: ["never"],
errors: [
{type: "SwitchStatement", line: 1, column: 12, message: "Unexpected space(s) after \"{\"."},
Expand All @@ -255,6 +281,7 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "while (a) { foo(); }",
output: "while (a) {foo();}",
options: ["never"],
errors: [
{type: "BlockStatement", line: 1, column: 11, message: "Unexpected space(s) after \"{\"."},
Expand All @@ -263,6 +290,7 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "do { foo(); } while (a);",
output: "do {foo();} while (a);",
options: ["never"],
errors: [
{type: "BlockStatement", line: 1, column: 4, message: "Unexpected space(s) after \"{\"."},
Expand All @@ -271,6 +299,7 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "for (;;) { foo(); }",
output: "for (;;) {foo();}",
options: ["never"],
errors: [
{type: "BlockStatement", line: 1, column: 10, message: "Unexpected space(s) after \"{\"."},
Expand All @@ -279,6 +308,7 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "for (var a in b) { foo(); }",
output: "for (var a in b) {foo();}",
options: ["never"],
errors: [
{type: "BlockStatement", line: 1, column: 18, message: "Unexpected space(s) after \"{\"."},
Expand All @@ -287,6 +317,7 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "for (var a of b) { foo(); }",
output: "for (var a of b) {foo();}",
options: ["never"],
ecmaFeatures: {forOf: true},
errors: [
Expand All @@ -296,6 +327,7 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "try { foo(); } catch (e) { foo(); } finally { foo(); }",
output: "try {foo();} catch (e) {foo();} finally {foo();}",
options: ["never"],
errors: [
{type: "BlockStatement", line: 1, column: 5, message: "Unexpected space(s) after \"{\"."},
Expand All @@ -308,6 +340,7 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "function foo() { bar(); }",
output: "function foo() {bar();}",
options: ["never"],
errors: [
{type: "BlockStatement", line: 1, column: 16, message: "Unexpected space(s) after \"{\"."},
Expand All @@ -316,6 +349,7 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "(function() { bar(); });",
output: "(function() {bar();});",
options: ["never"],
errors: [
{type: "BlockStatement", line: 1, column: 13, message: "Unexpected space(s) after \"{\"."},
Expand All @@ -324,6 +358,7 @@ ruleTester.run("block-spacing", rule, {
},
{
code: "(() => { bar(); });",
output: "(() => {bar();});",
ecmaFeatures: {arrowFunctions: true},
options: ["never"],
errors: [
Expand Down

0 comments on commit fbbd3b0

Please sign in to comment.