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
86 lines (73 loc) · 1.88 KB
/
expressions.js
File metadata and controls
86 lines (73 loc) · 1.88 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
/* 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 { correctIndentation } from "./indentation";
import type { Expression } from "../types";
// replace quotes that could interfere with the evaluation.
export function sanitizeInput(input: string) {
return input.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
}
`);
}
function isUnavailable(value) {
if (!value.preview || !value.preview.name) {
return false;
}
return ["ReferenceError", "TypeError"].includes(value.preview.name);
}
export function getValue(expression: Expression) {
const value = expression.value;
if (!value) {
return {
path: expression.from,
value: { unavailable: true }
};
}
if (value.exception) {
if (isUnavailable(value.exception)) {
return { value: { unavailable: true } };
}
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;
if (isUnavailable(value.result)) {
return { value: { unavailable: true } };
}
const newValue = `${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
};
}