forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (76 loc) · 2.15 KB
/
index.js
File metadata and controls
85 lines (76 loc) · 2.15 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
/* 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 {
parseSourceScopes,
type SourceScope,
type ParsedScope,
type BindingData,
type BindingLocation,
type BindingLocationType,
type BindingMetaValue,
type BindingType
} from "./visitor";
export type {
SourceScope,
BindingData,
BindingLocation,
BindingLocationType,
BindingMetaValue,
BindingType
};
import type { Location } from "../../../types";
let parsedScopesCache = new Map();
export default function getScopes(location: Location): SourceScope[] {
const { sourceId } = location;
let parsedScopes = parsedScopesCache.get(sourceId);
if (!parsedScopes) {
parsedScopes = parseSourceScopes(sourceId);
parsedScopesCache.set(sourceId, parsedScopes);
}
return parsedScopes ? findScopes(parsedScopes, location) : [];
}
export function clearScopes() {
parsedScopesCache = new Map();
}
/**
* Searches all scopes and their bindings at the specific location.
*/
function findScopes(scopes: ParsedScope[], location: Location): SourceScope[] {
// Find inner most in the tree structure.
let searchInScopes: ?(ParsedScope[]) = scopes;
const found = [];
while (searchInScopes) {
const foundOne = searchInScopes.some(s => {
if (
compareLocations(s.start, location) <= 0 &&
compareLocations(location, s.end) < 0
) {
// Found the next scope, trying to search recusevly in its children.
found.unshift(s);
searchInScopes = s.children;
return true;
}
return false;
});
if (!foundOne) {
break;
}
}
return found.map(i => {
return {
type: i.type,
displayName: i.displayName,
start: i.start,
end: i.end,
bindings: i.bindings
};
});
}
function compareLocations(a: Location, b: Location): number {
// According to type of Location.column can be undefined, if will not be the
// case here, ignoring flow error.
// $FlowIgnore
return a.line == b.line ? a.column - b.column : a.line - b.line;
}