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
170 lines (141 loc) · 3.76 KB
/
ast.js
File metadata and controls
170 lines (141 loc) · 3.76 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
// @flow
/* 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/. */
/**
* Ast reducer
* @module reducers/ast
*/
import * as I from "immutable";
import makeRecord from "../utils/makeRecord";
import type { SymbolDeclarations, AstLocation } from "../workers/parser/types";
import type { Map } from "immutable";
import type { Source } from "../types";
import type { Action } from "../actions/types";
import type { Record } from "../utils/makeRecord";
type EmptyLinesType = number[];
export type SymbolsMap = Map<string, SymbolDeclarations>;
export type EmptyLinesMap = Map<string, EmptyLinesType>;
export type Preview =
| {| updating: true |}
| null
| {|
updating: false,
expression: string,
location: AstLocation,
cursorPos: any,
tokenPos: AstLocation,
result: Object
|};
export type ASTState = {
symbols: SymbolsMap,
emptyLines: EmptyLinesMap,
outOfScopeLocations: ?Array<AstLocation>,
preview: Preview
};
export function initialState() {
return makeRecord(
({
symbols: I.Map(),
emptyLines: I.Map(),
outOfScopeLocations: null,
preview: null
}: ASTState)
)();
}
function update(
state: Record<ASTState> = initialState(),
action: Action
): Record<ASTState> {
switch (action.type) {
case "SET_SYMBOLS": {
const { source, symbols } = action;
return state.setIn(["symbols", source.id], symbols);
}
case "SET_EMPTY_LINES": {
const { source, emptyLines } = action;
return state.setIn(["emptyLines", source.id], emptyLines);
}
case "OUT_OF_SCOPE_LOCATIONS": {
return state.set("outOfScopeLocations", action.locations);
}
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);
}
const {
expression,
location,
result,
tokenPos,
cursorPos,
extra
} = action.value;
return state.set("preview", {
updating: false,
expression,
location,
result,
tokenPos,
cursorPos,
extra
});
}
case "RESUMED": {
return state.set("outOfScopeLocations", null);
}
case "NAVIGATE": {
return initialState();
}
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
): SymbolDeclarations {
const emptySet = { variables: [], functions: [] };
if (!source) {
return emptySet;
}
const symbols = state.ast.getIn(["symbols", source.id]);
return symbols || emptySet;
}
export function hasSymbols(state: OuterState, source: Source): boolean {
if (!source) {
return false;
}
return !!state.ast.getIn(["symbols", source.id]);
}
export function isEmptyLineInSource(
state: OuterState,
line: number,
selectedSource: Source
) {
const emptyLines = getEmptyLines(state, selectedSource);
return emptyLines.includes(line);
}
export function getEmptyLines(state: OuterState, source: Source) {
if (!source) {
return [];
}
return state.ast.getIn(["emptyLines", source.id]) || [];
}
export function getOutOfScopeLocations(state: OuterState) {
return state.ast.get("outOfScopeLocations");
}
export function getPreview(state: OuterState) {
return state.ast.get("preview");
}
export default update;