-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from iancmyers/rule-one-true-brace
Rule: One True Brace Style
- Loading branch information
Showing
4 changed files
with
426 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
"no-new": 1, | ||
|
||
"smarter-eqeqeq": 0, | ||
"brace-style": 0, | ||
"camelcase": 1, | ||
"curly": 1, | ||
"eqeqeq": 1, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
|
||
}; |
Oops, something went wrong.