/
symbols.js
50 lines (46 loc) · 1.55 KB
/
symbols.js
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
/**
* @typedef {Object} SymbolTable
* @property {Array<{start: number, end: number}>} spans - The spans of the symbols.
* @property {string[]} names - The names of the symbols.
* @property {string[]} flags - The flags of the symbols.
* @property {number[]} scopeIds - The scope IDs of the symbols.
* @property {number[]} declarations - The declarations of the symbols.
* @property {Array<number[]>} resolvedReferences - The resolved references of the symbols.
* @property {Array<{span: {start: number, end: number}, name: string, node_id: number, symbol_id: number|null, flag: string}>} references - The references of the symbols.
*/
/**
* @type {Array<any>}
*/
let cacheSymbols = null
/**
*
* @param {SymbolTable} symbols
* @returns
*/
export const renderSymbols = (symbols) => {
const target = []
symbols.declarations.forEach((nodeId, index) => {
target.push({
name: symbols.names[index],
flag: symbols.flags[index],
symbolId: index,
nodeId,
span: symbols.spans[index],
references: symbols.resolvedReferences[index].map((id) => ({ referenceId: id, ...symbols.references[id] })),
})
})
cacheSymbols = target
return JSON.stringify(target, null, 2)
}
export const getSymbolAndReferencesSpan = (start, end) => {
if (!cacheSymbols) {
return [{ start, end }]
}
const symbol = cacheSymbols.find((symbol) => {
return symbol.span.start == start && symbol.span.end == end
})
if (!symbol) {
return [{ start, end }]
}
return [symbol.span, ...symbol.references.map((reference) => reference.span)]
}