Enhancements to eslint's default brace-style
rules
You'll first need to install ESLint:
$ npm i eslint --save-dev
Next, install eslint-plugin-brace-rules
:
$ npm install eslint-plugin-brace-rules --save-dev
Note: If you installed ESLint globally (using the -g
flag) then you must also install eslint-plugin-brace-rules
globally.
Add brace-rules
to the plugins section of your .eslintrc
configuration file. You can omit the eslint-plugin-
prefix:
{
"plugins": [
"brace-rules"
]
}
Then, modify your .eslintrc
config with the following updates:
- Disable the internal
brace-style
setting - Set the default base rule-set that is close to your desired brace-style settings
- Add only the overrides you need from the list of braceRuleProps
In the example below:
- All internal
brace-style
checks are disabled - All
brace-rules/brace-on-same-line
violations are flagged as errors - The baseline brace styling is
stroustrup
brace on next-line
overrides are added for occurances of:class { ... }
export class MyClass { ... }
export default class MyClass { ... }
export default class { ... }
export default function { ... }
{
"rules": {
...,
"brace-style": 0,
"brace-rules/brace-on-same-line": [
"error",
"stroustrup",
{
"ClassDeclaration": "never",
"ExportClass": "never",
"ExportClassAnon": "never",
"ExportFunctionAnon": "never"
}
],
...
}
}
The basic schema for the brance-on-same-line
is as follows:
"brace-rules/brace-on-same-line": [
${standard eslint error levels, int/string-based},
(( schema ))
{
oneOf: [
{ enum: ["1tbs", "stroustrup", "allman"] },
{
type: "object",
properties: { ...braceRuleProps }
}
]
},
{
type: "object",
properties:
...braceRuleProps,
allowSingleLine: { type: "boolean" },
noCuddledElse: { type: "boolean" },
noCuddledCatchFinally: { type: "boolean" }
}),
additionalProperties: false
}
(( end schema ))
]
The anticipated flow here is to start with a base brace-style ruleset (i.e. stroustrup
), and override it with specific rules (as shown in the example from the Usage section, above).
You can also completely define your brace-style rules on a per-keyword basis by specifying them all in the section following the error level. No additional braceStyle properties would need to be added in the third section, which overrides anything specified in the second section.
Properties that can be defined in the third section:
...braceRuleProps
: See below in braceRuleProps for full detailsallowSingleLine
: Allow braces on single-line declarationsif (foo) { return true; }
noCuddledElse
: Don't allow theelse
keyword to be on the same line as the closing brace of the preceedingif
clause.noCuddledCatchFinally
: Don't allowcatch
orfinally
keywords to be on the same line as the closing brace of the preceedingtry
andcatch
clauses (respectively).
Below is the list of all brace rules that can be used. They all accept one of three values:
always
: Always require an opening brace on the same line as the keywordnever
: Never allow an opening brace on the same line as the keywordignore
: Ignore the bracing style for this keyword
Most of the brace rules are self-explanatary:
"The
WithStatement
rule is for formatting braces around with statements"
... but a few related to export
need additional detail:
ArrowFunctionExpression
ClassDeclaration
DoWhileStatement
ExportClass
: Adjusts brace formatting forexport class MyClass
orexport default class MyClass
declarations (overriding anyClassDeclaration
setting)ExportClassAnon
: Adjusts brace formatting forexport default class
declarations (overriding anyClassDeclaration
andExportClass
settings)ExportFunction
: Adjusts brace formatting forexport function MyFunction
orexport default function MyFunction
declarations (overriding anyFunctionDeclaration
setting)ExportFunctionAnon
: Adjusts brace formatting forexport default function
declarations (overriding anyFunctionDeclaration
andExportFunction
settings)ForStatement
ForInStatement
ForOfStatement
FunctionDeclaration
FunctionExpression
IfStatement
SwitchStatement
TryStatement
WhileStatement
WithStatement