forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.js
More file actions
254 lines (205 loc) · 5.98 KB
/
ast.js
File metadata and controls
254 lines (205 loc) · 5.98 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* 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
/**
* Ast reducer
* @module reducers/ast
*/
import * as I from "immutable";
import makeRecord from "../utils/makeRecord";
import { findEmptyLines } from "../utils/ast";
import type {
AstLocation,
SymbolDeclarations,
PausePoints,
PausePoint
} from "../workers/parser";
import type { Map } from "immutable";
import type { Source, Location } from "../types";
import type { Action, DonePromiseAction } from "../actions/types";
import type { Record } from "../utils/makeRecord";
type EmptyLinesType = number[];
export type Symbols = SymbolDeclarations | {| loading: true |};
export type SymbolsMap = Map<string, Symbols>;
export type EmptyLinesMap = Map<string, EmptyLinesType>;
export type SourceMetaDataType = {
framework: string | void
};
export type SourceMetaDataMap = Map<string, SourceMetaDataType>;
export type PausePointsMap = Map<string, PausePoints>;
export type Preview =
| {| updating: true |}
| null
| {|
updating: false,
expression: string,
location: AstLocation,
cursorPos: any,
tokenPos: AstLocation,
result: Object,
extra: Object
|};
export type ASTState = {
symbols: SymbolsMap,
emptyLines: EmptyLinesMap,
outOfScopeLocations: ?Array<AstLocation>,
inScopeLines: ?Array<Number>,
preview: Preview,
pausePoints: PausePointsMap,
sourceMetaData: SourceMetaDataMap
};
export function initialASTState() {
return makeRecord(
({
symbols: I.Map(),
emptyLines: I.Map(),
outOfScopeLocations: null,
inScopeLines: null,
preview: null,
pausePoints: I.Map(),
sourceMetaData: I.Map()
}: ASTState)
)();
}
function update(
state: Record<ASTState> = initialASTState(),
action: Action
): Record<ASTState> {
switch (action.type) {
case "SET_SYMBOLS": {
const { sourceId } = action;
if (action.status === "start") {
return state.setIn(["symbols", sourceId], { loading: true });
}
const value = ((action: any): DonePromiseAction).value;
return state.setIn(["symbols", sourceId], value);
}
case "SET_PAUSE_POINTS": {
const { sourceText, sourceId, pausePoints } = action;
const emptyLines = findEmptyLines(sourceText, pausePoints);
return state
.setIn(["pausePoints", sourceId], pausePoints)
.setIn(["emptyLines", sourceId], emptyLines);
}
case "OUT_OF_SCOPE_LOCATIONS": {
return state.set("outOfScopeLocations", action.locations);
}
case "IN_SCOPE_LINES": {
return state.set("inScopeLines", action.lines);
}
case "CLEAR_SELECTION": {
return state.set("preview", null);
}
case "SET_PREVIEW": {
if (action.status == "start") {
return state.set("preview", { updating: true });
}
if (!action.value) {
return state.set("preview", null);
}
return state.set("preview", {
...action.value,
updating: false
});
}
case "RESUME": {
return state.set("outOfScopeLocations", null);
}
case "NAVIGATE": {
return initialASTState();
}
case "SET_SOURCE_METADATA": {
return state.setIn(
["sourceMetaData", action.sourceId],
action.sourceMetaData
);
}
default: {
return state;
}
}
}
// NOTE: we'd like to have the app state fully typed
// https://github.com/devtools-html/debugger.html/blob/master/src/reducers/sources.js#L179-L185
type OuterState = { ast: Record<ASTState> };
export function getSymbols(state: OuterState, source: Source): ?Symbols {
if (!source) {
return null;
}
return state.ast.symbols.get(source.id) || null;
}
export function hasSymbols(state: OuterState, source: Source): boolean {
const symbols = getSymbols(state, source);
if (!symbols) {
return false;
}
return !symbols.hasOwnProperty("loading");
}
export function isSymbolsLoading(state: OuterState, source: Source): boolean {
const symbols = getSymbols(state, source);
if (!symbols) {
return false;
}
return symbols.hasOwnProperty("loading");
}
export function isEmptyLineInSource(
state: OuterState,
line: number,
selectedSource: Source
) {
const emptyLines = getEmptyLines(state, selectedSource);
return emptyLines && emptyLines.includes(line);
}
export function getEmptyLines(state: OuterState, source: Source) {
if (!source) {
return null;
}
return state.ast.emptyLines.get(source.id);
}
export function getPausePoints(
state: OuterState,
sourceId: string
): ?PausePoints {
return state.ast.pausePoints.get(sourceId);
}
export function getPausePoint(
state: OuterState,
location: ?Location
): ?PausePoint {
if (!location) {
return;
}
const { column, line, sourceId } = location;
const pausePoints = getPausePoints(state, sourceId);
if (!pausePoints) {
return;
}
const linePoints = pausePoints[line];
return linePoints && linePoints[column];
}
export function hasPausePoints(state: OuterState, sourceId: string): boolean {
const pausePoints = getPausePoints(state, sourceId);
return !!pausePoints;
}
export function getOutOfScopeLocations(state: OuterState) {
return state.ast.get("outOfScopeLocations");
}
export function getPreview(state: OuterState) {
return state.ast.get("preview");
}
const emptySourceMetaData = {};
export function getSourceMetaData(state: OuterState, sourceId: string) {
return state.ast.sourceMetaData.get(sourceId) || emptySourceMetaData;
}
export function hasSourceMetaData(state: OuterState, sourceId: string) {
return state.ast.hasIn(["sourceMetaData", sourceId]);
}
export function getInScopeLines(state: OuterState) {
return state.ast.get("inScopeLines");
}
export function isLineInScope(state: OuterState, line: number) {
const linesInScope = state.ast.get("inScopeLines");
return linesInScope && linesInScope.includes(line);
}
export default update;