summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/actions/preview.js
blob: 3b3637e2c0c6354cf1c8cad728edc8ebc8ce4771 (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
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
/* 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 { isConsole } from "../utils/preview";
import { getGrip, getFront } from "../utils/evaluation-result";
import { getExpressionFromCoords } from "../utils/editor/get-expression";

import {
  isLineInScope,
  isSelectedFrameVisible,
  getSelectedSource,
  getSelectedLocation,
  getSelectedFrame,
  getCurrentThread,
  getSelectedException,
} from "../selectors/index";

import { getMappedExpression } from "./expressions";

async function findExpressionMatch(state, parserWorker, codeMirror, tokenPos) {
  const location = getSelectedLocation(state);
  if (!location) {
    return null;
  }

  // Fallback on expression from codemirror cursor if parser worker misses symbols
  // or is unable to find a match.
  const match = await parserWorker.findBestMatchExpression(
    location.source.id,
    tokenPos
  );
  if (match) {
    return match;
  }
  return getExpressionFromCoords(codeMirror, tokenPos);
}

export function getPreview(target, tokenPos, codeMirror) {
  return async thunkArgs => {
    const { getState, client, parserWorker } = thunkArgs;
    if (
      !isSelectedFrameVisible(getState()) ||
      !isLineInScope(getState(), tokenPos.line)
    ) {
      return null;
    }

    const source = getSelectedSource(getState());
    if (!source) {
      return null;
    }
    const thread = getCurrentThread(getState());
    const selectedFrame = getSelectedFrame(getState(), thread);
    if (!selectedFrame) {
      return null;
    }

    const match = await findExpressionMatch(
      getState(),
      parserWorker,
      codeMirror,
      tokenPos
    );
    if (!match) {
      return null;
    }

    let { expression, location } = match;

    if (isConsole(expression)) {
      return null;
    }

    if (location && source.isOriginal) {
      const mapResult = await getMappedExpression(
        expression,
        thread,
        thunkArgs
      );
      if (mapResult) {
        expression = mapResult.expression;
      }
    }

    const { result } = await client.evaluate(expression, {
      frameId: selectedFrame.id,
    });

    const resultGrip = getGrip(result);

    // Error case occurs for a token that follows an errored evaluation
    // https://github.com/firefox-devtools/debugger/pull/8056
    // Accommodating for null allows us to show preview for falsy values
    // line "", false, null, Nan, and more
    if (resultGrip === null) {
      return null;
    }

    // Handle cases where the result is invisible to the debugger
    // and not possible to preview. Bug 1548256
    if (
      resultGrip &&
      resultGrip.class &&
      typeof resultGrip.class === "string" &&
      resultGrip.class.includes("InvisibleToDebugger")
    ) {
      return null;
    }

    const root = {
      path: expression,
      contents: {
        value: resultGrip,
        front: getFront(result),
      },
    };

    return {
      target,
      tokenPos,
      cursorPos: target.getBoundingClientRect(),
      expression,
      root,
      resultGrip,
    };
  };
}

export function getExceptionPreview(target, tokenPos, codeMirror) {
  return async ({ getState, parserWorker }) => {
    const match = await findExpressionMatch(
      getState(),
      parserWorker,
      codeMirror,
      tokenPos
    );
    if (!match) {
      return null;
    }

    const tokenColumnStart = match.location.start.column + 1;
    const exception = getSelectedException(
      getState(),
      tokenPos.line,
      tokenColumnStart
    );
    if (!exception) {
      return null;
    }

    return {
      target,
      tokenPos,
      cursorPos: target.getBoundingClientRect(),
      exception,
    };
  };
}