forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpressions.js
More file actions
69 lines (59 loc) · 1.42 KB
/
expressions.js
File metadata and controls
69 lines (59 loc) · 1.42 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
// @flow
import { correctIndentation } from "./indentation";
import type { Expression } from "debugger-html";
// replace quotes and slashes that could interfere with the evaluation.
export function sanitizeInput(input: string) {
return input.replace(/\\/g, "\\\\").replace(/"/g, '"');
}
/*
* wrap the expression input in a try/catch so that it can be safely
* evaluated.
*
* NOTE: we add line after the expression to protect against comments.
*/
export function wrapExpression(input: string) {
return correctIndentation(`
try {
${sanitizeInput(input)}
} catch (e) {
e
}
`);
}
export function getValue(expression: Expression) {
const value = expression.value;
if (!value) {
return {
path: expression.from,
value: { unavailable: true }
};
}
if (value.exception) {
return {
path: value.from,
value: value.exception
};
}
if (value.error) {
return {
path: value.from,
value: value.error
};
}
if (value.result && value.result.class == "Error") {
const { name, message } = value.result.preview;
const newValue =
name === "ReferenceError" ? { unavailable: true } : `${name}: ${message}`;
return { path: value.input, value: newValue };
}
if (typeof value.result == "object") {
return {
path: value.result.actor,
value: value.result
};
}
return {
path: value.input,
value: value.result
};
}