Allow `brace-style` rule to enforce different styles on different block types
See original GitHub issueWhat version of ESLint are you using? v3.1.0
What rule do you want to change? brace-style
I would like to enable different brace handling based on the type of block. For example, different handling for IfStatements vs FunctionDeclaration (and even FunctionExpression).
Ideally you would be able to use individual options to mimic the predefined formats (
{
"brace-style": ["error", { "IfStatement": "new-line", "FunctionDeclaration": "new-line", "FunctionExpression": "same-line" }, { "noCuddledElse": true }]
}
BlocksSupported:
FunctionDeclaration
FunctionExpression
ArrowFunctionExpression
IfStatement
TryStatement
DoWhileStatement
WhileStatement
WithStatement
ForStatement
ForInStatement
ForOfStatement
SwitchStatement
Options:
new-line
- brace always starts on a new line
same-line
- brace always starts on same line
ignore
- rule does not apply
Additional options to support “stroustrup” style:
noCuddledElse
- else keywords start on own line
noCuddledCatchFinally
- catch/finally keywords start on own line
What code should be flagged as incorrect with this change?
With this enabled, the following would be an error:
function doSomething()
{
// function body
}
if (true) {
// body
} else {
}
invokeCallback(function()
{
});
What happens when the rule is applied to this code now?
The brace variations do match the flexibility defined. I feel like i’m fighting with my tooling (VSCode) when using the brace-style rule and would like the possibility of adding additional properties for the rule to better fit within our preferred code formatting and development tooling.
Issue Analytics
- State:
- Created 7 years ago
- Comments:38 (23 by maintainers)
@jrsearles Sorry for losing track of this, but where are we here? Have I missed your PR?
I really want to see this implemented too, as the current
brace-style
implementation is not nearly granular enough. My style is mostly Stroustrup, but for class, function and method declarations I use the Allman style with the braces on their own line. There’s no way to express this to ESLint so I’m currently forced to disable thebrace-style
rule entirely.