forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpausePoints.js
More file actions
91 lines (76 loc) · 2.35 KB
/
pausePoints.js
File metadata and controls
91 lines (76 loc) · 2.35 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
import { traverseAst } from "./utils/ast";
import * as t from "@babel/types";
import type { AstLocation } from "./types";
export type PausePoint = {|
location: AstLocation,
types: {| breakpoint: boolean, stepOver: boolean |}
|};
export type PausePoints = PausePoint[];
const isControlFlow = node =>
t.isForStatement(node) || t.isWhileStatement(node) || t.isIfStatement(node);
const isAssignment = node =>
t.isVariableDeclarator(node) || t.isAssignmentExpression(node);
const isImport = node => t.isImport(node) || t.isImportDeclaration(node);
const isReturn = node => t.isReturnStatement(node);
const inExpression = parent =>
t.isArrayExpression(parent.node) ||
t.isObjectProperty(parent.node) ||
t.isCallExpression(parent.node) ||
t.isTemplateLiteral(parent.node);
export function getPausePoints(sourceId) {
const state = [];
traverseAst(sourceId, { enter: onEnter }, state);
return state;
}
function formatNode(location, types) {
return { location, types };
}
function onEnter(node, ancestors, state) {
const parent = ancestors[ancestors.length - 1];
if (isAssignment(node) || isImport(node) || isControlFlow(node)) {
state.push(
formatNode(node.loc.start, { breakpoint: true, stepOver: true })
);
}
if (isReturn(node)) {
if (t.isCallExpression(node.argument)) {
state.push(
formatNode(node.loc.start, { breakpoint: false, stepOver: false })
);
} else {
state.push(
formatNode(node.loc.start, { breakpoint: true, stepOver: true })
);
}
}
if (t.isCallExpression(node)) {
state.push(
formatNode(node.loc.start, {
breakpoint: true,
// NOTE: we do not want to land inside an expression e.g. [], {}, call
stepOver: !inExpression(parent)
})
);
}
if (t.isDebuggerStatement(node)) {
state.push(
formatNode(node.loc.start, { breakpoint: true, stepOver: true })
);
}
if (t.isFunction(node)) {
const { line, column } = node.loc.end;
state.push(formatNode(node.loc.start, { breakpoint: true }));
state.push(
formatNode(
{ line, column: column - 1 },
{ breakpoint: true, stepOver: true }
)
);
}
if (t.isProgram(node)) {
const lastStatement = node.body[node.body.length - 1];
state.push(
formatNode(lastStatement.loc.end, { breakpoint: true, stepOver: true })
);
}
}