forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block-spacing.test.ts
92 lines (86 loc) · 2.74 KB
/
block-spacing.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import rule from '../../src/rules/block-spacing';
import type { InvalidTestCase, ValidTestCase } from '../RuleTester';
import { RuleTester } from '../RuleTester';
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
});
type InvalidBlockSpacingTestCase = InvalidTestCase<
'missing' | 'extra',
['always' | 'never']
>;
const options = ['always', 'never'] as const;
const typeDeclaration = [
{
nodeType: AST_NODE_TYPES.TSInterfaceBody,
stringPrefix: 'interface Foo ',
},
{
nodeType: AST_NODE_TYPES.TSTypeLiteral,
stringPrefix: 'type Foo = ',
},
{
nodeType: AST_NODE_TYPES.TSEnumDeclaration,
stringPrefix: 'enum Foo ',
},
{
nodeType: AST_NODE_TYPES.TSEnumDeclaration,
stringPrefix: 'const enum Foo ',
},
];
const emptyBlocks = ['{}', '{ }'];
const singlePropertyBlocks = ['{bar: true}', '{ bar: true }'];
ruleTester.run('block-spacing', rule, {
valid: [
// Empty blocks don't apply
...options.flatMap(option =>
typeDeclaration.flatMap(typeDec =>
emptyBlocks.map<ValidTestCase<['always' | 'never']>>(blockType => ({
code: typeDec.stringPrefix + blockType,
options: [option],
})),
),
),
],
invalid: [
...options.flatMap(option =>
typeDeclaration.flatMap(typeDec => {
return singlePropertyBlocks.flatMap<InvalidBlockSpacingTestCase>(
(blockType, blockIndex) => {
// These are actually valid, so filter them out
if (
(option === 'always' && blockType.startsWith('{ ')) ||
(option === 'never' && blockType.startsWith('{bar'))
) {
return [];
}
const reverseBlockType = singlePropertyBlocks[1 - blockIndex];
let code = `${typeDec.stringPrefix}${blockType}; /* ${option} */`;
let output = `${typeDec.stringPrefix}${reverseBlockType}; /* ${option} */`;
if (typeDec.nodeType === AST_NODE_TYPES.TSEnumDeclaration) {
output = output.replace(':', '=');
code = code.replace(':', '=');
}
return {
code,
options: [option],
output,
errors: [
{
type: typeDec.nodeType,
messageId: option === 'always' ? 'missing' : 'extra',
data: { location: 'after', token: '{' },
},
{
type: typeDec.nodeType,
messageId: option === 'always' ? 'missing' : 'extra',
data: { location: 'before', token: '}' },
},
],
};
},
);
}),
),
],
});