Skip to content

Commit

Permalink
Merge pull request #73 from iancmyers/rule-one-true-brace
Browse files Browse the repository at this point in the history
Rule: One True Brace Style
  • Loading branch information
nzakas committed Jul 18, 2013
2 parents 3022ce2 + 725d841 commit 5266964
Show file tree
Hide file tree
Showing 4 changed files with 426 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"no-new": 1,

"smarter-eqeqeq": 0,
"brace-style": 0,
"camelcase": 1,
"curly": 1,
"eqeqeq": 1,
Expand Down
63 changes: 63 additions & 0 deletions docs/brace-style.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# brace style

One true brace style is a common coding style in JavaScript, in which the opening curly brace of a block is placed on the same line as its corresponding statement or declaration.

```js
function foo()
{
return true;
}

if (foo)
{
bar();
}
```

## Rule Details

This rule is aimed at enforcing one true brace style across your JavaScript. As such, it warns whenever it sees a statement or declaration that does not adhere to the one true brace style.

The following patterns are considered warnings:

```js
function foo()
{
return true;
}

if (foo)
{
bar();
}

try
{
somethingRisky();
} catch(e)
{
handleError();
}
```

The following patterns adhere to one true brace style and do not cause warnings:

```js
function foo() {
return true;
}

if (foo) {
bar();
}

try {
somethingRisky();
} catch(e) {
handleError();
}
```

## When Not To Use It

If your project will not be using the one true brace style, turn this rule off.
57 changes: 57 additions & 0 deletions lib/rules/brace-style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @fileoverview Rule to flag block statements that do not use the one true brace style
* @author Ian Christian Myers
*/

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {

//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------

function checkBlockStartsAtIdentifier(node) {
var startLine = node.loc.start.line;

// Checks for opening curly brace on FunctionDeclarations.
if (node.body && startLine !== node.body.loc.start.line) {
context.report(node, "Opening curly brace does not appear on the same line as the block identifier.");
}

// Checks for opening curly brace on IfStatement, DoWhileStatement,
// WhileStatement, WithStatement, ForStatement, and ForInStatement.
if (node.consequent && startLine !== node.consequent.loc.start.line) {
context.report(node, "Opening curly brace does not appear on the same line as the block identifier.");
}

// Checks for opening curly brace on TryStatement.
if (node.block && startLine !== node.block.loc.start.line) {
context.report(node, "Opening curly brace does not appear on the same line as the block identifier.");
}

// Checks for opening curly on SwitchStatement.
if (node.discriminant && startLine !== node.cases[0].loc.start.line - 1) {
context.report(node, "Opening curly brace does not appear on the same line as the block identifier.");
}
}

//--------------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------------

return {
"FunctionDeclaration": checkBlockStartsAtIdentifier,
"IfStatement": checkBlockStartsAtIdentifier,
"SwitchStatement": checkBlockStartsAtIdentifier,
"TryStatement": checkBlockStartsAtIdentifier,
"DoWhileStatement": checkBlockStartsAtIdentifier,
"WhileStatement": checkBlockStartsAtIdentifier,
"WithStatement": checkBlockStartsAtIdentifier,
"ForStatement": checkBlockStartsAtIdentifier,
"ForInStatement": checkBlockStartsAtIdentifier
};

};
Loading

0 comments on commit 5266964

Please sign in to comment.