forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
214 lines (182 loc) · 5.57 KB
/
index.js
File metadata and controls
214 lines (182 loc) · 5.57 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 {
getBreakpoint,
getSource,
getSourceActorsForSource
} from "../../selectors";
import { isGenerated } from "../source";
import { sortSelectedLocations } from "../location";
import assert from "../assert";
import { features } from "../prefs";
export * from "./astBreakpointLocation";
export * from "./breakpointPositions";
import type {
Source,
SourceActor,
SourceLocation,
SourceActorLocation,
PendingLocation,
Breakpoint,
BreakpointLocation,
PendingBreakpoint
} from "../../types";
import type { State } from "../../reducers/types";
// Return the first argument that is a string, or null if nothing is a
// string.
export function firstString(...args: string[]) {
for (const arg of args) {
if (typeof arg === "string") {
return arg;
}
}
return null;
}
// The ID for a Breakpoint is derived from its location in its Source.
export function makeBreakpointId(location: SourceLocation) {
const { sourceId, line, column } = location;
const columnString = column || "";
return `${sourceId}:${line}:${columnString}`;
}
export function getLocationWithoutColumn(location: SourceLocation) {
const { sourceId, line } = location;
return `${sourceId}:${line}`;
}
type AnySourceLocation = SourceLocation | PendingLocation;
export function makePendingLocationId(location: AnySourceLocation) {
assertPendingLocation(location);
const { sourceUrl, line, column } = location;
const sourceUrlString = sourceUrl || "";
const columnString = column || "";
return `${sourceUrlString}:${line}:${columnString}`;
}
export function makeBreakpointLocation(
state: State,
location: SourceLocation
): BreakpointLocation {
const source = getSource(state, location.sourceId);
if (!source) {
throw new Error("no source");
}
const breakpointLocation: any = {
line: location.line,
column: location.column
};
if (source.url) {
breakpointLocation.sourceUrl = source.url;
} else {
breakpointLocation.sourceId = getSourceActorsForSource(
state,
source.id
)[0].id;
}
return breakpointLocation;
}
export function makeSourceActorLocation(
sourceActor: SourceActor,
location: SourceLocation
) {
return {
sourceActor,
line: location.line,
column: location.column
};
}
// The ID for a BreakpointActor is derived from its location in its SourceActor.
export function makeBreakpointActorId(location: SourceActorLocation) {
const { sourceActor, line, column } = location;
const columnString = column || "";
return `${(sourceActor: any)}:${line}:${columnString}`;
}
export function assertBreakpoint(breakpoint: Breakpoint) {
assertLocation(breakpoint.location);
assertLocation(breakpoint.generatedLocation);
}
export function assertPendingBreakpoint(pendingBreakpoint: PendingBreakpoint) {
assertPendingLocation(pendingBreakpoint.location);
assertPendingLocation(pendingBreakpoint.generatedLocation);
}
export function assertLocation(location: SourceLocation) {
assertPendingLocation(location);
const { sourceId } = location;
assert(!!sourceId, "location must have a source id");
}
export function assertPendingLocation(location: PendingLocation) {
assert(!!location, "location must exist");
const { sourceUrl } = location;
// sourceUrl is null when the source does not have a url
assert(sourceUrl !== undefined, "location must have a source url");
assert(location.hasOwnProperty("line"), "location must have a line");
assert(
location.hasOwnProperty("column") != null,
"location must have a column"
);
}
// syncing
export function breakpointAtLocation(
breakpoints: Breakpoint[],
{ line, column }: SourceLocation
) {
return breakpoints.find(breakpoint => {
const sameLine = breakpoint.location.line === line;
if (!sameLine) {
return false;
}
// NOTE: when column breakpoints are disabled we want to find
// the first breakpoint
if (!features.columnBreakpoints) {
return true;
}
return breakpoint.location.column === column;
});
}
export function breakpointExists(state: State, location: SourceLocation) {
const currentBp = getBreakpoint(state, location);
return currentBp && !currentBp.disabled;
}
export function createXHRBreakpoint(
path: string,
method: string,
overrides?: Object = {}
) {
const properties = {
path,
method,
disabled: false,
loading: false,
text: L10N.getFormatStr("xhrBreakpoints.item.label", path)
};
return { ...properties, ...overrides };
}
function createPendingLocation(location: PendingLocation) {
const { sourceUrl, line, column } = location;
return { sourceUrl, line, column };
}
export function createPendingBreakpoint(bp: Breakpoint) {
const pendingLocation = createPendingLocation(bp.location);
const pendingGeneratedLocation = createPendingLocation(bp.generatedLocation);
assertPendingLocation(pendingLocation);
return {
options: bp.options,
disabled: bp.disabled,
location: pendingLocation,
astLocation: bp.astLocation,
generatedLocation: pendingGeneratedLocation
};
}
export function getSelectedText(
breakpoint: Breakpoint,
selectedSource: Source
) {
return selectedSource && isGenerated(selectedSource)
? breakpoint.text
: breakpoint.originalText;
}
export function sortSelectedBreakpoints(
breakpoints: Array<Breakpoint>,
selectedSource: ?Source
): Array<Breakpoint> {
return sortSelectedLocations(breakpoints, selectedSource);
}