forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreakpointPositions.js
More file actions
214 lines (182 loc) · 5.92 KB
/
Copy pathbreakpointPositions.js
File metadata and controls
214 lines (182 loc) · 5.92 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
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
/* 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 {
isOriginalId,
isGeneratedId,
originalToGeneratedId
} from "devtools-source-map";
import { uniqBy, zip } from "lodash";
import {
getSource,
getSourceFromId,
hasBreakpointPositions,
hasBreakpointPositionsForLine,
getBreakpointPositionsForSource,
getSourceActorsForSource
} from "../../selectors";
import type {
MappedLocation,
Range,
SourceLocation,
BreakpointPositions,
Context
} from "../../types";
import { makeBreakpointId } from "../../utils/breakpoint";
import {
memoizeableAction,
type MemoizedAction
} from "../../utils/memoizableAction";
import type { ThunkArgs } from "../../actions/types";
async function mapLocations(
generatedLocations: SourceLocation[],
{ sourceMaps }: ThunkArgs
) {
if (generatedLocations.length == 0) {
return [];
}
const { sourceId } = generatedLocations[0];
const originalLocations = await sourceMaps.getOriginalLocations(
sourceId,
generatedLocations
);
return zip(originalLocations, generatedLocations).map(
([location, generatedLocation]) => ({ location, generatedLocation })
);
}
// Filter out positions, that are not in the original source Id
function filterBySource(positions, sourceId) {
if (!isOriginalId(sourceId)) {
return positions;
}
return positions.filter(position => position.location.sourceId == sourceId);
}
function filterByUniqLocation(positions: MappedLocation[]) {
return uniqBy(positions, ({ location }) => makeBreakpointId(location));
}
function convertToList(results, source) {
const { id, url } = source;
const positions = [];
for (const line in results) {
for (const column of results[line]) {
positions.push({
line: Number(line),
column: column,
sourceId: id,
sourceUrl: url
});
}
}
return positions;
}
function groupByLine(results, sourceId, line) {
const isOriginal = isOriginalId(sourceId);
const positions = {};
// Ensure that we have an entry for the line fetched
if (typeof line === "number") {
positions[line] = [];
}
for (const result of results) {
const location = isOriginal ? result.location : result.generatedLocation;
if (!positions[location.line]) {
positions[location.line] = [];
}
positions[location.line].push(result);
}
return positions;
}
async function _setBreakpointPositions(cx, sourceId, line, thunkArgs) {
const { client, dispatch, getState, sourceMaps } = thunkArgs;
let generatedSource = getSource(getState(), sourceId);
if (!generatedSource) {
return;
}
let results = {};
if (isOriginalId(sourceId)) {
// Explicitly typing ranges is required to work around the following issue
// https://github.com/facebook/flow/issues/5294
const ranges: Range[] = await sourceMaps.getGeneratedRangesForOriginal(
sourceId,
generatedSource.url,
true
);
const generatedSourceId = originalToGeneratedId(sourceId);
generatedSource = getSourceFromId(getState(), generatedSourceId);
// Note: While looping here may not look ideal, in the vast majority of
// cases, the number of ranges here should be very small, and is quite
// likely to only be a single range.
for (const range of ranges) {
// Wrap infinite end positions to the next line to keep things simple
// and because we know we don't care about the end-line whitespace
// in this case.
if (range.end.column === Infinity) {
range.end = {
line: range.end.line + 1,
column: 0
};
}
const bps = await client.getBreakpointPositions(
getSourceActorsForSource(getState(), generatedSource.id),
range
);
for (const bpLine in bps) {
results[bpLine] = (results[bpLine] || []).concat(bps[bpLine]);
}
}
} else {
if (typeof line !== "number") {
throw new Error("Line is required for generated sources");
}
results = await client.getBreakpointPositions(
getSourceActorsForSource(getState(), generatedSource.id),
{ start: { line, column: 0 }, end: { line: line + 1, column: 0 } }
);
}
let positions = convertToList(results, generatedSource);
positions = await mapLocations(positions, thunkArgs);
positions = filterBySource(positions, sourceId);
positions = filterByUniqLocation(positions);
positions = groupByLine(positions, sourceId, line);
const source = getSource(getState(), sourceId);
// NOTE: it's possible that the source was removed during a navigate
if (!source) {
return;
}
dispatch({
type: "ADD_BREAKPOINT_POSITIONS",
cx,
source: source,
positions
});
return positions;
}
function generatedSourceActorKey(state, sourceId) {
const generatedSource = getSource(
state,
isOriginalId(sourceId) ? originalToGeneratedId(sourceId) : sourceId
);
const actors = generatedSource
? getSourceActorsForSource(state, generatedSource.id).map(
({ actor }) => actor
)
: [];
return [sourceId, ...actors].join(":");
}
export const setBreakpointPositions: MemoizedAction<
{ cx: Context, sourceId: string, line?: number },
?BreakpointPositions
> = memoizeableAction("setBreakpointPositions", {
hasValue: ({ sourceId, line }, { getState }) =>
isGeneratedId(sourceId) && line
? hasBreakpointPositionsForLine(getState(), sourceId, line)
: hasBreakpointPositions(getState(), sourceId),
getValue: ({ sourceId, line }, { getState }) =>
getBreakpointPositionsForSource(getState(), sourceId),
createKey({ sourceId, line }, { getState }) {
const key = generatedSourceActorKey(getState(), sourceId);
return isGeneratedId(sourceId) && line ? `${key}-${line}` : key;
},
action: async ({ cx, sourceId, line }, thunkArgs) =>
_setBreakpointPositions(cx, sourceId, line, thunkArgs)
});