Skip to content

Commit e01dc5f

Browse files
a-tarasyukbradzacher
authored andcommitted
feat(eslint-plugin): add brace-style [extension] (#810)
1 parent e9fcf70 commit e01dc5f

File tree

8 files changed

+1213
-0
lines changed

8 files changed

+1213
-0
lines changed

packages/eslint-plugin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e
145145
| [`@typescript-eslint/await-thenable`](./docs/rules/await-thenable.md) | Disallows awaiting a value that is not a Thenable | :heavy_check_mark: | | :thought_balloon: |
146146
| [`@typescript-eslint/ban-ts-ignore`](./docs/rules/ban-ts-ignore.md) | Bans “// @ts-ignore” comments from being used | :heavy_check_mark: | | |
147147
| [`@typescript-eslint/ban-types`](./docs/rules/ban-types.md) | Bans specific types from being used | :heavy_check_mark: | :wrench: | |
148+
| [`@typescript-eslint/brace-style`](./docs/rules/brace-style.md) | Enforce consistent brace style for blocks | | :wrench: | |
148149
| [`@typescript-eslint/camelcase`](./docs/rules/camelcase.md) | Enforce camelCase naming convention | :heavy_check_mark: | | |
149150
| [`@typescript-eslint/class-name-casing`](./docs/rules/class-name-casing.md) | Require PascalCased class and interface names | :heavy_check_mark: | | |
150151
| [`@typescript-eslint/consistent-type-assertions`](./docs/rules/consistent-type-assertions.md) | Enforces consistent usage of type assertions. | :heavy_check_mark: | | |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Enforce consistent brace style for blocks
2+
3+
## Rule Details
4+
5+
This rule extends the base [eslint/brace-style](https://eslint.org/docs/rules/brace-style) rule.
6+
It supports all options and features of the base rule.
7+
8+
## How to use
9+
10+
```cjson
11+
{
12+
// note you must disable the base rule as it can report incorrect errors
13+
"brace-style": "off",
14+
"@typescript-eslint/brace-style": ["error"]
15+
}
16+
```
17+
18+
## Options
19+
20+
See [eslint/brace-style options](https://eslint.org/docs/rules/brace-style#options).
21+
22+
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/brace-style.md)</sup>

packages/eslint-plugin/src/configs/all.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"@typescript-eslint/await-thenable": "error",
77
"@typescript-eslint/ban-ts-ignore": "error",
88
"@typescript-eslint/ban-types": "error",
9+
"brace-style": "off",
10+
"@typescript-eslint/brace-style": "error",
911
"camelcase": "off",
1012
"@typescript-eslint/camelcase": "error",
1113
"@typescript-eslint/class-name-casing": "error",
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {
2+
TSESTree,
3+
AST_NODE_TYPES,
4+
} from '@typescript-eslint/experimental-utils';
5+
import baseRule from 'eslint/lib/rules/brace-style';
6+
import * as util from '../util';
7+
8+
export type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
9+
export type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;
10+
11+
export default util.createRule<Options, MessageIds>({
12+
name: 'brace-style',
13+
meta: {
14+
type: 'layout',
15+
docs: {
16+
description: 'Enforce consistent brace style for blocks',
17+
category: 'Stylistic Issues',
18+
recommended: false,
19+
},
20+
messages: baseRule.meta.messages,
21+
fixable: baseRule.meta.fixable,
22+
schema: baseRule.meta.schema,
23+
},
24+
defaultOptions: ['1tbs'],
25+
create(context) {
26+
const rules = baseRule.create(context);
27+
const checkBlockStatement = (
28+
node: TSESTree.TSModuleBlock | TSESTree.TSInterfaceBody,
29+
): void => {
30+
rules.BlockStatement({
31+
type: AST_NODE_TYPES.BlockStatement,
32+
parent: node.parent,
33+
range: node.range,
34+
body: node.body as any, // eslint-disable-line @typescript-eslint/no-explicit-any
35+
loc: node.loc,
36+
});
37+
};
38+
39+
return {
40+
...rules,
41+
TSInterfaceBody: checkBlockStatement,
42+
TSModuleBlock: checkBlockStatement,
43+
};
44+
},
45+
});

packages/eslint-plugin/src/rules/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import arrayType from './array-type';
33
import awaitThenable from './await-thenable';
44
import banTsIgnore from './ban-ts-ignore';
55
import banTypes from './ban-types';
6+
import braceStyle from './brace-style';
67
import camelcase from './camelcase';
78
import classNameCasing from './class-name-casing';
89
import consistentTypeAssertions from './consistent-type-assertions';
@@ -67,6 +68,7 @@ export default {
6768
'await-thenable': awaitThenable,
6869
'ban-ts-ignore': banTsIgnore,
6970
'ban-types': banTypes,
71+
'brace-style': braceStyle,
7072
camelcase: camelcase,
7173
'class-name-casing': classNameCasing,
7274
'consistent-type-assertions': consistentTypeAssertions,

0 commit comments

Comments
 (0)