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
187 lines (152 loc) · 5.09 KB
/
Copy pathpausePoints.js
File metadata and controls
187 lines (152 loc) · 5.09 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
/* 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 isEqual from "lodash/isEqual";
import type { BabelNode } from "@babel/types";
import type { SimplePath } from "./utils/simple-path";
const isForStatement = node =>
t.isForStatement(node) || t.isForOfStatement(node);
const isControlFlow = node =>
isForStatement(node) ||
t.isWhileStatement(node) ||
t.isIfStatement(node) ||
t.isSwitchCase(node) ||
t.isSwitchStatement(node) ||
t.isTryStatement(node) ||
t.isWithStatement(node);
const isAssignment = node =>
t.isVariableDeclarator(node) ||
t.isAssignmentExpression(node) ||
t.isAssignmentPattern(node);
const isImport = node => t.isImport(node) || t.isImportDeclaration(node);
const isReturn = node => t.isReturnStatement(node);
const isCall = node => t.isCallExpression(node) || t.isJSXElement(node);
const inStepExpression = parent =>
t.isArrayExpression(parent) ||
t.isObjectProperty(parent) ||
t.isCallExpression(parent) ||
t.isJSXElement(parent);
const inExpression = (parent, grandParent) =>
inStepExpression(parent) ||
t.isJSXAttribute(grandParent) ||
t.isTemplateLiteral(parent);
const isExport = node =>
t.isExportNamedDeclaration(node) || t.isExportDefaultDeclaration(node);
function getStartLine(node) {
return node.loc.start.line;
}
export function getPausePoints(sourceId: string) {
const state = {};
traverseAst(sourceId, { enter: onEnter }, state);
return state;
}
/* eslint-disable complexity */
function onEnter(node: BabelNode, ancestors: SimplePath[], state) {
const parent = ancestors[ancestors.length - 1];
const parentNode = parent && parent.node;
const grandParent = ancestors[ancestors.length - 2];
const startLocation = node.loc.start;
if (
isImport(node) ||
t.isClassDeclaration(node) ||
isExport(node) ||
t.isDebuggerStatement(node) ||
t.isThrowStatement(node) ||
t.isExpressionStatement(node) ||
t.isBreakStatement(node) ||
t.isContinueStatement(node)
) {
return addStopPoint(state, startLocation);
}
if (isControlFlow(node)) {
if (isForStatement(node)) {
addStopPoint(state, startLocation);
} else {
addEmptyPoint(state, startLocation);
}
const test = node.test || node.discriminant;
if (test) {
addStopPoint(state, test.loc.start);
}
return;
}
if (t.isBlockStatement(node)) {
return addEmptyPoint(state, startLocation);
}
if (isReturn(node)) {
// We do not want to pause at the return if the
// argument is a call on the same line e.g. return foo()
if (
isCall(node.argument) &&
getStartLine(node) == getStartLine(node.argument)
) {
return addEmptyPoint(state, startLocation);
}
return addStopPoint(state, startLocation);
}
if (isAssignment(node)) {
// We only want to pause at literal assignments `var a = foo()`
const value = node.right || node.init;
if (isCall(value) || t.isFunction(parentNode)) {
return addEmptyPoint(state, startLocation);
}
return addStopPoint(state, startLocation);
}
if (isCall(node)) {
let location = startLocation;
// When functions are chained, we want to use the property location
// e.g `foo().bar()`
if (t.isMemberExpression(node.callee)) {
location = node.callee.property.loc.start;
}
// NOTE: we do not want to land inside an expression e.g. [], {}, call
const step = !inExpression(parent.node, grandParent && grandParent.node);
// NOTE: we add a point at the beginning of the expression
// and each of the calls because the engine does not support
// column-based member expression calls.
addPoint(state, startLocation, { break: true, step });
if (location && !isEqual(location, startLocation)) {
addPoint(state, location, { break: true, step });
}
return;
}
if (t.isClassProperty(node)) {
return addBreakPoint(state, startLocation);
}
if (t.isFunction(node)) {
const { line, column } = node.loc.end;
addBreakPoint(state, startLocation);
return addStopPoint(state, { line, column: column - 1 });
}
if (t.isProgram(node)) {
const lastStatement = node.body[node.body.length - 1];
if (lastStatement) {
return addStopPoint(state, lastStatement.loc.end);
}
}
if (!hasPoint(state, startLocation) && inStepExpression(parentNode)) {
return addEmptyPoint(state, startLocation);
}
}
function hasPoint(state, { line, column }) {
return state[line] && state[line][column];
}
function addPoint(state, { line, column }, types) {
if (!state[line]) {
state[line] = {};
}
state[line][column] = types;
return state;
}
function addStopPoint(state, location) {
return addPoint(state, location, { break: true, step: true });
}
function addEmptyPoint(state, location) {
return addPoint(state, location, {});
}
function addBreakPoint(state, location) {
return addPoint(state, location, { break: true });
}