summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/utils/location.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /devtools/client/debugger/src/utils/location.js
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/debugger/src/utils/location.js')
-rw-r--r--devtools/client/debugger/src/utils/location.js134
1 files changed, 134 insertions, 0 deletions
diff --git a/devtools/client/debugger/src/utils/location.js b/devtools/client/debugger/src/utils/location.js
new file mode 100644
index 0000000000..eefe516f12
--- /dev/null
+++ b/devtools/client/debugger/src/utils/location.js
@@ -0,0 +1,134 @@
+/* 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 { getSelectedLocation } from "./selected-location";
+import { getSource } from "../selectors";
+
+/**
+ * Note that arguments can be created via `createLocation`.
+ * But they can also be created via `createPendingLocation` in reducer/pending-breakpoints.js.
+ * Both will have similar line and column attributes.
+ */
+export function comparePosition(a, b) {
+ return a && b && a.line == b.line && a.column == b.column;
+}
+
+export function createLocation({
+ source,
+ sourceActor = null,
+
+ // Line 0 represents no specific line chosen for action
+ line = 0,
+ column,
+
+ sourceUrl = "",
+}) {
+ return {
+ source,
+ sourceActor,
+ // Alias which should probably be migrate to query source and sourceActor?
+ sourceId: source.id,
+ sourceActorId: sourceActor?.id,
+
+ line,
+ column,
+
+ // Is this still used anywhere??
+ sourceUrl,
+ };
+}
+
+/**
+ * Convert location objects created via `createLocation` into
+ * the format used by the Source Map Loader/Worker.
+ * It only needs sourceId, line and column attributes.
+ */
+export function debuggerToSourceMapLocation(location) {
+ return {
+ sourceId: location.source.id,
+ line: location.line,
+ column: location.column,
+
+ // Also add sourceUrl attribute as this may be preserved in jest tests
+ // where we return the exact same object.
+ // This will be removed by bug 1822783.
+ sourceUrl: location.sourceUrl,
+ };
+}
+
+/**
+ * Pending location only need these three attributes,
+ * and especially doesn't need the large source and sourceActor objects of the regular location objects.
+ *
+ * @param {Object} location
+ */
+export function createPendingSelectedLocation(location) {
+ return {
+ url: location.source.url,
+
+ line: location.line,
+ column: location.column,
+ };
+}
+
+export function sortSelectedLocations(locations, selectedSource) {
+ return Array.from(locations).sort((locationA, locationB) => {
+ const aSelected = getSelectedLocation(locationA, selectedSource);
+ const bSelected = getSelectedLocation(locationB, selectedSource);
+
+ // Order the locations by line number…
+ if (aSelected.line < bSelected.line) {
+ return -1;
+ }
+
+ if (aSelected.line > bSelected.line) {
+ return 1;
+ }
+
+ // … and if we have the same line, we want to return location with undefined columns
+ // first, and then order them by column
+ if (aSelected.column == bSelected.column) {
+ return 0;
+ }
+
+ if (aSelected.column === undefined) {
+ return -1;
+ }
+
+ if (bSelected.column === undefined) {
+ return 1;
+ }
+
+ return aSelected.column < bSelected.column ? -1 : 1;
+ });
+}
+
+/**
+ * Source map Loader/Worker and debugger frontend don't use the same objects for locations.
+ * Worker uses 'sourceId' attributes whereas the frontend has 'source' attribute.
+ */
+export function sourceMapToDebuggerLocation(state, location) {
+ // From MapScopes modules, we might re-process the exact same location objects
+ // for which we would already have computed the source object,
+ // and which would lack sourceId attribute.
+ if (location.source) {
+ return location;
+ }
+
+ // SourceMapLoader doesn't known about debugger's source objects
+ // so that we have to fetch it from here
+ const source = getSource(state, location.sourceId);
+ if (!source) {
+ throw new Error(`Could not find source-map source ${location.sourceId}`);
+ }
+
+ return createLocation({
+ ...location,
+ source,
+
+ // Ensure having location with sourceUrl attribute set.
+ // To be removed in bug 1822783.
+ sourceUrl: source.url,
+ });
+}