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
100 lines (80 loc) · 2.61 KB
/
pausePoints.js
File metadata and controls
100 lines (80 loc) · 2.61 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
import { traverseAst } from "./utils/ast";
import * as t from "@babel/types";
import type { AstLocation } from "./types";
import type { BabelNode } from "@babel/types";
import type { SimplePath } from "./utils/simple-path";
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: string) {
const state = [];
traverseAst(sourceId, { enter: onEnter }, state);
return state;
}
function onEnter(node: BabelNode, ancestors: SimplePath[], state) {
const parent = ancestors[ancestors.length - 1];
if (
isAssignment(node) ||
isImport(node) ||
isControlFlow(node) ||
t.isDebuggerStatement(node)
) {
addPoint(state, node.loc.start);
}
if (isReturn(node)) {
if (t.isCallExpression(node.argument)) {
addEmptyPoint(state, node.loc.start);
} else {
addPoint(state, node.loc.start);
}
}
if (t.isCallExpression(node)) {
addPoint(state, node.loc.start, {
breakpoint: true,
// NOTE: we do not want to land inside an expression e.g. [], {}, call
stepOver: !inExpression(parent)
});
}
if (t.isFunction(node)) {
const { line, column } = node.loc.end;
addBreakPoint(state, node.loc.start);
addPoint(state, { line, column: column - 1 });
}
if (t.isProgram(node)) {
const lastStatement = node.body[node.body.length - 1];
addPoint(state, lastStatement.loc.end);
}
}
function formatNode(location, types) {
return { location, types };
}
function addPoint(
state,
location,
types = { breakpoint: true, stepOver: true }
) {
state.push(formatNode(location, types));
}
function addEmptyPoint(state, location) {
addPoint(state, location, { breakpoint: false, stepOver: false });
}
function addBreakPoint(state, location) {
addPoint(state, location, { breakpoint: true, stepOver: false });
}