summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/utils/breakpoint/index.js
blob: 740d3cd180cd82d379c6548bd9971e39db3b4b92 (plain)
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
/* 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/>. */

import { getSourceActorsForSource } from "../../selectors/index";
import { sortSelectedLocations } from "../location";
export * from "./breakpointPositions";

// The ID for a Breakpoint is derived from its location in its Source.
export function makeBreakpointId(location) {
  const { source, line, column } = location;
  const columnString = column || "";
  return `${source.id}:${line}:${columnString}`;
}

export function makeBreakpointServerLocationId(breakpointServerLocation) {
  const { sourceUrl, sourceId, line, column } = breakpointServerLocation;
  const sourceUrlOrId = sourceUrl || sourceId;
  const columnString = column || "";

  return `${sourceUrlOrId}:${line}:${columnString}`;
}

/**
 * Create a location object to set a breakpoint on the server.
 *
 * Debugger location objects includes a source and sourceActor attributes
 * whereas the server don't need them and instead only need either
 * the source URL -or- a precise source actor ID.
 */
export function makeBreakpointServerLocation(state, location) {
  const source = location.source;
  if (!source) {
    throw new Error("Missing 'source' attribute on location object");
  }
  const breakpointLocation = {
    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 createXHRBreakpoint(path, method, overrides = {}) {
  const properties = {
    path,
    method,
    disabled: false,
    loading: false,
    text: L10N.getFormatStr("xhrBreakpoints.item.label", path),
  };

  return { ...properties, ...overrides };
}

export function getSelectedText(breakpoint, selectedSource) {
  return !!selectedSource && !selectedSource.isOriginal
    ? breakpoint.text
    : breakpoint.originalText;
}

export function sortSelectedBreakpoints(breakpoints, selectedSource) {
  return sortSelectedLocations(breakpoints, selectedSource);
}