@codua/codeoutline-win32-x64 0.1.9 → 0.2.1

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.
Files changed (40) hide show
  1. package/README.md +38 -6
  2. package/README.zh-CN.md +36 -6
  3. package/THIRD_PARTY.md +3 -3
  4. package/bin/xnet.exe +0 -0
  5. package/build-info.json +1 -1
  6. package/package.json +1 -1
  7. package/scripts/codeoutline/cli.lua +2 -2
  8. package/scripts/codeoutline/command.lua +9 -1
  9. package/scripts/codeoutline/common.lua +45 -5
  10. package/scripts/codeoutline/completion.lua +936 -0
  11. package/scripts/codeoutline/control.lua +5 -1
  12. package/scripts/codeoutline/documents.lua +154 -0
  13. package/scripts/codeoutline/explore.lua +35 -8
  14. package/scripts/codeoutline/graph.lua +321 -102
  15. package/scripts/codeoutline/index.lua +170 -50
  16. package/scripts/codeoutline/index_worker.lua +108 -17
  17. package/scripts/codeoutline/indirect.lua +201 -0
  18. package/scripts/codeoutline/js_evidence.lua +175 -0
  19. package/scripts/codeoutline/js_resolution.lua +201 -0
  20. package/scripts/codeoutline/lang/c.lua +22 -12
  21. package/scripts/codeoutline/lang/cpp.lua +13 -12
  22. package/scripts/codeoutline/lang/csharp.lua +21 -21
  23. package/scripts/codeoutline/lang/go.lua +10 -10
  24. package/scripts/codeoutline/lang/java.lua +9 -9
  25. package/scripts/codeoutline/lang/js.lua +15 -14
  26. package/scripts/codeoutline/lang/kotlin.lua +384 -0
  27. package/scripts/codeoutline/lang/lua.lua +11 -6
  28. package/scripts/codeoutline/lang/python.lua +6 -6
  29. package/scripts/codeoutline/lang/rust.lua +23 -15
  30. package/scripts/codeoutline/lsp.lua +574 -0
  31. package/scripts/codeoutline/lua_bindings.lua +222 -0
  32. package/scripts/codeoutline/main.lua +28 -5
  33. package/scripts/codeoutline/navigation.lua +1046 -0
  34. package/scripts/codeoutline/parse.lua +9 -2
  35. package/scripts/codeoutline/parse_pool.lua +189 -0
  36. package/scripts/codeoutline/parse_worker.lua +24 -0
  37. package/scripts/codeoutline/service.lua +82 -31
  38. package/scripts/codeoutline/update_sequence.lua +1 -1
  39. package/scripts/codeoutline/version.lua +1 -1
  40. package/licenses/rpmalloc.txt +0 -12
@@ -0,0 +1,201 @@
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)
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
+ for _, rule in ipairs(rules) do names[rule.call] = true; if rule.register then names[rule.register] = true end end
91
+ local function candidate(rec, call)
92
+ if rec.dispatch_sites then return true end -- explicit in-memory parse option
93
+ for _, name in ipairs(rec.dispatch_calls or {}) do if name == call then return true end end
94
+ return false
95
+ end
96
+ local function sites(f)
97
+ local rec = G.files[f]
98
+ if rec.dispatch_sites then return rec.dispatch_sites end
99
+ if cache[f] then return cache[f].sites end
100
+ assert(G.root, 'indirect resolution requires a project root or explicit dispatch parse data')
101
+ local file = assert(io.open(G.root .. '/' .. rec.path, 'rb'), 'cannot read indirect source: ' .. rec.path)
102
+ local raw = file:read('a'); file:close()
103
+ local crc = xcompress and xcompress.crc32 and xcompress.crc32(raw) or xutils.sha256_hex(raw)
104
+ assert(#raw == rec.size and crc == rec.crc, 'source changed during query; retry: ' .. rec.path)
105
+ local source = raw
106
+ if rec.encoding ~= 'utf-8' and rec.encoding ~= 'utf-8-bom' then
107
+ source = assert(xutils.to_utf8(raw, rec.encoding == 'gbk' and 'gbk' or 'auto'))
108
+ end
109
+ local parsed = assert(require('codeoutline.parse').parse(rec.path, source, { ranges = false, dispatch = names }))
110
+ assert(#parsed.nodes == #rec.nodes, 'source encoding changed; refresh index before indirect query: ' .. rec.path)
111
+ for i, n in ipairs(parsed.nodes) do
112
+ assert(n.qualified == rec.nodes[i].qualified and n.line == rec.nodes[i].line,
113
+ 'source encoding changed; refresh index before indirect query: ' .. rec.path)
114
+ end
115
+ local result = parsed.dispatch_sites or {}
116
+ local weight = #source + #result * 512
117
+ -- Query-local FIFO: no retained source/tokens and no cross-query invalidation.
118
+ while #order > 0 and (#order >= 16 or bytes + weight > 8 * 1024 * 1024) do
119
+ local old = table.remove(order, 1); bytes = bytes - cache[old].weight; cache[old] = nil
120
+ end
121
+ if weight <= 8 * 1024 * 1024 then
122
+ cache[f] = { sites = result, weight = weight }; order[#order + 1] = f; bytes = bytes + weight
123
+ end
124
+ return result
125
+ end
126
+ local function targets(f, site, arg, literal)
127
+ local name
128
+ if arg then
129
+ if literal then name = arg.literal else name = arg.symbol end
130
+ end
131
+ if not name or not name:match('^[%a_][%w_%.:>%-]*$') then return {} end
132
+ local short = name:match('([%w_]+)$')
133
+ local deps, paired = graph.dependencies(G, f)
134
+ local view = G
135
+ if short ~= name then
136
+ local filtered = {}
137
+ for _, id in ipairs(G.by_name[short] or {}) do
138
+ if G.nodes[id].qualified == name then filtered[#filtered + 1] = id end
139
+ end
140
+ view = setmetatable({ by_name = { [short] = filtered } }, { __index = G })
141
+ end
142
+ return graph.resolve(view, f, G.id_of[f][site.from], short, 'ref', nil, deps, paired) or {}
143
+ end
144
+ local function registrations(ri, rule)
145
+ if registrations_by_rule[ri] then return registrations_by_rule[ri] end
146
+ local registrations = {}
147
+ if rule.kind == 'event' then
148
+ for _, f in ipairs(G.file_order) do
149
+ control.check()
150
+ local rec = G.files[f]
151
+ if (not rule.language or rule.language == rec.language) and candidate(rec, rule.register) then
152
+ for _, site in ipairs(sites(f)) do
153
+ local key = site.args[rule.keyArgument]
154
+ if site.call == rule.register and key and key.literal then
155
+ local bucket = rec.language .. '\0' .. key.literal
156
+ registrations[bucket] = registrations[bucket] or {}
157
+ for _, id in ipairs(targets(f, site, site.args[rule.handlerArgument])) do
158
+ registrations[bucket][id] = true
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
165
+ registrations_by_rule[ri] = registrations
166
+ return registrations
167
+ end
168
+ local function scan(only, wanted)
169
+ local out = {}
170
+ for ri, rule in ipairs(rules) do
171
+ local files = only and { G.node_file[only] } or G.file_order
172
+ for _, f in ipairs(files) do
173
+ control.check()
174
+ local rec = G.files[f]
175
+ if (not rule.language or rule.language == rec.language) and candidate(rec, rule.call) then
176
+ for _, site in ipairs(sites(f)) do
177
+ if site.call == rule.call and site.from > 0 and (not only or G.id_of[f][site.from] == only) then
178
+ local ids, arg = {}, site.args[rule.argument]
179
+ if rule.kind == 'event' then
180
+ local registered = registrations(ri, rule)
181
+ for id in pairs(arg and arg.literal and registered[rec.language .. '\0' .. arg.literal] or {}) do ids[#ids + 1] = id end
182
+ table.sort(ids, function(a, b) return graph.before(G, a, b) end)
183
+ else ids = targets(f, site, arg, rule.kind == 'target') end
184
+ local src = G.id_of[f][site.from]
185
+ for _, id in ipairs(ids) do
186
+ if not wanted or wanted[id] then
187
+ out[src] = out[src] or {}
188
+ out[src][#out[src] + 1] = { target = id, line = site.line, kind = 'indirect:' .. rule.kind .. '#' .. ri }
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end
195
+ end
196
+ return out
197
+ end
198
+ return { outgoing = function(id) return scan(id)[id] or {} end, incoming = function(wanted) return scan(nil, wanted) end }
199
+ end
200
+
201
+ 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
@@ -0,0 +1,201 @@
1
+ -- JS/TS module and receiver evidence; deliberately not a type checker.
2
+ local M = {}
3
+ local control = require('codeoutline.control')
4
+ local function js(rec) return rec and (rec.language == 'javascript' or rec.language == 'typescript') end
5
+ local function dir(p) return p:match('^(.*)/[^/]*$') or '' end
6
+ local function norm(p)
7
+ local parts = {}
8
+ for s in p:gsub('\\', '/'):gmatch('[^/]+') do
9
+ if s == '..' and #parts > 0 and parts[#parts] ~= '..' then parts[#parts] = nil
10
+ elseif s ~= '.' then parts[#parts + 1] = s end
11
+ end
12
+ return (p:sub(1, 1) == '/' and '/' or '') .. table.concat(parts, '/')
13
+ end
14
+
15
+ -- JSONC is tokenized, never executed. Token-aware comma removal preserves strings.
16
+ local function config(path, seen, depth)
17
+ path = norm(path)
18
+ if seen[path] or depth > 8 then return {} end
19
+ seen[path] = true
20
+ local f = io.open(path, 'rb')
21
+ if not f then return {} end
22
+ local raw = f:read(1048577); f:close()
23
+ assert(#raw <= 1048576, 'JS project configuration exceeds 1 MiB: ' .. path)
24
+ local T = require('codeoutline.lang.js').lang:tokenize(raw)
25
+ local parts = {}
26
+ for i = 1, T.n do
27
+ local s, next = T:text(i), T:text(i + 1)
28
+ if not (s == ',' and (next == '}' or next == ']')) then parts[#parts + 1] = s end
29
+ end
30
+ local ok, data = pcall(xutils.json_unpack, table.concat(parts, ' '))
31
+ assert(ok and type(data) == 'table', 'invalid JS project configuration: ' .. path)
32
+ local base = dir(path)
33
+ local effective = {}
34
+ if type(data.extends) == 'string' and data.extends:sub(1, 1) == '.' then
35
+ local parent = norm(base .. '/' .. data.extends)
36
+ if not parent:match('%.json$') then parent = parent .. '.json' end
37
+ effective = config(parent, seen, depth + 1)
38
+ end
39
+ local co = data.compilerOptions or {}
40
+ if type(co.baseUrl) == 'string' then effective.base = norm(base .. '/' .. co.baseUrl) end
41
+ if type(co.paths) == 'table' then effective.paths, effective.origin = co.paths, base end
42
+ return effective
43
+ end
44
+
45
+ function M.context(G)
46
+ local ctx = { modules = {}, exports = {} }
47
+ if G.root then
48
+ for _, name in ipairs({ 'tsconfig.json', 'jsconfig.json', 'tsconfig.base.json' }) do
49
+ local opts = config(G.root .. '/' .. name, {}, 0)
50
+ if opts.paths then
51
+ ctx.base, ctx.patterns = opts.base or opts.origin, {}
52
+ for pattern, targets in pairs(opts.paths) do
53
+ if type(pattern) == 'string' and type(targets) == 'table' then
54
+ local pre, post = pattern:match('^(.-)%*(.-)$')
55
+ ctx.patterns[#ctx.patterns + 1] = { pattern = pattern, pre = pre, post = post, targets = targets }
56
+ end
57
+ end
58
+ table.sort(ctx.patterns, function(a, b)
59
+ local ap, bp = a.pre or a.pattern, b.pre or b.pattern
60
+ if #ap ~= #bp then return #ap > #bp end
61
+ if (a.pre == nil) ~= (b.pre == nil) then return a.pre == nil end
62
+ return a.pattern < b.pattern
63
+ end)
64
+ break
65
+ end
66
+ end
67
+ end
68
+ return ctx
69
+ end
70
+
71
+ local extensions = { '', '.ts', '.tsx', '.js', '.jsx', '.mts', '.cts', '.mjs', '.cjs',
72
+ '/index.ts', '/index.tsx', '/index.js', '/index.jsx', '/index.mts', '/index.mjs' }
73
+ function M.module(G, f, spec, ctx)
74
+ ctx = ctx or M.context(G)
75
+ local key = f .. '\0' .. spec
76
+ if ctx.modules[key] ~= nil then return ctx.modules[key] or nil end
77
+ local bases = {}
78
+ if spec:sub(1, 1) == '.' then bases[1] = norm(dir(G.files[f].path) .. '/' .. spec):gsub('^/', '')
79
+ else
80
+ for _, p in ipairs(ctx.patterns or {}) do
81
+ local capture
82
+ if not p.pre and spec == p.pattern then capture = ''
83
+ elseif p.pre and spec:sub(1, #p.pre) == p.pre and (#p.post == 0 or spec:sub(-#p.post) == p.post)
84
+ and #spec >= #p.pre + #p.post then capture = spec:sub(#p.pre + 1, #spec - #p.post) end
85
+ if capture then
86
+ for _, target in ipairs(p.targets) do
87
+ if type(target) == 'string' then
88
+ local absolute = norm(ctx.base .. '/' .. target:gsub('%*', function() return capture end))
89
+ local root = norm(G.root)
90
+ if absolute:sub(1, #root + 1) == root .. '/' then bases[#bases + 1] = absolute:sub(#root + 2) end
91
+ end
92
+ end
93
+ break
94
+ end
95
+ end
96
+ end
97
+ for _, base in ipairs(bases) do
98
+ local candidates = { base }
99
+ if base:match('%.js$') then candidates[#candidates + 1] = base:gsub('%.js$', '.ts'); candidates[#candidates + 1] = base:gsub('%.js$', '.tsx') end
100
+ for _, ext in ipairs(extensions) do candidates[#candidates + 1] = base .. ext end
101
+ for _, candidate in ipairs(candidates) do
102
+ local target = G.file_index[candidate]
103
+ if target and js(G.files[target]) then ctx.modules[key] = target; return target end
104
+ end
105
+ end
106
+ ctx.modules[key] = false
107
+ end
108
+
109
+ local function exported(G, f, name, ctx, seen, depth)
110
+ control.check()
111
+ if depth > 8 then return {} end
112
+ local key = f .. '\0' .. name
113
+ if depth == 0 and ctx.exports[key] then return ctx.exports[key] end
114
+ if seen[key] then return {} end
115
+ seen[key] = true
116
+ local result, unique = {}, {}
117
+ local function add(ids) for _, id in ipairs(ids) do if not unique[id] then unique[id] = true; result[#result + 1] = id end end end
118
+ local rec = G.files[f]
119
+ local entries = rec.js_exports or {}
120
+ local explicit = false
121
+ for _, e in ipairs(entries) do if e.name == name then explicit = true end end
122
+ for _, e in ipairs(entries) do
123
+ if e.name == name or (not explicit and e.name == '*' and name ~= 'default') then
124
+ local want = e.local_name == '*' and name or e.local_name
125
+ if e.path then
126
+ local target = M.module(G, f, e.path, ctx)
127
+ if target then add(exported(G, target, want, ctx, seen, depth + 1)) end
128
+ else
129
+ for _, id in ipairs(G.id_of[f]) do
130
+ local n = G.nodes[id]
131
+ if not n.parent and n.name == want then add({ id }) end
132
+ end
133
+ for _, b in ipairs(rec.js_imports or {}) do
134
+ if b.local_name == want then
135
+ local target = M.module(G, f, b.path, ctx)
136
+ if target then add(exported(G, target, b.imported, ctx, seen, depth + 1)) end
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
142
+ if depth == 0 then ctx.exports[key] = result end
143
+ return result
144
+ end
145
+
146
+ function M.resolve(G, f, name, kind, recv, evidence, ctx)
147
+ local rec = G.files[f]
148
+ if not js(rec) then return nil end
149
+ ctx = ctx or M.context(G)
150
+ local function binding(local_name, member)
151
+ for _, b in ipairs(rec.js_imports or {}) do
152
+ if b.local_name == local_name then
153
+ local target = M.module(G, f, b.path, ctx)
154
+ if not target then return {} end
155
+ if b.imported == '*' then return member and exported(G, target, member, ctx, {}, 0) or {} end
156
+ return exported(G, target, b.imported, ctx, {}, 0)
157
+ end
158
+ end
159
+ end
160
+ if evidence == false then return {} end
161
+ if evidence then
162
+ local node = evidence:match('^node:(%d+)$')
163
+ if node then return { G.id_of[f][tonumber(node)] } end
164
+ local head, tail = evidence:match('^([%w_$]+)%.([%w_$]+)$')
165
+ local classes = binding(head or evidence, tail)
166
+ if classes == nil then
167
+ classes = {}
168
+ for _, id in ipairs(G.id_of[f]) do
169
+ local n = G.nodes[id]
170
+ if not n.parent and n.name == evidence and (n.kind == 'class' or n.kind == 'interface') then classes[#classes + 1] = id end
171
+ end
172
+ end
173
+ local result = {}
174
+ for _, cls in ipairs(classes) do
175
+ if G.nodes[cls].kind == 'class' or G.nodes[cls].kind == 'interface' then
176
+ for _, id in ipairs(G.by_name[name] or {}) do
177
+ local n = G.nodes[id]
178
+ if n.parent and G.id_of[G.node_file[id]][n.parent] == cls then result[#result + 1] = id end
179
+ end
180
+ end
181
+ end
182
+ return result
183
+ end
184
+ if recv then
185
+ for _, b in ipairs(rec.js_imports or {}) do
186
+ if b.local_name == recv then
187
+ if b.imported == '*' then return binding(recv, name) end
188
+ local result = {}
189
+ for _, cls in ipairs(binding(recv) or {}) do
190
+ for _, id in ipairs(G.by_name[name] or {}) do
191
+ local n = G.nodes[id]
192
+ if n.parent and G.id_of[G.node_file[id]][n.parent] == cls then result[#result + 1] = id end
193
+ end
194
+ end
195
+ return result
196
+ end
197
+ end
198
+ else return binding(name) end
199
+ end
200
+
201
+ return M