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
177 lines (138 loc) · 4.16 KB
/
ast.js
File metadata and controls
177 lines (138 loc) · 4.16 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
/* 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 type { AstLocation, SymbolDeclarations } from "../workers/parser";
import type { Source } from "../types";
import type { Action, DonePromiseAction } from "../actions/types";
import type { Node, Grip, GripProperties } from "devtools-reps";
type EmptyLinesType = number[];
export type LoadedSymbols = SymbolDeclarations;
export type Symbols = LoadedSymbols | {| loading: true |};
export type EmptyLinesMap = { [k: string]: EmptyLinesType };
export type SymbolsMap = { [k: string]: Symbols };
export type SourceMetaDataType = {
framework: ?string
};
export type SourceMetaDataMap = { [k: string]: SourceMetaDataType };
export type Preview = {| updating: true |} | null | PreviewValue;
export type PreviewValue = {|
expression: string,
result: Grip,
root: Node,
properties: GripProperties,
location: AstLocation,
cursorPos: any,
tokenPos: AstLocation,
updating: false
|};
export type ASTState = {
+symbols: SymbolsMap,
+outOfScopeLocations: ?Array<AstLocation>,
+inScopeLines: ?Array<number>,
+preview: Preview
};
export function initialASTState(): ASTState {
return {
symbols: {},
outOfScopeLocations: null,
inScopeLines: null,
preview: null
};
}
function update(state: ASTState = initialASTState(), action: Action): ASTState {
switch (action.type) {
case "SET_SYMBOLS": {
const { sourceId } = action;
if (action.status === "start") {
return {
...state,
symbols: { ...state.symbols, [sourceId]: { loading: true } }
};
}
const value = ((action: any): DonePromiseAction).value;
return {
...state,
symbols: { ...state.symbols, [sourceId]: value }
};
}
case "OUT_OF_SCOPE_LOCATIONS": {
return { ...state, outOfScopeLocations: action.locations };
}
case "IN_SCOPE_LINES": {
return { ...state, inScopeLines: action.lines };
}
case "CLEAR_SELECTION": {
return { ...state, preview: null };
}
case "SET_PREVIEW": {
if (action.status == "start") {
return { ...state, preview: { updating: true } };
}
if (!action.value) {
return { ...state, preview: null };
}
// NOTE: if the preview does not exist, it has been cleared
if (state.preview) {
return { ...state, preview: { ...action.value, updating: false } };
}
return state;
}
case "RESUME": {
return { ...state, outOfScopeLocations: null };
}
case "NAVIGATE": {
return initialASTState();
}
default: {
return state;
}
}
}
// NOTE: we'd like to have the app state fully typed
// https://github.com/firefox-devtools/debugger/blob/master/src/reducers/sources.js#L179-L185
type OuterState = { ast: ASTState };
export function getSymbols(state: OuterState, source: ?Source): ?Symbols {
if (!source) {
return null;
}
return state.ast.symbols[source.id] || null;
}
export function hasSymbols(state: OuterState, source: Source): boolean {
const symbols = getSymbols(state, source);
if (!symbols) {
return false;
}
return !symbols.loading;
}
export function getFramework(state: OuterState, source: Source): ?string {
const symbols = getSymbols(state, source);
if (symbols && !symbols.loading) {
return symbols.framework;
}
}
export function isSymbolsLoading(state: OuterState, source: ?Source): boolean {
const symbols = getSymbols(state, source);
if (!symbols) {
return false;
}
return symbols.loading;
}
export function getOutOfScopeLocations(state: OuterState) {
return state.ast.outOfScopeLocations;
}
export function getPreview(state: OuterState) {
return state.ast.preview;
}
export function getInScopeLines(state: OuterState) {
return state.ast.inScopeLines;
}
export function isLineInScope(state: OuterState, line: number) {
const linesInScope = state.ast.inScopeLines;
return linesInScope && linesInScope.includes(line);
}
export default update;