@codua/codeoutline-win32-x64 0.2.0 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -1
- package/README.zh-CN.md +4 -1
- package/bin/xnet.exe +0 -0
- package/build-info.json +1 -1
- package/package.json +1 -1
- package/scripts/codeoutline/callers.lua +116 -0
- package/scripts/codeoutline/command.lua +7 -3
- package/scripts/codeoutline/common.lua +21 -1
- package/scripts/codeoutline/completion.lua +93 -22
- package/scripts/codeoutline/documents.lua +1 -1
- package/scripts/codeoutline/explore.lua +39 -10
- package/scripts/codeoutline/foreign.lua +60 -0
- package/scripts/codeoutline/foreign_lua.lua +146 -0
- package/scripts/codeoutline/graph.lua +107 -54
- package/scripts/codeoutline/index.lua +7 -3
- package/scripts/codeoutline/index_worker.lua +15 -7
- package/scripts/codeoutline/indirect.lua +225 -0
- package/scripts/codeoutline/js_evidence.lua +175 -0
- package/scripts/codeoutline/js_resolution.lua +201 -0
- package/scripts/codeoutline/lang/kotlin.lua +384 -0
- package/scripts/codeoutline/lang/lua.lua +17 -0
- package/scripts/codeoutline/lsp.lua +58 -34
- package/scripts/codeoutline/lua_bindings.lua +360 -0
- package/scripts/codeoutline/main.lua +24 -3
- package/scripts/codeoutline/mcp.lua +13 -4
- package/scripts/codeoutline/navigation.lua +161 -34
- package/scripts/codeoutline/parse.lua +3 -0
- package/scripts/codeoutline/query_report.lua +36 -0
- package/scripts/codeoutline/service.lua +11 -3
- package/scripts/codeoutline/update_sequence.lua +1 -1
- package/scripts/codeoutline/version.lua +1 -1
|
@@ -27,8 +27,8 @@ local INDEX_PARSE = { ranges = false }
|
|
|
27
27
|
-- rebuilds stale caches instead of silently reusing old parse results.
|
|
28
28
|
M.VERSION = (function()
|
|
29
29
|
local parts = {}
|
|
30
|
-
for _, m in ipairs({ 'xscan', 'text', 'common', 'parse', 'lang.c', 'lang.cpp', 'lang.lua', 'lang.python',
|
|
31
|
-
'lang.java', 'lang.go', 'lang.js', 'lang.csharp', 'lang.rust' }) do
|
|
30
|
+
for _, m in ipairs({ 'xscan', 'text', 'common', 'indirect', 'js_evidence', 'lua_bindings', 'foreign', 'foreign_lua', 'parse', 'lang.c', 'lang.cpp', 'lang.lua', 'lang.python',
|
|
31
|
+
'lang.java', 'lang.kotlin', 'lang.go', 'lang.js', 'lang.csharp', 'lang.rust' }) do
|
|
32
32
|
local file = package.searchpath and package.searchpath('codeoutline.' .. m, package.path)
|
|
33
33
|
local h = file and io.open(file, 'rb')
|
|
34
34
|
if h then parts[#parts + 1] = h:read('a'); h:close() end
|
|
@@ -229,7 +229,11 @@ end
|
|
|
229
229
|
-- Symbol tables over the records, built on first use and then kept current
|
|
230
230
|
-- by commit(). Hosts that never resolve names do not pay for them.
|
|
231
231
|
function Index:symbols()
|
|
232
|
-
|
|
232
|
+
local ctx = require('codeoutline.lua_bindings').context(self)
|
|
233
|
+
if not self.symbol_tables or table.concat(self.symbol_tables.lua_global_export_tables, '\0')
|
|
234
|
+
~= table.concat(ctx.global_export_tables, '\0') then
|
|
235
|
+
self.symbol_tables = graph.build(self, ctx)
|
|
236
|
+
end
|
|
233
237
|
return self.symbol_tables
|
|
234
238
|
end
|
|
235
239
|
|
|
@@ -57,8 +57,8 @@ local function execute(req)
|
|
|
57
57
|
service.configure({ max_projects = lsp.MAX_ROOTS })
|
|
58
58
|
service.refresh(root, { full_interval = 45, paths = req.paths })
|
|
59
59
|
return true
|
|
60
|
-
elseif req.method == 'explore' then
|
|
61
|
-
local text, info = service.
|
|
60
|
+
elseif req.method == 'explore' or req.method == 'callers' then
|
|
61
|
+
local text, info = service[req.method](root, req.query, { budget = req.budget, depth = req.depth, max_nodes = req.max_nodes })
|
|
62
62
|
return { text = text, truncated = info.truncated or false, refresh = info.refresh }
|
|
63
63
|
elseif req.method == 'status' then return service.status(root)
|
|
64
64
|
elseif req.method == 'rebuild' then return service.rebuild(root)
|
|
@@ -70,7 +70,7 @@ end
|
|
|
70
70
|
-- threads; jobs arriving meanwhile queue behind it.
|
|
71
71
|
local queue, active = {}, nil
|
|
72
72
|
|
|
73
|
-
local function run(id, req)
|
|
73
|
+
local function run(id, req, reply)
|
|
74
74
|
-- Draft text applies even to a cancelled request: the session counts it as sent.
|
|
75
75
|
if req.session and req.open then
|
|
76
76
|
local synced, err = pcall(navigation.sync, req)
|
|
@@ -84,7 +84,11 @@ local function run(id, req)
|
|
|
84
84
|
control.callback = nil
|
|
85
85
|
if not ok then result = tostring(result) end
|
|
86
86
|
shared:delete('cancel:' .. id)
|
|
87
|
-
|
|
87
|
+
if reply then
|
|
88
|
+
assert(_thread_replys[reply.router](reply.co, reply.sk, 'run_rpc', ok, result))
|
|
89
|
+
else
|
|
90
|
+
assert(xthread.post(1, 'worker_result', id, ok, result))
|
|
91
|
+
end
|
|
88
92
|
end
|
|
89
93
|
|
|
90
94
|
local function resume(...)
|
|
@@ -98,7 +102,7 @@ local function pump()
|
|
|
98
102
|
local item = table.remove(queue, 1)
|
|
99
103
|
active = coroutine.create(run)
|
|
100
104
|
pool.bind(active)
|
|
101
|
-
resume(item[1], item[2])
|
|
105
|
+
resume(item[1], item[2], item[3])
|
|
102
106
|
end
|
|
103
107
|
end
|
|
104
108
|
|
|
@@ -115,8 +119,12 @@ return {
|
|
|
115
119
|
pool.sweep()
|
|
116
120
|
if not active then service.sweep(); service.collect() end
|
|
117
121
|
end,
|
|
118
|
-
__thread_handle = function(
|
|
119
|
-
if
|
|
122
|
+
__thread_handle = function(router, op, id, req, failure, rpc_request)
|
|
123
|
+
if type(router) == 'string' and req == 'run_rpc' then
|
|
124
|
+
-- RPC wire: router, coroutine id, session key, method, job id, request.
|
|
125
|
+
queue[#queue + 1] = { failure, rpc_request, { router = router, co = op, sk = id } }
|
|
126
|
+
pump()
|
|
127
|
+
elseif op == 'run' then
|
|
120
128
|
queue[#queue + 1] = { id, req }
|
|
121
129
|
pump()
|
|
122
130
|
elseif op == 'parsed' then
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
-- Explicit dispatch contracts. No wildcard API guesses or source evaluation.
|
|
2
|
+
local control = require('codeoutline.control')
|
|
3
|
+
local M = {}
|
|
4
|
+
local member = { ['.'] = true, [':'] = true, ['::'] = true, ['->'] = true }
|
|
5
|
+
|
|
6
|
+
local function value(T, a, b)
|
|
7
|
+
if a == b and T.k[a] == 'str' then
|
|
8
|
+
local raw = T:text(a)
|
|
9
|
+
local q = raw:sub(1, 1)
|
|
10
|
+
if (q == '"' or q == "'") and raw:sub(-1) == q then
|
|
11
|
+
local body = raw:sub(2, -2)
|
|
12
|
+
-- Reject escapes/interpolation instead of silently mis-decoding keys.
|
|
13
|
+
if not body:find('[\\$]') then return { literal = body } end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
if T:text(a) == '&' then a = a + 1 end
|
|
17
|
+
if a > b or (b - a) % 2 ~= 0 or T.k[a] ~= 'id' then return false end
|
|
18
|
+
local parts = { T:text(a) }
|
|
19
|
+
for i = a + 1, b, 2 do
|
|
20
|
+
if not member[T:text(i)] or T.k[i + 1] ~= 'id' then return false end
|
|
21
|
+
parts[#parts + 1] = T:text(i); parts[#parts + 1] = T:text(i + 1)
|
|
22
|
+
end
|
|
23
|
+
return { symbol = table.concat(parts) }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
function M.collect(r, T)
|
|
27
|
+
local sites, calls = {}, {}
|
|
28
|
+
for index, ref in ipairs(r.refs) do
|
|
29
|
+
if index % 128 == 0 then control.check() end
|
|
30
|
+
local x = ref.tok
|
|
31
|
+
if (ref.kind == 'call' or ref.kind == 'member_call') and T:text(x + 1) == '(' then
|
|
32
|
+
local close = T:match(x + 1)
|
|
33
|
+
if close then
|
|
34
|
+
local first = x
|
|
35
|
+
while first > 2 and member[T:text(first - 1)] and T.k[first - 2] == 'id' do first = first - 2 end
|
|
36
|
+
local call = value(T, first, x)
|
|
37
|
+
if call then calls[call.symbol] = true end
|
|
38
|
+
if not r.collect_dispatch or (type(r.collect_dispatch) == 'table' and not r.collect_dispatch[call and call.symbol]) then goto next_ref end
|
|
39
|
+
local args, a, i = {}, x + 2, x + 2
|
|
40
|
+
while i <= close do
|
|
41
|
+
if i == close or T:text(i) == ',' then
|
|
42
|
+
args[#args + 1] = value(T, a, i - 1)
|
|
43
|
+
a = i + 1
|
|
44
|
+
elseif T:match(i) and T:match(i) > i then i = T:match(i) end
|
|
45
|
+
i = i + 1
|
|
46
|
+
end
|
|
47
|
+
if call then sites[#sites + 1] = { call = call.symbol, args = args, from = ref.from, line = ref.line } end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
::next_ref::
|
|
51
|
+
end
|
|
52
|
+
local names = {}
|
|
53
|
+
for name in pairs(calls) do names[#names + 1] = name end
|
|
54
|
+
table.sort(names)
|
|
55
|
+
if #names > 0 then r.dispatch_calls = names end
|
|
56
|
+
if #sites > 0 then r.dispatch_sites = sites end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
local function rules_for(G, rules)
|
|
60
|
+
if rules == nil and G.root then
|
|
61
|
+
local f = io.open(G.root .. '/.codeoutline.json', 'rb')
|
|
62
|
+
if f then
|
|
63
|
+
local raw = f:read(65537); f:close()
|
|
64
|
+
assert(#raw <= 65536, '.codeoutline.json exceeds 64 KiB')
|
|
65
|
+
local ok, config = pcall(xutils.json_unpack, raw)
|
|
66
|
+
assert(ok and type(config) == 'table', 'invalid .codeoutline.json')
|
|
67
|
+
rules = config.dispatchRules
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
rules = rules or {}
|
|
71
|
+
assert(type(rules) == 'table' and #rules <= 32, 'dispatchRules must contain at most 32 rules')
|
|
72
|
+
for _, r in ipairs(rules) do
|
|
73
|
+
assert(type(r) == 'table' and type(r.call) == 'string' and r.call:match('^[%a_][%w_%.:>%-]*$'), 'invalid dispatchRules.call')
|
|
74
|
+
assert(r.kind == 'target' or r.kind == 'callback' or r.kind == 'event', 'invalid dispatchRules.kind')
|
|
75
|
+
assert(r.language == nil or type(r.language) == 'string', 'invalid dispatchRules.language')
|
|
76
|
+
for _, key in ipairs(r.kind == 'event' and { 'argument', 'keyArgument', 'handlerArgument' } or { 'argument' }) do
|
|
77
|
+
local n = r[key]
|
|
78
|
+
assert(type(n) == 'number' and n >= 1 and n <= 32 and n % 1 == 0, 'invalid dispatchRules.' .. key)
|
|
79
|
+
end
|
|
80
|
+
if r.kind == 'event' then assert(type(r.register) == 'string' and r.register:match('^[%a_][%w_%.:>%-]*$'), 'invalid dispatchRules.register') end
|
|
81
|
+
end
|
|
82
|
+
return rules
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
function M.query(G, rules, report)
|
|
86
|
+
rules = rules_for(G, rules)
|
|
87
|
+
if #rules == 0 then return { outgoing = function() return {} end, incoming = function() return {} end } end
|
|
88
|
+
local graph = require('codeoutline.graph')
|
|
89
|
+
local names, cache, order, bytes, registrations_by_rule = {}, {}, {}, 0, {}
|
|
90
|
+
local dependencies = {}
|
|
91
|
+
for _, rule in ipairs(rules) do names[rule.call] = true; if rule.register then names[rule.register] = true end end
|
|
92
|
+
local function candidate(rec, call)
|
|
93
|
+
if rec.dispatch_sites then return true end -- explicit in-memory parse option
|
|
94
|
+
for _, name in ipairs(rec.dispatch_calls or {}) do if name == call then return true end end
|
|
95
|
+
return false
|
|
96
|
+
end
|
|
97
|
+
local function sites(f)
|
|
98
|
+
local rec = G.files[f]
|
|
99
|
+
if rec.dispatch_sites then return rec.dispatch_sites end
|
|
100
|
+
if cache[f] then return cache[f].sites end
|
|
101
|
+
assert(G.root, 'indirect resolution requires a project root or explicit dispatch parse data')
|
|
102
|
+
local file = assert(io.open(G.root .. '/' .. rec.path, 'rb'), 'cannot read indirect source: ' .. rec.path)
|
|
103
|
+
local raw = file:read('a'); file:close()
|
|
104
|
+
local crc = xcompress and xcompress.crc32 and xcompress.crc32(raw) or xutils.sha256_hex(raw)
|
|
105
|
+
assert(#raw == rec.size and crc == rec.crc, 'source changed during query; retry: ' .. rec.path)
|
|
106
|
+
local source = raw
|
|
107
|
+
if rec.encoding ~= 'utf-8' and rec.encoding ~= 'utf-8-bom' then
|
|
108
|
+
source = assert(xutils.to_utf8(raw, rec.encoding == 'gbk' and 'gbk' or 'auto'))
|
|
109
|
+
end
|
|
110
|
+
local parsed = assert(require('codeoutline.parse').parse(rec.path, source,
|
|
111
|
+
{ ranges = false, dispatch = names, dispatch_only = true }))
|
|
112
|
+
assert(#parsed.nodes == #rec.nodes, 'source encoding changed; refresh index before indirect query: ' .. rec.path)
|
|
113
|
+
for i, n in ipairs(parsed.nodes) do
|
|
114
|
+
assert(n.qualified == rec.nodes[i].qualified and n.line == rec.nodes[i].line,
|
|
115
|
+
'source encoding changed; refresh index before indirect query: ' .. rec.path)
|
|
116
|
+
end
|
|
117
|
+
local result = parsed.dispatch_sites or {}
|
|
118
|
+
local weight = #source + #result * 512
|
|
119
|
+
-- Query-local FIFO: no retained source/tokens and no cross-query invalidation.
|
|
120
|
+
while #order > 0 and (#order >= 16 or bytes + weight > 8 * 1024 * 1024) do
|
|
121
|
+
local old = table.remove(order, 1); bytes = bytes - cache[old].weight; cache[old] = nil
|
|
122
|
+
end
|
|
123
|
+
if weight <= 8 * 1024 * 1024 then
|
|
124
|
+
cache[f] = { sites = result, weight = weight }; order[#order + 1] = f; bytes = bytes + weight
|
|
125
|
+
end
|
|
126
|
+
return result
|
|
127
|
+
end
|
|
128
|
+
local function targets(f, site, arg, literal)
|
|
129
|
+
local name
|
|
130
|
+
if arg then
|
|
131
|
+
if literal then name = arg.literal else name = arg.symbol end
|
|
132
|
+
end
|
|
133
|
+
if not name or not name:match('^[%a_][%w_%.:>%-]*$') then return {} end
|
|
134
|
+
local short = name:match('([%w_]+)$')
|
|
135
|
+
local dependency = dependencies[f]
|
|
136
|
+
if not dependency then
|
|
137
|
+
local deps, paired = graph.dependencies(G, f)
|
|
138
|
+
dependency = { deps, paired }; dependencies[f] = dependency
|
|
139
|
+
end
|
|
140
|
+
local deps, paired = dependency[1], dependency[2]
|
|
141
|
+
local view = G
|
|
142
|
+
if short ~= name then
|
|
143
|
+
local filtered = {}
|
|
144
|
+
for _, id in ipairs(G.by_name[short] or {}) do
|
|
145
|
+
if G.nodes[id].qualified == name then filtered[#filtered + 1] = id end
|
|
146
|
+
end
|
|
147
|
+
view = setmetatable({ by_name = { [short] = filtered } }, { __index = G })
|
|
148
|
+
end
|
|
149
|
+
return graph.resolve(view, f, G.id_of[f][site.from], short, 'ref', nil, deps, paired) or {}
|
|
150
|
+
end
|
|
151
|
+
local function registrations(ri, rule)
|
|
152
|
+
if registrations_by_rule[ri] then return registrations_by_rule[ri] end
|
|
153
|
+
local registrations = {}
|
|
154
|
+
if rule.kind == 'event' then
|
|
155
|
+
for _, f in ipairs(G.file_order) do
|
|
156
|
+
control.check()
|
|
157
|
+
local rec = G.files[f]
|
|
158
|
+
if (not rule.language or rule.language == rec.language) and candidate(rec, rule.register) then
|
|
159
|
+
for _, site in ipairs(sites(f)) do
|
|
160
|
+
local key = site.args[rule.keyArgument]
|
|
161
|
+
if site.call == rule.register and key and key.literal then
|
|
162
|
+
local bucket = rec.language .. '\0' .. key.literal
|
|
163
|
+
registrations[bucket] = registrations[bucket] or {}
|
|
164
|
+
for _, id in ipairs(targets(f, site, site.args[rule.handlerArgument])) do
|
|
165
|
+
registrations[bucket][id] = registrations[bucket][id]
|
|
166
|
+
or (report and (rec.path .. ':' .. site.line) or true)
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
registrations_by_rule[ri] = registrations
|
|
174
|
+
return registrations
|
|
175
|
+
end
|
|
176
|
+
local function scan(only, wanted)
|
|
177
|
+
local out = {}
|
|
178
|
+
local files = only and { G.node_file[only] } or G.file_order
|
|
179
|
+
for _, f in ipairs(files) do
|
|
180
|
+
control.check()
|
|
181
|
+
local rec, file_sites = G.files[f], nil
|
|
182
|
+
for ri, rule in ipairs(rules) do
|
|
183
|
+
if (not rule.language or rule.language == rec.language) and candidate(rec, rule.call) then
|
|
184
|
+
-- Process every applicable rule while this file is available,
|
|
185
|
+
-- even if event registration scans evict it from the FIFO.
|
|
186
|
+
file_sites = file_sites or sites(f)
|
|
187
|
+
for _, site in ipairs(file_sites) do
|
|
188
|
+
if site.call == rule.call and site.from > 0 and (not only or G.id_of[f][site.from] == only) then
|
|
189
|
+
local ids, arg, registered = {}, site.args[rule.argument], nil
|
|
190
|
+
if rule.kind == 'event' then
|
|
191
|
+
registered = registrations(ri, rule)
|
|
192
|
+
for id in pairs(arg and arg.literal and registered[rec.language .. '\0' .. arg.literal] or {}) do ids[#ids + 1] = id end
|
|
193
|
+
table.sort(ids, function(a, b) return graph.before(G, a, b) end)
|
|
194
|
+
else ids = targets(f, site, arg, rule.kind == 'target') end
|
|
195
|
+
local src = G.id_of[f][site.from]
|
|
196
|
+
if report and only and #ids == 0 then
|
|
197
|
+
local static = arg and (rule.kind == 'callback' and arg.symbol or rule.kind ~= 'callback' and arg.literal)
|
|
198
|
+
report:add({ source = src, line = site.line, rule = ri, argument = rule.argument, call = rule.call,
|
|
199
|
+
reason = not static and 'dispatch argument is dynamic, unsupported or missing'
|
|
200
|
+
or rule.kind == 'event' and 'no resolved registration for event key'
|
|
201
|
+
or 'dispatch target unresolved or ambiguous' })
|
|
202
|
+
end
|
|
203
|
+
for _, id in ipairs(ids) do
|
|
204
|
+
if not wanted or wanted[id] then
|
|
205
|
+
if report then
|
|
206
|
+
local bucket = registered and arg and arg.literal and registered[rec.language .. '\0' .. arg.literal]
|
|
207
|
+
report:add({ source = src, target = id, line = site.line, rule = ri, argument = rule.argument,
|
|
208
|
+
call = rule.call, kind = 'configured ' .. rule.kind,
|
|
209
|
+
registered_at = bucket and bucket[id] or nil })
|
|
210
|
+
end
|
|
211
|
+
out[src] = out[src] or {}
|
|
212
|
+
out[src][#out[src] + 1] = { target = id, line = site.line, kind = 'indirect:' .. rule.kind .. '#' .. ri }
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
return out
|
|
221
|
+
end
|
|
222
|
+
return { outgoing = function(id) return scan(id)[id] or {} end, incoming = function(wanted) return scan(nil, wanted) end }
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
return M
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
-- Token-derived ES module bindings and bounded lexical receiver declarations.
|
|
2
|
+
local M = {}
|
|
3
|
+
local control = require('codeoutline.control')
|
|
4
|
+
local function identifier(T, i) return T.k[i] == 'id' or T:text(i) == 'default' end
|
|
5
|
+
local function literal(T, i)
|
|
6
|
+
if T.k[i] ~= 'str' then return nil end
|
|
7
|
+
local s = T:text(i)
|
|
8
|
+
if s:find('\\', 1, true) then return nil end
|
|
9
|
+
return s:sub(2, -2)
|
|
10
|
+
end
|
|
11
|
+
local function type_name(T, i)
|
|
12
|
+
if T.k[i] ~= 'id' then return nil end
|
|
13
|
+
local name = T:text(i)
|
|
14
|
+
if T:text(i + 1) == '.' and T.k[i + 2] == 'id' then return name .. '.' .. T:text(i + 2), i + 3 end
|
|
15
|
+
return name, i + 1
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
function M.collect(r, T)
|
|
19
|
+
if r.language ~= 'javascript' and r.language ~= 'typescript' then return end
|
|
20
|
+
r.js_imports, r.js_exports = {}, {}
|
|
21
|
+
local skip = {}
|
|
22
|
+
local function export(name, local_name, path)
|
|
23
|
+
r.js_exports[#r.js_exports + 1] = { name = name, local_name = local_name, path = path }
|
|
24
|
+
end
|
|
25
|
+
for i = 1, T.n do
|
|
26
|
+
if i % 4096 == 0 then control.check() end
|
|
27
|
+
local word = T:text(i)
|
|
28
|
+
if (word == 'import' or word == 'export') and T.k[i] == 'kw' then
|
|
29
|
+
local x = i + 1
|
|
30
|
+
if T:text(x) == 'type' then x = x + 1 end
|
|
31
|
+
if T:text(x) ~= '(' then
|
|
32
|
+
local entries, star = {}, false
|
|
33
|
+
if word == 'import' and T.k[x] == 'id' then
|
|
34
|
+
entries[#entries + 1] = { local_name = T:text(x), imported = 'default' }; x = x + 1
|
|
35
|
+
if T:text(x) == ',' then x = x + 1 end
|
|
36
|
+
end
|
|
37
|
+
if T:text(x) == '*' then
|
|
38
|
+
star = true; x = x + 1
|
|
39
|
+
if T:text(x) == 'as' and T.k[x + 1] == 'id' then
|
|
40
|
+
entries[#entries + 1] = { local_name = T:text(x + 1), imported = '*' }; x = x + 2
|
|
41
|
+
end
|
|
42
|
+
elseif T:text(x) == '{' and T.m[x] then
|
|
43
|
+
local close = T.m[x]; x = x + 1
|
|
44
|
+
while x < close do
|
|
45
|
+
if T:text(x) == 'type' and identifier(T, x + 1) then x = x + 1 end
|
|
46
|
+
if identifier(T, x) then
|
|
47
|
+
local original, alias = T:text(x), T:text(x); x = x + 1
|
|
48
|
+
if T:text(x) == 'as' and identifier(T, x + 1) then alias = T:text(x + 1); x = x + 2 end
|
|
49
|
+
entries[#entries + 1] = { local_name = alias, imported = original }
|
|
50
|
+
else x = x + 1 end
|
|
51
|
+
end
|
|
52
|
+
x = close + 1
|
|
53
|
+
end
|
|
54
|
+
local path = T:text(x) == 'from' and literal(T, x + 1) or nil
|
|
55
|
+
if word == 'import' and path then
|
|
56
|
+
for _, entry in ipairs(entries) do entry.path = path; r.js_imports[#r.js_imports + 1] = entry end
|
|
57
|
+
for n = i, x + 1 do skip[n] = true end
|
|
58
|
+
elseif word == 'export' then
|
|
59
|
+
for _, entry in ipairs(entries) do export(entry.local_name, entry.imported, path) end
|
|
60
|
+
if star and #entries == 0 and path then export('*', '*', path) end
|
|
61
|
+
if #entries > 0 or star then for n = i, x do skip[n] = true end end
|
|
62
|
+
if T:text(i + 1) == 'default' and T.k[i + 2] == 'id' then export('default', T:text(i + 2)) end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
for _, n in ipairs(r.nodes) do
|
|
68
|
+
if not n.parent then
|
|
69
|
+
local a = n.ti
|
|
70
|
+
-- Variable nodes may begin at the binding rather than `export const`.
|
|
71
|
+
while a > 1 and T.l[a - 1] == T.l[n.ti] and
|
|
72
|
+
(T:text(a - 1) == 'const' or T:text(a - 1) == 'let' or T:text(a - 1) == 'var'
|
|
73
|
+
or T:text(a - 1) == 'export' or T:text(a - 1) == 'default' or T:text(a - 1) == 'async') do a = a - 1 end
|
|
74
|
+
if T:text(a) == 'export' then export(T:text(a + 1) == 'default' and 'default' or n.name, n.name) end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
-- Braces provide block lifetime; node ranges extend parameter bindings to bodies.
|
|
79
|
+
local scopes, stack = {}, {}
|
|
80
|
+
for i = 1, T.n do
|
|
81
|
+
if T:text(i) == '{' and T.m[i] then stack[#stack + 1] = i end
|
|
82
|
+
scopes[i] = stack[#stack] or 0
|
|
83
|
+
if stack[#stack] and T.m[stack[#stack]] == i then stack[#stack] = nil end
|
|
84
|
+
end
|
|
85
|
+
local bindings, parameters = {}, {}
|
|
86
|
+
local callable_at = {}
|
|
87
|
+
for _, n in ipairs(r.nodes) do
|
|
88
|
+
if n.kind == 'function' or n.kind == 'method' or n.kind == 'constructor' then
|
|
89
|
+
for i = n.ti, n.tj do
|
|
90
|
+
if T:text(i) == '(' and T.m[i] then
|
|
91
|
+
local j, close = i + 1, T.m[i]
|
|
92
|
+
while j < close do
|
|
93
|
+
if T.k[j] == 'id' and (j == i + 1 or T:text(j - 1) == ',') then parameters[j] = true end
|
|
94
|
+
if T.m[j] and T.m[j] > j then j = T.m[j] end
|
|
95
|
+
j = j + 1
|
|
96
|
+
end
|
|
97
|
+
break
|
|
98
|
+
elseif T:text(i) == '{' then break end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
local function scope_for(i, parameter)
|
|
103
|
+
if parameter then
|
|
104
|
+
local best
|
|
105
|
+
for _, n in ipairs(r.nodes) do
|
|
106
|
+
if (n.kind == 'function' or n.kind == 'method' or n.kind == 'constructor') and n.ti <= i and n.tj >= i
|
|
107
|
+
and (not best or n.tj - n.ti < best.tj - best.ti) then best = n end
|
|
108
|
+
end
|
|
109
|
+
if best then return best.ti, best.tj end
|
|
110
|
+
end
|
|
111
|
+
local s = scopes[i]
|
|
112
|
+
return s, s > 0 and T.m[s] or T.n
|
|
113
|
+
end
|
|
114
|
+
for index, n in ipairs(r.nodes) do
|
|
115
|
+
if n.kind == 'function' then
|
|
116
|
+
for i = n.ti, math.min(n.tj, n.ti + 5) do
|
|
117
|
+
if T:text(i) == n.name and (T:text(i - 1) == 'function' or T:text(i - 1) == 'const'
|
|
118
|
+
or T:text(i - 1) == 'let' or T:text(i - 1) == 'var') then
|
|
119
|
+
callable_at[i] = index
|
|
120
|
+
if T:text(i - 1) == 'function' then
|
|
121
|
+
local a, b = scope_for(n.ti)
|
|
122
|
+
bindings[#bindings + 1] = { name = n.name, type = '?', token = i, a = a, b = b,
|
|
123
|
+
declaration = true, target = index, hoisted = true }
|
|
124
|
+
end
|
|
125
|
+
break
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
for i = 1, T.n do
|
|
131
|
+
if T.k[i] == 'id' and not skip[i] then
|
|
132
|
+
local prev, next = T:text(i - 1), T:text(i + 1)
|
|
133
|
+
local declaration = prev == 'const' or prev == 'let' or prev == 'var'
|
|
134
|
+
local parameter = parameters[i]
|
|
135
|
+
local assignment = next == '=' and prev ~= '.' and prev ~= '?.'
|
|
136
|
+
if declaration or parameter or assignment then
|
|
137
|
+
local typ, after
|
|
138
|
+
if next == ':' then
|
|
139
|
+
typ, after = type_name(T, i + 2)
|
|
140
|
+
-- Union/array/generic types require more than a simple type name.
|
|
141
|
+
if after and T:text(after) ~= '=' and T:text(after) ~= ',' and T:text(after) ~= ')' and T:text(after) ~= ';' then typ = nil end
|
|
142
|
+
elseif next == '=' and T:text(i + 2) == 'new' then typ = type_name(T, i + 3) end
|
|
143
|
+
local a, b = scope_for(i, parameter)
|
|
144
|
+
bindings[#bindings + 1] = { name = T:text(i), type = typ or '?', token = i, a = a, b = b,
|
|
145
|
+
declaration = declaration or parameter, target = declaration and callable_at[i] or nil }
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
local by_name = {}
|
|
150
|
+
for _, binding in ipairs(bindings) do
|
|
151
|
+
by_name[binding.name] = by_name[binding.name] or {}
|
|
152
|
+
by_name[binding.name][#by_name[binding.name] + 1] = binding
|
|
153
|
+
end
|
|
154
|
+
for index, ref in ipairs(r.refs) do
|
|
155
|
+
if index % 128 == 0 then control.check() end
|
|
156
|
+
local target = ref.recv or ref.name
|
|
157
|
+
local best
|
|
158
|
+
for _, binding in ipairs(by_name[target] or {}) do
|
|
159
|
+
if binding.name == target and binding.a <= ref.tok and binding.b >= ref.tok
|
|
160
|
+
and (binding.token <= ref.tok or binding.declaration) then
|
|
161
|
+
if not best or binding.b - binding.a < best.b - best.a
|
|
162
|
+
or (binding.a == best.a and binding.b == best.b and binding.token > best.token and binding.token <= ref.tok) then best = binding end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
if best then
|
|
166
|
+
if not ref.recv and best.target and (best.token <= ref.tok or best.hoisted) then ref.js_receiver = 'node:' .. best.target
|
|
167
|
+
else ref.js_receiver = ref.recv and best.token <= ref.tok and best.type or '?' end
|
|
168
|
+
end
|
|
169
|
+
if ref.kind == 'member_call' and (not ref.recv or T:text(ref.tok - 3) == '.' or T:text(ref.tok - 3) == '?.') then
|
|
170
|
+
ref.js_receiver = '?' -- A field/call-result chain is not the local variable sharing its tail.
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
return M
|