@saji/eslint-plugin-brace-rules

0.2.1 • Public • Published

@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-dev

Next, install @saji/eslint-plugin-brace-rules:

$ npm install @saji/eslint-plugin-brace-rules --save-dev

Note: 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-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 @saji/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,
        "@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 details
  • allowSingleLine: Allow braces on single-line declarations
    if (foo) { return true; }
  • noCuddledElse: Don't allow the else keyword to be on the same line as the closing brace of the preceeding if clause.
  • noCuddledCatchFinally: Don't allow catch or finally keywords to be on the same line as the closing brace of the preceeding try and catch clauses (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 keyword
  • never: Never allow an opening brace on the same line as the keyword
  • ignore: 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 for export class MyClass or export default class MyClass declarations (overriding any ClassDeclaration setting)
  • ExportClassAnon: Adjusts brace formatting for export default class declarations (overriding any ClassDeclaration and ExportClass settings)
  • ExportFunction: Adjusts brace formatting for export function MyFunction or export default function MyFunction declarations (overriding any FunctionDeclaration setting)
  • ExportFunctionAnon: Adjusts brace formatting for export default function declarations (overriding any FunctionDeclaration and ExportFunction settings)
  • ForInStatement
  • ForOfStatement
  • ForStatement
  • FunctionDeclaration
  • FunctionExpression
  • IfStatement
  • MethodDeclaration: Adjusts brace formatting for class method declaration
  • SwitchStatement
  • TryStatement
  • WhileStatement
  • WithStatement

Package Sidebar

Install

npm i @saji/eslint-plugin-brace-rules

Weekly Downloads

229

Version

0.2.1

License

ISC

Unpacked Size

22.1 kB

Total Files

5

Last publish

Collaborators

  • saji