-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
sort-interface-members.ts
53 lines (46 loc) · 1.34 KB
/
sort-interface-members.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
/**
* @fileoverview Forbids unsorted interface members
*/
import { TSESTree } from '../../../typescript-estree/dist/parser';
import * as util from '../util';
import {
Identifier,
TSPropertySignature
} from '@typescript-eslint/typescript-estree/dist/ts-estree/ts-estree';
type Options = [];
type MessageIds = 'notSorted';
export default util.createRule<Options, MessageIds>({
name: 'sort-interface-members',
meta: {
type: 'suggestion',
docs: {
description: 'Forbids unsorted interface members',
category: 'Stylistic Issues',
recommended: false
},
schema: [],
messages: {
notSorted: 'The interface members are not sorted alphabetically.'
}
},
defaultOptions: [],
create(context) {
return {
TSInterfaceBody(node: TSESTree.TSInterfaceBody) {
const members = node.body as TSPropertySignature[];
for (let i = 0; i < members.length - 1; i++) {
const currentItemKey = members[i].key as Identifier;
const nextItemKey = members[i + 1].key as Identifier;
if (currentItemKey.name > nextItemKey.name) {
context.report({
messageId: 'notSorted',
node: currentItemKey
});
return; // Leave loop immediately
}
}
return; // No rule violation found
}
};
}
});