forked from microsoft/TypeScript-DOM-lib-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchangelog.ts
182 lines (162 loc) · 5.21 KB
/
changelog.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
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
import { execSync } from "child_process";
import ts from "typescript";
import { fileURLToPath } from "url";
function gitShowFile(commit: string, path: string) {
return execSync(`git show ${commit}:${path}`, { encoding: "utf-8" });
}
function gitLatestTag() {
return execSync(`git describe --tags --abbrev=0`, {
encoding: "utf-8",
}).trim();
}
function mapInterfaceToMembers(interfaces: ts.InterfaceDeclaration[]) {
const interfaceToMemberMap = new Map<string, string[]>();
for (const decl of interfaces) {
interfaceToMemberMap.set(
decl.name.text,
decl.members.map((m) => m.name?.getText()).filter((n) => n) as string[]
);
}
return interfaceToMemberMap;
}
function extractTypesFromFile(file: string) {
const source = ts.createSourceFile(
"dom",
file,
ts.ScriptTarget.ES2015,
/*setParentNodes */ true
);
const interfaceNames = source.statements
.filter(ts.isVariableStatement)
.map((v) => v.declarationList.declarations[0].name.getText(source));
const tsInterfacedecls = source.statements.filter(ts.isInterfaceDeclaration);
const idlInterfaceDecls = tsInterfacedecls.filter((i) =>
interfaceNames.includes(i.name.text)
);
const otherDecls = tsInterfacedecls.filter(
(i) => !interfaceNames.includes(i.name.text)
);
const interfaceToMemberMap = mapInterfaceToMembers(idlInterfaceDecls);
const otherToMemberMap = mapInterfaceToMembers(otherDecls);
return { interfaceToMemberMap, otherToMemberMap };
}
function compareSet<T>(x: Set<T>, y: Set<T>) {
function intersection<T>(x: Set<T>, y: Set<T>) {
const result = new Set<T>();
for (const i of y) {
if (x.has(i)) {
result.add(i);
}
}
return result;
}
function difference<T>(x: Set<T>, y: Set<T>) {
const result = new Set(x);
for (const i of y) {
result.delete(i);
}
return result;
}
const common = intersection(x, y);
const added = difference(y, common);
const removed = difference(x, common);
return { added, removed, common };
}
function diffTypes(previous: string, current: string) {
function diff(
previousMap: Map<string, string[]>,
currentMap: Map<string, string[]>
) {
const { added, removed, common } = compareSet(
new Set(previousMap.keys()),
new Set(currentMap.keys())
);
const modified = new Map<
string,
{ added: Set<string>; removed: Set<string> }
>();
for (const name of common) {
const previousMembers = new Set(previousMap.get(name));
const currentMembers = new Set(currentMap.get(name));
const { added, removed } = compareSet(previousMembers, currentMembers);
if (!added.size && !removed.size) {
continue;
}
modified.set(name, { added, removed });
}
return { added, removed, modified };
}
const previousTypes = extractTypesFromFile(previous);
const currentTypes = extractTypesFromFile(current);
return {
interfaces: diff(
previousTypes.interfaceToMemberMap,
currentTypes.interfaceToMemberMap
),
others: diff(previousTypes.otherToMemberMap, currentTypes.otherToMemberMap),
};
}
function writeAddedRemoved(added: Set<string>, removed: Set<string>) {
function newlineSeparatedList(names: Set<string>) {
return [...names].map((a) => `* \`${a}\``).join("\n");
}
const output = [];
if (added.size) {
output.push(`## New interfaces\n\n${newlineSeparatedList(added)}`);
}
if (removed.size) {
output.push(`## Removed interfaces\n\n${newlineSeparatedList(removed)}`);
}
return output.join("\n\n");
}
function writeAddedRemovedInline(added: Set<string>, removed: Set<string>) {
function commaSeparatedList(names: Set<string>) {
return [...names].map((a) => `\`${a}\``).join(", ");
}
const output = [];
if (added.size) {
output.push(` * Added: ${commaSeparatedList(added)}`);
}
if (removed.size) {
output.push(` * Removed: ${commaSeparatedList(removed)}`);
}
return output.join("\n");
}
const dom = "baselines/dom.generated.d.ts";
export function generate(): string {
const [base = gitLatestTag(), head = "HEAD"] = process.argv.slice(2);
const previous = gitShowFile(base, dom);
const current = gitShowFile(head, dom);
const {
interfaces: { added, removed, modified },
others,
} = diffTypes(previous, current);
const outputs = [];
if (added.size || removed.size) {
outputs.push(writeAddedRemoved(added, removed));
}
if (modified.size) {
const modifiedOutput = [`## Modified\n`];
for (const [key, value] of modified.entries()) {
modifiedOutput.push(`* ${key}`);
modifiedOutput.push(writeAddedRemovedInline(value.added, value.removed));
}
outputs.push(modifiedOutput.join("\n"));
}
if (others.modified.size) {
const modifiedOutput = [`### Non-value types\n`];
for (const [key, value] of others.modified.entries()) {
modifiedOutput.push(`* ${key}`);
modifiedOutput.push(writeAddedRemovedInline(value.added, value.removed));
}
outputs.push(modifiedOutput.join("\n"));
}
const output = outputs.join("\n\n");
if (!output.length) {
throw new Error(`No change reported between ${base} and ${head}.`);
}
return output;
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {
console.log(generate());
}