-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathdbg.ts
516 lines (458 loc) · 18.5 KB
/
dbg.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/// <reference lib="es2019" />
/* @internal */
namespace Debug {
interface Node {
kind: number;
}
type FunctionExpression = Node;
type ArrowFunction = Node;
type MethodDeclaration = Node;
type Expression = Node;
type SourceFile = Node;
type VariableDeclaration = Node;
type BindingElement = Node;
type CallExpression = Node;
type BinaryExpression = Node;
interface SwitchStatement extends Node {
caseBlock: CaseBlock;
}
interface CaseBlock extends Node {
clauses: (CaseClause | DefaultClause)[];
}
interface CaseClause extends Node {
_caseclauseBrand: any;
expression: Expression;
}
interface DefaultClause extends Node {
_defaultClauseBrand: any;
}
interface TypeScriptModule {
readonly SyntaxKind: {
readonly CaseClause: number;
readonly DefaultClause: number;
};
readonly FlowFlags: {
readonly Unreachable: number,
readonly Start: number,
readonly BranchLabel: number,
readonly LoopLabel: number,
readonly Assignment: number,
readonly TrueCondition: number,
readonly FalseCondition: number,
readonly SwitchClause: number,
readonly ArrayMutation: number,
readonly Call: number,
readonly ReduceLabel: number,
readonly Referenced: number,
readonly Shared: number,
readonly Label: number,
readonly Condition: number,
};
getSourceFileOfNode(node: Node): SourceFile;
getSourceTextOfNodeFromSourceFile(sourceFile: SourceFile, node: Node, includeTrivia?: boolean): string;
isDefaultClause(node: Node): node is DefaultClause;
}
type FlowNode =
| FlowStart
| FlowLabel
| FlowAssignment
| FlowCall
| FlowCondition
| FlowSwitchClause
| FlowArrayMutation
| FlowReduceLabel
;
interface FlowNodeBase {
flags: FlowFlags;
id?: number;
}
interface FlowStart extends FlowNodeBase {
node?: FunctionExpression | ArrowFunction | MethodDeclaration;
}
interface FlowLabel extends FlowNodeBase {
antecedents: FlowNode[] | undefined;
}
interface FlowAssignment extends FlowNodeBase {
node: Expression | VariableDeclaration | BindingElement;
antecedent: FlowNode;
}
interface FlowCall extends FlowNodeBase {
node: CallExpression;
antecedent: FlowNode;
}
interface FlowCondition extends FlowNodeBase {
node: Expression;
antecedent: FlowNode;
}
interface FlowSwitchClause extends FlowNodeBase {
switchStatement: SwitchStatement;
clauseStart: number;
clauseEnd: number;
antecedent: FlowNode;
}
interface FlowArrayMutation extends FlowNodeBase {
node: CallExpression | BinaryExpression;
antecedent: FlowNode;
}
export interface FlowReduceLabel extends FlowNodeBase {
target: FlowLabel;
antecedents: FlowNode[];
antecedent: FlowNode;
}
type FlowFlags = number;
let FlowFlags: TypeScriptModule["FlowFlags"];
let getSourceFileOfNode: TypeScriptModule["getSourceFileOfNode"];
let getSourceTextOfNodeFromSourceFile: TypeScriptModule["getSourceTextOfNodeFromSourceFile"];
let isDefaultClause: TypeScriptModule["isDefaultClause"];
export function init(ts: TypeScriptModule) {
FlowFlags = ts.FlowFlags;
getSourceFileOfNode = ts.getSourceFileOfNode;
getSourceTextOfNodeFromSourceFile = ts.getSourceTextOfNodeFromSourceFile;
isDefaultClause = ts.isDefaultClause;
}
let nextDebugFlowId = -1;
function getDebugFlowNodeId(f: FlowNode) {
if (!f.id) {
f.id = nextDebugFlowId;
nextDebugFlowId--;
}
return f.id;
}
export function formatControlFlowGraph(flowNode: FlowNode) {
const enum BoxCharacter {
lr = "─",
ud = "│",
dr = "╭",
dl = "╮",
ul = "╯",
ur = "╰",
udr = "├",
udl = "┤",
dlr = "┬",
ulr = "┴",
udlr = "╫",
}
const enum Connection {
Up = 1 << 0,
Down = 1 << 1,
Left = 1 << 2,
Right = 1 << 3,
UpDown = Up | Down,
LeftRight = Left | Right,
UpLeft = Up | Left,
UpRight = Up | Right,
DownLeft = Down | Left,
DownRight = Down | Right,
UpDownLeft = UpDown | Left,
UpDownRight = UpDown | Right,
UpLeftRight = Up | LeftRight,
DownLeftRight = Down | LeftRight,
UpDownLeftRight = UpDown | LeftRight,
NoChildren = 1 << 4,
}
interface FlowGraphNode {
id: number;
flowNode: FlowNode;
edges: FlowGraphEdge[];
text: string;
lane: number;
endLane: number;
level: number;
circular: boolean | "circularity";
}
interface FlowGraphEdge {
source: FlowGraphNode;
target: FlowGraphNode;
}
const hasAntecedentFlags =
FlowFlags.Assignment |
FlowFlags.Condition |
FlowFlags.SwitchClause |
FlowFlags.ArrayMutation |
FlowFlags.Call |
FlowFlags.ReduceLabel;
const hasNodeFlags =
FlowFlags.Start |
FlowFlags.Assignment |
FlowFlags.Call |
FlowFlags.Condition |
FlowFlags.ArrayMutation;
const links: Record<number, FlowGraphNode> = Object.create(/*o*/ null); // eslint-disable-line no-null/no-null
const nodes: FlowGraphNode[] = [];
const edges: FlowGraphEdge[] = [];
const root = buildGraphNode(flowNode, new Set());
for (const node of nodes) {
node.text = renderFlowNode(node.flowNode, node.circular);
computeLevel(node);
}
const height = computeHeight(root);
const columnWidths = computeColumnWidths(height);
computeLanes(root, 0);
return renderGraph();
function isFlowSwitchClause(f: FlowNode): f is FlowSwitchClause {
return !!(f.flags & FlowFlags.SwitchClause);
}
function hasAntecedents(f: FlowNode): f is FlowLabel & { antecedents: FlowNode[] } {
return !!(f.flags & FlowFlags.Label) && !!(f as FlowLabel).antecedents;
}
function hasAntecedent(f: FlowNode): f is Extract<FlowNode, { antecedent: FlowNode }> {
return !!(f.flags & hasAntecedentFlags);
}
function hasNode(f: FlowNode): f is Extract<FlowNode, { node?: Node }> {
return !!(f.flags & hasNodeFlags);
}
function getChildren(node: FlowGraphNode) {
const children: FlowGraphNode[] = [];
for (const edge of node.edges) {
if (edge.source === node) {
children.push(edge.target);
}
}
return children;
}
function getParents(node: FlowGraphNode) {
const parents: FlowGraphNode[] = [];
for (const edge of node.edges) {
if (edge.target === node) {
parents.push(edge.source);
}
}
return parents;
}
function buildGraphNode(flowNode: FlowNode, seen: Set<FlowNode>): FlowGraphNode {
const id = getDebugFlowNodeId(flowNode);
let graphNode = links[id];
if (graphNode && seen.has(flowNode)) {
graphNode.circular = true;
graphNode = {
id: -1,
flowNode,
edges: [],
text: "",
lane: -1,
endLane: -1,
level: -1,
circular: "circularity"
};
nodes.push(graphNode);
return graphNode;
}
seen.add(flowNode);
if (!graphNode) {
links[id] = graphNode = { id, flowNode, edges: [], text: "", lane: -1, endLane: -1, level: -1, circular: false };
nodes.push(graphNode);
if (hasAntecedents(flowNode)) {
for (const antecedent of flowNode.antecedents) {
buildGraphEdge(graphNode, antecedent, seen);
}
}
else if (hasAntecedent(flowNode)) {
buildGraphEdge(graphNode, flowNode.antecedent, seen);
}
}
seen.delete(flowNode);
return graphNode;
}
function buildGraphEdge(source: FlowGraphNode, antecedent: FlowNode, seen: Set<FlowNode>) {
const target = buildGraphNode(antecedent, seen);
const edge: FlowGraphEdge = { source, target };
edges.push(edge);
source.edges.push(edge);
target.edges.push(edge);
}
function computeLevel(node: FlowGraphNode): number {
if (node.level !== -1) {
return node.level;
}
let level = 0;
for (const parent of getParents(node)) {
level = Math.max(level, computeLevel(parent) + 1);
}
return node.level = level;
}
function computeHeight(node: FlowGraphNode): number {
let height = 0;
for (const child of getChildren(node)) {
height = Math.max(height, computeHeight(child));
}
return height + 1;
}
function computeColumnWidths(height: number) {
const columns: number[] = fill(Array(height), 0);
for (const node of nodes) {
columns[node.level] = Math.max(columns[node.level], node.text.length);
}
return columns;
}
function computeLanes(node: FlowGraphNode, lane: number) {
if (node.lane === -1) {
node.lane = lane;
node.endLane = lane;
const children = getChildren(node);
for (let i = 0; i < children.length; i++) {
if (i > 0) lane++;
const child = children[i];
computeLanes(child, lane);
if (child.endLane > node.endLane) {
lane = child.endLane;
}
}
node.endLane = lane;
}
}
function getHeader(flags: FlowFlags) {
if (flags & FlowFlags.Start) return "Start";
if (flags & FlowFlags.BranchLabel) return "Branch";
if (flags & FlowFlags.LoopLabel) return "Loop";
if (flags & FlowFlags.Assignment) return "Assignment";
if (flags & FlowFlags.TrueCondition) return "True";
if (flags & FlowFlags.FalseCondition) return "False";
if (flags & FlowFlags.SwitchClause) return "SwitchClause";
if (flags & FlowFlags.ArrayMutation) return "ArrayMutation";
if (flags & FlowFlags.Call) return "Call";
if (flags & FlowFlags.ReduceLabel) return "ReduceLabel";
if (flags & FlowFlags.Unreachable) return "Unreachable";
throw new Error();
}
function getNodeText(node: Node) {
const sourceFile = getSourceFileOfNode(node);
return getSourceTextOfNodeFromSourceFile(sourceFile, node, /*includeTrivia*/ false);
}
function renderFlowNode(flowNode: FlowNode, circular: boolean | "circularity") {
let text = getHeader(flowNode.flags);
if (circular) {
text = `${text}#${getDebugFlowNodeId(flowNode)}`;
}
if (hasNode(flowNode)) {
if (flowNode.node) {
text += ` (${getNodeText(flowNode.node)})`;
}
}
else if (isFlowSwitchClause(flowNode)) {
const clauses: string[] = [];
for (let i = flowNode.clauseStart; i < flowNode.clauseEnd; i++) {
const clause = flowNode.switchStatement.caseBlock.clauses[i];
if (isDefaultClause(clause)) {
clauses.push("default");
}
else {
clauses.push(getNodeText(clause.expression));
}
}
text += ` (${clauses.join(", ")})`;
}
return circular === "circularity" ? `Circular(${text})` : text;
}
function renderGraph() {
const columnCount = columnWidths.length;
const laneCount = nodes.reduce((x, n) => Math.max(x, n.lane), 0) + 1;
const lanes: string[] = fill(Array(laneCount), "");
const grid: (FlowGraphNode | undefined)[][] = columnWidths.map(() => Array(laneCount));
const connectors: Connection[][] = columnWidths.map(() => fill(Array(laneCount), 0));
// build connectors
for (const node of nodes) {
grid[node.level][node.lane] = node;
const children = getChildren(node);
for (let i = 0; i < children.length; i++) {
const child = children[i];
let connector: Connection = Connection.Right;
if (child.lane === node.lane) connector |= Connection.Left;
if (i > 0) connector |= Connection.Up;
if (i < children.length - 1) connector |= Connection.Down;
connectors[node.level][child.lane] |= connector;
}
if (children.length === 0) {
connectors[node.level][node.lane] |= Connection.NoChildren;
}
const parents = getParents(node);
for (let i = 0; i < parents.length; i++) {
const parent = parents[i];
let connector: Connection = Connection.Left;
if (i > 0) connector |= Connection.Up;
if (i < parents.length - 1) connector |= Connection.Down;
connectors[node.level - 1][parent.lane] |= connector;
}
}
// fill in missing connectors
for (let column = 0; column < columnCount; column++) {
for (let lane = 0; lane < laneCount; lane++) {
const left = column > 0 ? connectors[column - 1][lane] : 0;
const above = lane > 0 ? connectors[column][lane - 1] : 0;
let connector = connectors[column][lane];
if (!connector) {
if (left & Connection.Right) connector |= Connection.LeftRight;
if (above & Connection.Down) connector |= Connection.UpDown;
connectors[column][lane] = connector;
}
}
}
for (let column = 0; column < columnCount; column++) {
for (let lane = 0; lane < lanes.length; lane++) {
const connector = connectors[column][lane];
const fill = connector & Connection.Left ? BoxCharacter.lr : " ";
const node = grid[column][lane];
if (!node) {
if (column < columnCount - 1) {
writeLane(lane, repeat(fill, columnWidths[column] + 1));
}
}
else {
writeLane(lane, node.text);
if (column < columnCount - 1) {
writeLane(lane, " ");
writeLane(lane, repeat(fill, columnWidths[column] - node.text.length));
}
}
writeLane(lane, getBoxCharacter(connector));
writeLane(lane, connector & Connection.Right && column < columnCount - 1 && !grid[column + 1][lane] ? BoxCharacter.lr : " ");
}
}
return `\n${lanes.join("\n")}\n`;
function writeLane(lane: number, text: string) {
lanes[lane] += text;
}
}
function getBoxCharacter(connector: Connection) {
switch (connector) {
case Connection.UpDown: return BoxCharacter.ud;
case Connection.LeftRight: return BoxCharacter.lr;
case Connection.UpLeft: return BoxCharacter.ul;
case Connection.UpRight: return BoxCharacter.ur;
case Connection.DownLeft: return BoxCharacter.dl;
case Connection.DownRight: return BoxCharacter.dr;
case Connection.UpDownLeft: return BoxCharacter.udl;
case Connection.UpDownRight: return BoxCharacter.udr;
case Connection.UpLeftRight: return BoxCharacter.ulr;
case Connection.DownLeftRight: return BoxCharacter.dlr;
case Connection.UpDownLeftRight: return BoxCharacter.udlr;
}
return " ";
}
function fill<T>(array: T[], value: T) {
if (array.fill) {
array.fill(value);
}
else {
for (let i = 0; i < array.length; i++) {
array[i] = value;
}
}
return array;
}
function repeat(ch: string, length: number) {
if (ch.repeat) {
return length > 0 ? ch.repeat(length) : "";
}
let s = "";
while (s.length < length) {
s += ch;
}
return s;
}
}
// Export as a module. NOTE: Can't use module exports as this is built using --outFile
declare const module: { exports: {} };
if (typeof module !== "undefined" && module.exports) {
module.exports = Debug;
}
}