Skip to content
Permalink
Browse files

Breaking: `space-before-blocks` ignores after keywords (fixes #1338)

  • Loading branch information...
mysticatea committed Jan 10, 2016
1 parent a4e8312 commit 19a9fbb21fb83b4661a9220427c8c78aa36d0a79
Showing with 25 additions and 40 deletions.
  1. +10 −8 docs/rules/space-before-blocks.md
  2. +5 −4 lib/rules/space-before-blocks.js
  3. +10 −28 tests/lib/rules/space-before-blocks.js
@@ -11,7 +11,8 @@ Having an inconsistent style distracts the reader from seeing the important part

This rule will enforce consistency of spacing before blocks. It is only applied on blocks that don’t begin on a new line.

This rule ignores spacing which is between `=>` and a block. The spacing is handled by the `arrow-spacing` rule.
* This rule ignores spacing which is between `=>` and a block. The spacing is handled by the `arrow-spacing` rule.
* This rule ignores spacing which is between a keyword and a block. The spacing is handled by the `keyword-spacing` rule.

This rule takes one argument. If it is `"always"` then blocks must always have at least one preceding space. If `"never"`
then all blocks should never have any preceding space. If different spacing is desired for function
@@ -33,12 +34,6 @@ if (a){ /*error Missing space before opening brace.*/
b();
}
if (a) {
b();
} else{ /*error Missing space before opening brace.*/
c();
}
function a(){} /*error Missing space before opening brace.*/
for (;;){ /*error Missing space before opening brace.*/
@@ -61,6 +56,13 @@ if (a) {
b();
}
if (a) {
b();
} else{ /*no error. this is checked by `keyword-spacing` rule.*/
c();
}
function a() {}
for (;;) {
@@ -209,6 +211,6 @@ You can turn this rule off if you are not concerned with the consistency of spac

## Related Rules

* [space-after-keywords](space-after-keywords.md)
* [keyword-spacing](keyword-spacing.md)
* [arrow-spacing](arrow-spacing.md)
* [brace-style](brace-style.md)
@@ -30,13 +30,14 @@ module.exports = function(context) {
}

/**
* Checks whether or not a given token is an arrow operator (=>).
* Checks whether or not a given token is an arrow operator (=>) or a keyword
* in order to avoid to conflict with `arrow-spacing` and `keyword-spacing`.
*
* @param {Token} token - A token to check.
* @returns {boolean} `true` if the token is an arrow operator.
*/
function isArrow(token) {
return token.type === "Punctuator" && token.value === "=>";
function isConflicted(token) {
return (token.type === "Punctuator" && token.value === "=>") || token.type === "Keyword";
}

/**
@@ -50,7 +51,7 @@ module.exports = function(context) {
parent,
requireSpace;

if (precedingToken && !isArrow(precedingToken) && astUtils.isTokenOnSameLine(precedingToken, node)) {
if (precedingToken && !isConflicted(precedingToken) && astUtils.isTokenOnSameLine(precedingToken, node)) {
hasSpace = sourceCode.isSpaceBetweenTokens(precedingToken, node);
parent = context.getAncestors().pop();
if (parent.type === "FunctionExpression" || parent.type === "FunctionDeclaration") {
@@ -135,7 +135,13 @@ ruleTester.run("space-before-blocks", rule, {

// https://github.com/eslint/eslint/issues/3769
{code: "()=>{};", options: ["always"], parserOptions: { ecmaVersion: 6 }},
{code: "() => {};", options: ["never"], parserOptions: { ecmaVersion: 6 }}
{code: "() => {};", options: ["never"], parserOptions: { ecmaVersion: 6 }},

// https://github.com/eslint/eslint/issues/1338
{code: "if(a) {}else{}"},
{code: "if(a){}else {}", options: neverArgs},
{code: "try {}catch(a){}", options: functionsOnlyArgs},
{code: "export default class{}", options: classesOnlyArgs, parserOptions: { sourceType: "module" }}
],
invalid: [
{
@@ -173,17 +179,6 @@ ruleTester.run("space-before-blocks", rule, {
errors: [ expectedNoSpacingError ],
output: "if(a){}"
},
{
code: "if(a) {}else{}",
errors: [ expectedSpacingError ],
output: "if(a) {}else {}"
},
{
code: "if(a){}else {}",
options: neverArgs,
errors: [ expectedNoSpacingError ],
output: "if(a){}else{}"
},
{
code: "function a(){}",
errors: [ expectedSpacingError ],
@@ -261,20 +256,14 @@ ruleTester.run("space-before-blocks", rule, {
},
{
code: "try{}catch(a){}",
errors: [ expectedSpacingError, expectedSpacingError ],
output: "try {}catch(a) {}"
errors: [ expectedSpacingError ],
output: "try{}catch(a) {}"
},
{
code: "try {}catch(a) {}",
options: neverArgs,
errors: [ expectedNoSpacingError, expectedNoSpacingError ],
output: "try{}catch(a){}"
},
{
code: "try {}catch(a){}",
options: functionsOnlyArgs,
errors: [ expectedNoSpacingError ],
output: "try{}catch(a){}"
output: "try {}catch(a){}"
},
{
code: "try {} catch(a){}",
@@ -399,13 +388,6 @@ ruleTester.run("space-before-blocks", rule, {
errors: [ expectedNoSpacingError ],
output: "export function a(){}"
},
{
code: "export default class{}",
options: classesOnlyArgs,
parserOptions: { sourceType: "module" },
errors: [ expectedSpacingError ],
output: "export default class {}"
},
{
code: "class test{}",
parserOptions: { ecmaVersion: 6 },

0 comments on commit 19a9fbb

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