@saji/eslint-plugin-brace-rules v0.2.1
@saji/eslint-plugin-brace-rules
Enhancements to eslint's default brace-style rules
Originally created by Joshua Searles with modifications by Jason Keimig.
Installation
You'll first need to install ESLint:
$ npm i eslint --save-devNext, install @saji/eslint-plugin-brace-rules:
$ npm install @saji/eslint-plugin-brace-rules --save-devNote: If you installed ESLint globally (using the -g flag) then you must also install @saji/eslint-plugin-brace-rules globally.
Usage
Add brace-rules to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:
{
"plugins": [
"@saji/brace-rules"
]
}Then, modify your .eslintrc config with the following updates:
- Disable the internal
brace-stylesetting - 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-stylechecks are disabled - All
@saji/brace-rules/brace-on-same-lineviolations are flagged as errors - The baseline brace styling is
stroustrup brace on next-lineoverrides are added for occurances of:class { ... }export class MyClass { ... }export default class MyClass { ... }export default class { ... }export default function { ... }
{
"rules": {
...,
"brace-style": 0,
"@saji/brace-rules/brace-on-same-line": [
"error",
"stroustrup",
{
"ClassDeclaration": "never",
"ExportClass": "never",
"ExportClassAnon": "never",
"ExportFunctionAnon": "never"
}
],
...
}
}Supported Rules
The basic schema for the brance-on-same-line is as follows:
"@saji/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 theelsekeyword to be on the same line as the closing brace of the preceedingifclause.noCuddledCatchFinally: Don't allowcatchorfinallykeywords to be on the same line as the closing brace of the preceedingtryandcatchclauses (respectively).
braceRuleProps
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
WithStatementrule is for formatting braces around with statements"
... but a few related to export need additional detail:
ArrowFunctionExpressionClassDeclarationDoWhileStatementExportClass: Adjusts brace formatting forexport class MyClassorexport default class MyClassdeclarations (overriding anyClassDeclarationsetting)ExportClassAnon: Adjusts brace formatting forexport default classdeclarations (overriding anyClassDeclarationandExportClasssettings)ExportFunction: Adjusts brace formatting forexport function MyFunctionorexport default function MyFunctiondeclarations (overriding anyFunctionDeclarationsetting)ExportFunctionAnon: Adjusts brace formatting forexport default functiondeclarations (overriding anyFunctionDeclarationandExportFunctionsettings)ForInStatementForOfStatementForStatementFunctionDeclarationFunctionExpressionIfStatementMethodDeclaration: Adjusts brace formatting for class method declarationSwitchStatementTryStatementWhileStatementWithStatement
6 years ago