Skip to content

Commit e9a4b80

Browse files
authored
feat!: handle TS object-like nodes in object-curly-spacing instead of block-spacing (#822)
* feat(block-spacing)!: stop handling `TSInterfaceBody`, `TSTypeLiteral`, `TSEnumDeclaration` * refactor(object-curly-spacing): simplify validateBraceSpacing parameters * refactor(object-curly-spacing): unify object-like node checks * refactor(object-curly-spacing): simplify object-like node handling * feat(object-curly-spacing): add support for `TSInterfaceBody` and `TSEnumBody` * chore(block-spacing): update type `SupportedNodes` * docs(object-curly-spacing): update
1 parent 48181b5 commit e9a4b80

6 files changed

Lines changed: 143 additions & 236 deletions

File tree

packages/eslint-plugin/rules/block-spacing/block-spacing._ts_.test.ts

Lines changed: 0 additions & 140 deletions
This file was deleted.

packages/eslint-plugin/rules/block-spacing/block-spacing._js_.test.ts renamed to packages/eslint-plugin/rules/block-spacing/block-spacing.test.ts

File renamed without changes.

packages/eslint-plugin/rules/block-spacing/block-spacing.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createRule } from '#utils/create-rule'
44
import { AST_TOKEN_TYPES } from '@typescript-eslint/utils'
55
import { isTokenOnSameLine } from '@typescript-eslint/utils/ast-utils'
66

7-
type SupportedNodes = Tree.BlockStatement | Tree.StaticBlock | Tree.SwitchStatement | Tree.TSInterfaceBody | Tree.TSTypeLiteral | Tree.TSEnumDeclaration
7+
type SupportedNodes = Tree.BlockStatement | Tree.StaticBlock | Tree.SwitchStatement
88

99
export default createRule<RuleOptions, MessageIds>({
1010
name: 'block-spacing',
@@ -145,14 +145,6 @@ export default createRule<RuleOptions, MessageIds>({
145145
BlockStatement: checkSpacingInsideBraces,
146146
StaticBlock: checkSpacingInsideBraces,
147147
SwitchStatement: checkSpacingInsideBraces,
148-
149-
// This code worked "out of the box" for interface and type literal
150-
// Enums were very close to match as well, the only reason they are not is that was that enums don't have a body node in the parser
151-
// So the opening brace punctuator starts in the middle of the node - `getFirstToken` in
152-
// the base rule did not filter for the first opening brace punctuator
153-
TSInterfaceBody: checkSpacingInsideBraces,
154-
TSTypeLiteral: checkSpacingInsideBraces,
155-
TSEnumDeclaration: checkSpacingInsideBraces,
156148
}
157149
},
158150
})

packages/eslint-plugin/rules/object-curly-spacing/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ var { x, y } = y;
2626
// import/export declarations (EcmaScript 6)
2727
import { foo } from "bar";
2828
export { foo };
29+
30+
// type literals
31+
type Foo = { bar: string };
32+
33+
// interface
34+
interface Foo = { bar: string };
35+
36+
// enum
37+
enum Foo = { Bar };
2938
```
3039

3140
## Rule Details
@@ -63,6 +72,10 @@ var obj = { baz: {'foo': 'qux'}, bar};
6372
var obj = {baz: { 'foo': 'qux'}, bar};
6473
var {x } = y;
6574
import { foo } from 'bar';
75+
export { foo };
76+
type Foo = { bar: string };
77+
interface Foo = { bar: string };
78+
enum Foo = { Bar };
6679
```
6780

6881
:::
@@ -86,6 +99,10 @@ var obj = {
8699
var obj = {};
87100
var {x} = y;
88101
import {foo} from 'bar';
102+
export {foo};
103+
type Foo = {bar: string};
104+
interface Foo = {bar: string};
105+
enum Foo = {Bar};
89106
```
90107

91108
:::
@@ -109,6 +126,10 @@ var obj = {
109126
'foo':'bar'};
110127
var {x} = y;
111128
import {foo } from 'bar';
129+
export {foo };
130+
type Foo = {bar: string };
131+
interface Foo = {bar: string };
132+
enum Foo = {Bar };
112133
```
113134

114135
:::
@@ -128,6 +149,10 @@ var obj = {
128149
};
129150
var { x } = y;
130151
import { foo } from 'bar';
152+
export { foo };
153+
type Foo = { bar: string };
154+
interface Foo = { bar: string };
155+
enum Foo = { Bar };
131156
```
132157

133158
:::

packages/eslint-plugin/rules/object-curly-spacing/object-curly-spacing._ts_.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,35 @@ run<RuleOptions, MessageIds>({
808808
code: 'const x:{[key: string]: [number]}',
809809
options: ['always', { arraysInObjects: false }],
810810
},
811+
812+
// default - TSInterfaceBody
813+
{
814+
code: 'interface x {f: number}',
815+
},
816+
// always - TSInterfaceBody
817+
{
818+
code: 'interface x { f: number }',
819+
options: ['always'],
820+
},
821+
// never - TSInterfaceBody
822+
{
823+
code: 'interface x {f: number}',
824+
options: ['never'],
825+
},
826+
// default - TSEnumBody
827+
{
828+
code: 'enum Foo {ONE, TWO,}',
829+
},
830+
// always - TSEnumBody
831+
{
832+
code: 'enum Foo { ONE, TWO = 2 }',
833+
options: ['always'],
834+
},
835+
// never - TSEnumBody
836+
{
837+
code: 'enum Foo {ONE, TWO,}',
838+
options: ['never'],
839+
},
811840
],
812841

813842
invalid: [
@@ -2129,5 +2158,41 @@ run<RuleOptions, MessageIds>({
21292158
{ messageId: 'unexpectedSpaceBefore' },
21302159
],
21312160
},
2161+
// TSInterfaceBody
2162+
{
2163+
code: 'interface x { f: number }',
2164+
output: 'interface x {f: number}',
2165+
errors: [
2166+
{ messageId: 'unexpectedSpaceAfter' },
2167+
{ messageId: 'unexpectedSpaceBefore' },
2168+
],
2169+
},
2170+
{
2171+
code: 'interface x {f: number}',
2172+
output: 'interface x { f: number }',
2173+
options: ['always'],
2174+
errors: [
2175+
{ messageId: 'requireSpaceAfter' },
2176+
{ messageId: 'requireSpaceBefore' },
2177+
],
2178+
},
2179+
// TSEnumBody
2180+
{
2181+
code: 'enum Foo { ONE, TWO = 2 }',
2182+
output: 'enum Foo {ONE, TWO = 2}',
2183+
errors: [
2184+
{ messageId: 'unexpectedSpaceAfter' },
2185+
{ messageId: 'unexpectedSpaceBefore' },
2186+
],
2187+
},
2188+
{
2189+
code: 'enum Foo {ONE, TWO,}',
2190+
output: 'enum Foo { ONE, TWO, }',
2191+
options: ['always'],
2192+
errors: [
2193+
{ messageId: 'requireSpaceAfter' },
2194+
{ messageId: 'requireSpaceBefore' },
2195+
],
2196+
},
21322197
],
21332198
})

0 commit comments

Comments
 (0)