-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_methods.ts
More file actions
198 lines (176 loc) · 5.13 KB
/
Copy pathsort_methods.ts
File metadata and controls
198 lines (176 loc) · 5.13 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {
type MethodDeclaration,
Project,
SyntaxKind,
} from "jsr:@ts-morph/ts-morph@28.0.0";
const paths = Deno.args.length === 0
? [
"client/1_client_generic.ts",
"client/6_client_dispatcher.ts",
"client/6_client.ts",
]
: Deno.args;
const sectionPattern =
/^(\s*\/\/\s*\n\s*\/\/ ========================= (.+) ========================= \/\/\s*\n\s*\/\/\s*\n)/gm;
const sectionMethodTags = new Map([
["CONNECTION", "cn"],
["AUTHORIZATION", "au"],
["LOW-LEVEL", "ll"],
["ACCOUNT", "ac"],
["MESSAGES", "ms"],
["POLLS", "pl"],
["CHECKLISTS", "cl"],
["FILES", "fs"],
["CHATS", "ch"],
["CALLBACK QUERIES", "cq"],
["INLINE QUERIES", "iq"],
["BOTS", "bo"],
["REACTIONS", "re"],
["STORIES", "st"],
["STORY ALBUMS", "sa"],
["NETWORK STATISTICS", "ns"],
["VIDEO CHATS", "vc"],
["PAYMENTS", "pa"],
["CONTACTS", "co"],
["COMMUNITIES", "ct"],
["TRANSLATIONS", "ta"],
["GIFTS", "gf"],
["GIFT COLLECTIONS", "gc"],
["TAKEOUTS", "to"],
["STICKER SETS", "ss"],
["MANAGED BOTS", "mb"],
["GUEST QUERIES", "gq"],
["SECRET CHATS", "sc"],
]);
const project = new Project();
function getMainClass(sourceFile: ReturnType<Project["addSourceFileAtPath"]>) {
const classDeclaration = sourceFile.getClasses()[0];
if (classDeclaration === undefined) {
throw new Error(`No class found in ${sourceFile.getFilePath()}`);
}
return classDeclaration;
}
function getSectionBoundaries(text: string) {
const sections = [...text.matchAll(sectionPattern)].map((match) => ({
contentStart: match.index! + match[1].length,
sectionStart: match.index!,
tag: sectionMethodTags.get(match[2]),
title: match[2],
}));
return sections.map((section, index) => ({
contentEnd: sections[index + 1]?.sectionStart ?? text.length,
contentStart: section.contentStart,
tag: section.tag,
title: section.title,
}));
}
function getMethodSegmentStart(
method: MethodDeclaration,
sectionStart: number,
): number {
return Math.max(method.getFullStart(), sectionStart);
}
function normalizeMethodSegment(text: string): string {
return ` ${text.trimStart()}`;
}
function normalizeSortedRun(
sortedText: string,
replacementStart: number,
sectionStart: number,
): string {
return replacementStart === sectionStart ? sortedText : `\n\n${sortedText}`;
}
function isSortableMethod(method: MethodDeclaration) {
return !method.getName().startsWith("#");
}
function getMethodTag(text: string, method: MethodDeclaration) {
return text.slice(method.getFullStart(), method.getStart()).match(
/@method\s+(\w+)/,
)?.[1];
}
function getSectionForMember(
sections: ReturnType<typeof getSectionBoundaries>,
member: { getEnd(): number; getStart(): number },
) {
return sections.find((section) =>
member.getStart() >= section.contentStart &&
member.getEnd() <= section.contentEnd
);
}
for (const path of paths) {
const sourceFile = project.addSourceFileAtPath(path);
const classDeclaration = getMainClass(sourceFile);
const members = classDeclaration.getMembers();
const text = sourceFile.getFullText();
const sections = getSectionBoundaries(text);
const replacements = [];
const sortableMethods = members.flatMap((member) => {
if (
member.getKind() !== SyntaxKind.MethodDeclaration ||
!isSortableMethod(member as MethodDeclaration)
) {
return [];
}
const method = member as MethodDeclaration;
const section = getSectionForMember(sections, method);
if (section === undefined) {
return [];
}
return [{
index: members.indexOf(method),
method,
section,
tag: getMethodTag(text, method) ?? section.tag,
}];
});
for (const section of sections) {
const sectionMethods = sortableMethods
.filter((entry) => entry.section === section)
.map((entry) => entry.method);
const targetMethods = sortableMethods
.filter((entry) => entry.tag === section.tag)
.toSorted((left, right) => {
const byName = left.method.getName().localeCompare(
right.method.getName(),
);
return byName === 0 ? left.index - right.index : byName;
});
if (sectionMethods.length === 0 && targetMethods.length === 0) {
continue;
}
const replacementStart = sectionMethods.length === 0
? section.contentStart
: getMethodSegmentStart(sectionMethods[0], section.contentStart);
const replacementEnd = sectionMethods.at(-1)?.getEnd() ??
section.contentStart;
const sortedText = targetMethods
.map((entry) =>
normalizeMethodSegment(
text.slice(
getMethodSegmentStart(
entry.method,
entry.section.contentStart,
),
entry.method.getEnd(),
),
)
)
.join("\n\n");
replacements.push({
end: replacementEnd,
start: replacementStart,
text: normalizeSortedRun(
sortedText,
replacementStart,
section.contentStart,
),
});
}
for (const replacement of replacements.toReversed()) {
sourceFile.replaceText(
[replacement.start, replacement.end],
replacement.text,
);
}
sourceFile.saveSync();
}