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
158 lines (136 loc) · 4.02 KB
/
expressions.js
File metadata and controls
158 lines (136 loc) · 4.02 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
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
/* 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 {
getExpression,
getExpressions,
getSelectedFrame,
getSelectedFrameId,
getSource,
getSelectedSource,
getSelectedScopeMappings
} from "../selectors";
import { PROMISE } from "./utils/middleware/promise";
import { isGeneratedId } from "devtools-source-map";
import { wrapExpression } from "../utils/expressions";
import * as parser from "../workers/parser";
import type { Expression } from "../types";
import type { ThunkArgs } from "./types";
/**
* Add expression for debugger to watch
*
* @param {object} expression
* @param {number} expression.id
* @memberof actions/pause
* @static
*/
export function addExpression(input: string) {
return async ({ dispatch, getState }: ThunkArgs) => {
if (!input) {
return;
}
const expressionError = await parser.hasSyntaxError(input);
const expression = getExpression(getState(), input);
if (expression) {
return dispatch(evaluateExpression(expression));
}
dispatch({ type: "ADD_EXPRESSION", input, expressionError });
const newExpression = getExpression(getState(), input);
if (newExpression) {
return dispatch(evaluateExpression(newExpression));
}
};
}
export function clearExpressionError() {
return { type: "CLEAR_EXPRESSION_ERROR" };
}
export function updateExpression(input: string, expression: Expression) {
return async ({ dispatch, getState }: ThunkArgs) => {
if (!input || input == expression.input) {
return;
}
const expressionError = await parser.hasSyntaxError(input);
dispatch({
type: "UPDATE_EXPRESSION",
expression,
input: expressionError ? expression.input : input,
expressionError
});
dispatch(evaluateExpressions());
};
}
/**
*
* @param {object} expression
* @param {number} expression.id
* @memberof actions/pause
* @static
*/
export function deleteExpression(expression: Expression) {
return ({ dispatch }: ThunkArgs) => {
dispatch({
type: "DELETE_EXPRESSION",
input: expression.input
});
};
}
/**
*
* @memberof actions/pause
* @param {number} selectedFrameId
* @static
*/
export function evaluateExpressions() {
return async function({ dispatch, getState, client }: ThunkArgs) {
const expressions = getExpressions(getState());
for (const expression of expressions) {
await dispatch(evaluateExpression(expression));
}
};
}
function evaluateExpression(expression: Expression) {
return async function({ dispatch, getState, client, sourceMaps }: ThunkArgs) {
if (!expression.input) {
console.warn("Expressions should not be empty");
return;
}
let input = expression.input;
const frame = getSelectedFrame(getState());
if (frame) {
const { location } = frame;
const source = getSource(getState(), location.sourceId);
const sourceId = source.get("id");
const selectedSource = getSelectedSource(getState());
if (
selectedSource &&
!isGeneratedId(sourceId) &&
!isGeneratedId(selectedSource.get("id"))
) {
input = await dispatch(getMappedExpression(input));
}
}
const frameId = getSelectedFrameId(getState());
return dispatch({
type: "EVALUATE_EXPRESSION",
input: expression.input,
[PROMISE]: client.evaluate(wrapExpression(input), { frameId })
});
};
}
/**
* Gets information about original variable names from the source map
* and replaces all posible generated names.
*/
export function getMappedExpression(expression: string) {
return async function({ dispatch, getState, client, sourceMaps }: ThunkArgs) {
const mappings = getSelectedScopeMappings(getState());
if (!mappings) {
return expression;
}
return await dispatch({
type: "MAP_EXPRESSION_RESULT",
[PROMISE]: parser.mapOriginalExpression(expression, mappings)
});
};
}