-
Notifications
You must be signed in to change notification settings - Fork 2
/
sort-rules.ts
138 lines (118 loc) · 3.57 KB
/
sort-rules.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {
AST_NODE_TYPES,
ESLintUtils,
TSESTree
} from '@typescript-eslint/experimental-utils';
const getPropertyName = (node: TSESTree.Property): string | null => {
const key = node.key;
switch (key.type) {
case AST_NODE_TYPES.Literal:
return String(key.value);
case AST_NODE_TYPES.TemplateLiteral:
if (key.expressions.length === 0 && key.quasis.length === 1) {
return key.quasis[0].value.cooked;
}
break;
case AST_NODE_TYPES.Identifier:
break;
// no default
}
return null;
};
const trimPluginName = (rule: string): string => {
return rule.substring(rule.indexOf('/'));
};
const isRuleSorted = (a: string, b: string): boolean => {
if (a.includes('/') && b.includes('/')) {
return a <= b;
}
return trimPluginName(a) <= trimPluginName(b);
};
const isPropertyInRulesConfig = (node: TSESTree.Property): boolean =>
Boolean(
node.parent?.parent?.type === AST_NODE_TYPES.Property &&
node.parent.parent.key.type === AST_NODE_TYPES.Identifier &&
node.parent.parent.key.name === 'rules'
);
export = ESLintUtils.RuleCreator(name => name)({
name: __filename,
meta: {
type: 'problem',
docs: {
description: 'Ensures that rules are sorted in a consistent order',
recommended: false
},
fixable: 'code',
messages: {
incorrectOrder:
"Rules should be in ascending order, with core rules last. '{{ thisName }}' should be before '{{ prevName }}'."
},
schema: []
},
defaultOptions: [],
create(context) {
interface Stack {
upper: Stack | null;
prevName: string | null;
prevNode: TSESTree.Node | null;
numKeys: number;
}
// The stack to save the previous property's name for each object literals.
let stack: Stack | null = null;
return {
[AST_NODE_TYPES.ObjectExpression](node: TSESTree.ObjectExpression): void {
stack = {
upper: stack,
prevName: null,
prevNode: null,
numKeys: node.properties.length
};
},
[`${AST_NODE_TYPES.ObjectExpression}:exit`](): void {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
stack = stack!.upper;
},
[AST_NODE_TYPES.SpreadElement](node: TSESTree.SpreadElement): void {
if (node.parent?.type === AST_NODE_TYPES.ObjectExpression && stack) {
stack.prevName = null;
}
},
[AST_NODE_TYPES.Property](node: TSESTree.Property): void {
if (
node.parent?.type === AST_NODE_TYPES.ObjectPattern ||
!isPropertyInRulesConfig(node) ||
!stack
) {
return;
}
const prevName = stack.prevName;
const prevNode = stack.prevNode ?? node;
const thisName = getPropertyName(node);
if (thisName !== null) {
stack.prevName = thisName;
stack.prevNode = node;
}
if (prevName === null || thisName === null) {
return;
}
if (!isRuleSorted(prevName, thisName)) {
context.report({
messageId: 'incorrectOrder',
data: { thisName, prevName },
node,
loc: node.key.loc,
fix(fixer) {
const sourceCode = context.getSourceCode();
const thisText = sourceCode.getText(node);
const prevText = sourceCode.getText(prevNode);
return [
fixer.replaceText(node, prevText),
fixer.replaceText(prevNode, thisText)
];
}
});
}
}
};
}
});