veoflow 0.5.7 → 0.7.0

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/bin/veoflow.mjs CHANGED
@@ -17,7 +17,7 @@ const here = dirname(fileURLToPath(import.meta.url));
17
17
  process.env.VEO_CHECKERS_MODULE ??= pathToFileURL(join(here, "../dist/checkers.mjs")).href;
18
18
  process.env.VEO_NATIVE_DIR ??= join(here, "../native");
19
19
  process.env.VEO_WEB_ROOT ??= join(here, "../web");
20
- process.env.VEO_VERSION = "0.5.7";
20
+ process.env.VEO_VERSION = "0.7.0";
21
21
  const { main } = await import("../dist/veo.mjs");
22
22
  const code = await main(process.argv.slice(2), {
23
23
  out: (line) => process.stdout.write(line + "\n"),
package/dist/checkers.mjs CHANGED
@@ -109,6 +109,83 @@ function readIndex(path) {
109
109
  occurrences: d.occurrences.map((o) => ({ range: [...o.range], symbol: o.symbol, roles: o.symbol_roles }))
110
110
  }));
111
111
  }
112
+ function decodeIndex(bytes) {
113
+ const fields = (start, end) => {
114
+ const out = [];
115
+ let i = start;
116
+ const varint = () => {
117
+ let x = 0;
118
+ let mul = 1;
119
+ for (; ; ) {
120
+ const c = bytes[i++];
121
+ x += (c & 127) * mul;
122
+ mul *= 128;
123
+ if (!(c & 128)) return x;
124
+ }
125
+ };
126
+ while (i < end) {
127
+ const key = varint();
128
+ const f = Math.floor(key / 8);
129
+ const w = key & 7;
130
+ if (w === 0) out.push({ f, w, v: varint(), s: 0, e: 0 });
131
+ else if (w === 2) {
132
+ const n = varint();
133
+ out.push({ f, w, v: 0, s: i, e: i + n });
134
+ i += n;
135
+ } else if (w === 5) i += 4;
136
+ else if (w === 1) i += 8;
137
+ else throw new Error(`unsupported protobuf wire type ${w}`);
138
+ }
139
+ return out;
140
+ };
141
+ const packed = (s, e) => {
142
+ const out = [];
143
+ let i = s;
144
+ while (i < e) {
145
+ let x = 0;
146
+ let mul = 1;
147
+ for (; ; ) {
148
+ const c = bytes[i++];
149
+ x += (c & 127) * mul;
150
+ mul *= 128;
151
+ if (!(c & 128)) break;
152
+ }
153
+ out.push(x);
154
+ }
155
+ return out;
156
+ };
157
+ const text = (s, e) => Buffer.from(bytes.subarray(s, e)).toString("utf8");
158
+ const encodings = ["unspecified", "utf8", "utf16", "utf32"];
159
+ const docs = [];
160
+ for (const d of fields(0, bytes.length)) {
161
+ if (d.f !== 2 || d.w !== 2) continue;
162
+ const doc = {
163
+ path: "",
164
+ occurrences: [],
165
+ encoding: "unspecified"
166
+ };
167
+ for (const x of fields(d.s, d.e)) {
168
+ if (x.f === 1 && x.w === 2) doc.path = text(x.s, x.e);
169
+ else if (x.f === 6 && x.w === 0) doc.encoding = encodings[x.v] ?? "unspecified";
170
+ else if (x.f === 2 && x.w === 2) {
171
+ const o = { range: [], symbol: "", roles: 0 };
172
+ for (const y of fields(x.s, x.e)) {
173
+ if (y.f === 1) o.range.push(...y.w === 2 ? packed(y.s, y.e) : [y.v]);
174
+ else if (y.f === 2 && y.w === 2) o.symbol = text(y.s, y.e);
175
+ else if (y.f === 3 && y.w === 0) o.roles = y.v;
176
+ else if ((y.f === 8 || y.f === 9) && y.w === 2) {
177
+ const r = [0, 0, 0, 0];
178
+ for (const z of fields(y.s, y.e)) if (z.w === 0 && z.f >= 1 && z.f <= 4) r[z.f - 1] = z.v;
179
+ o.range = y.f === 8 ? r.slice(0, 3) : r;
180
+ }
181
+ }
182
+ doc.occurrences.push(o);
183
+ }
184
+ }
185
+ docs.push(doc);
186
+ }
187
+ return docs;
188
+ }
112
189
 
113
190
  // packages/checkers/src/translate.ts
114
191
  import ts2 from "typescript";
@@ -359,12 +436,30 @@ function adapterDef(files, call) {
359
436
  if (callee.exact)
360
437
  return defs.find((d) => d.span.start.line === callee.at.line && d.span.start.column === callee.at.column) ?? null;
361
438
  if (callee.kind === "class" || call.form === "new") {
362
- const names = call.form === "jsx" ? [`${callee.name}.render`, `${callee.name}.constructor`] : [`${callee.name}.constructor`];
439
+ const names = call.form === "jsx" ? [`${callee.name}.render`, `${callee.name}.constructor`] : (
440
+ // Python's constructor is `__init__` (#417).
441
+ [`${callee.name}.constructor`, `${callee.name}.__init__`]
442
+ );
363
443
  for (const n of names) {
364
444
  const d = defs.find((x) => x.qualifiedName === n || x.qualifiedName.endsWith(`.${n}`));
365
445
  if (d) return d;
366
446
  }
367
- if (callee.kind === "class") return null;
447
+ if (callee.kind === "class") {
448
+ const types = files.get(callee.file)?.types ?? [];
449
+ const seen = /* @__PURE__ */ new Set();
450
+ const up = (name) => {
451
+ if (seen.has(name)) return null;
452
+ seen.add(name);
453
+ for (const base of types.find((t) => t.name === name)?.bases ?? []) {
454
+ const d = defs.find((x) => x.qualifiedName === `${base.name}.__init__` && !x.implicit);
455
+ if (d) return d;
456
+ const further = up(base.name);
457
+ if (further) return further;
458
+ }
459
+ return null;
460
+ };
461
+ return up(callee.name);
462
+ }
368
463
  }
369
464
  const named = defs.filter((d) => d.name === callee.name);
370
465
  const next = named.filter((d) => before(callee.at, d.span)).sort((a, b) => a.span.start.offset - b.span.start.offset)[0] ?? null;
@@ -571,18 +666,539 @@ function workspacePackages(root, manifests) {
571
666
  }
572
667
  return out;
573
668
  }
669
+
670
+ // packages/checkers/src/python.ts
671
+ import { spawnSync as spawnSync2 } from "node:child_process";
672
+ import { cpSync as cpSync2, existsSync as existsSync3, mkdirSync as mkdirSync2, mkdtempSync as mkdtempSync2, rmSync as rmSync2, writeFileSync as writeFileSync2 } from "node:fs";
673
+ import { createRequire as createRequire2 } from "node:module";
674
+ import { tmpdir as tmpdir2 } from "node:os";
675
+ import { dirname as dirname3, join as join3 } from "node:path";
676
+ var require3 = createRequire2(import.meta.url);
677
+ var CONFIG2 = ["pyproject.toml", "setup.cfg", "pyrightconfig.json"];
678
+ function indexPython(root, files) {
679
+ const main = require3.resolve("@sourcegraph/scip-python/index.js");
680
+ const work = mkdtempSync2(join3(tmpdir2(), "veo-scip-py-"));
681
+ try {
682
+ const copy = join3(work, "src");
683
+ for (const f of [...files, ...CONFIG2.filter((c) => existsSync3(join3(root, c)))]) {
684
+ mkdirSync2(dirname3(join3(copy, f)), { recursive: true });
685
+ cpSync2(join3(root, f), join3(copy, f));
686
+ }
687
+ writeFileSync2(join3(work, "env.json"), "[]");
688
+ const out = join3(work, "index.scip");
689
+ const r = spawnSync2(
690
+ process.execPath,
691
+ [
692
+ main,
693
+ "index",
694
+ "--project-name",
695
+ "veoflow-check",
696
+ "--project-version",
697
+ "0",
698
+ "--environment",
699
+ join3(work, "env.json"),
700
+ "--output",
701
+ out,
702
+ "src"
703
+ ],
704
+ {
705
+ cwd: work,
706
+ encoding: "utf8",
707
+ maxBuffer: 1 << 26,
708
+ timeout: 30 * 60 * 1e3,
709
+ env: { PATH: dirname3(process.execPath), PYTHONSAFEPATH: "1", HOME: work }
710
+ }
711
+ );
712
+ if (r.status !== 0 || !existsSync3(out)) {
713
+ const lines = `${r.stderr ?? ""}
714
+ ${r.stdout ?? ""}`.split("\n").map((l) => l.trim()).filter(Boolean);
715
+ const error = lines.find((l) => /error|failure/i.test(l) && !l.startsWith("at ")) ?? lines.at(-1) ?? "";
716
+ throw new Error(`scip-python failed${r.error ? ` (${r.error.message})` : ""}: ${error}`);
717
+ }
718
+ return readIndex(out).map((d) => ({ ...d, path: d.path.replace(/^src\//, "") }));
719
+ } finally {
720
+ rmSync2(work, { recursive: true, force: true });
721
+ }
722
+ }
723
+ var Lines = class {
724
+ constructor(text) {
725
+ this.text = text;
726
+ for (let i = 0; i < text.length; i++) if (text.charCodeAt(i) === 10) this.starts.push(i + 1);
727
+ }
728
+ text;
729
+ starts = [0];
730
+ /** A position from a 0-based line and UTF-16 column, as the adapter reports it (columns in characters). */
731
+ position(line0, char0) {
732
+ const start = this.starts[line0] ?? this.text.length;
733
+ return { line: line0 + 1, column: [...this.text.slice(start, start + char0)].length + 1 };
734
+ }
735
+ /** The 0-based line and UTF-16 column of a UTF-16 index. */
736
+ at(index) {
737
+ let lo = 0;
738
+ let hi = this.starts.length - 1;
739
+ while (lo < hi) {
740
+ const mid = lo + hi + 1 >> 1;
741
+ if (this.starts[mid] <= index) lo = mid;
742
+ else hi = mid - 1;
743
+ }
744
+ return { line: lo, char: index - this.starts[lo] };
745
+ }
746
+ };
747
+ var NAME = /([\p{L}_][\p{L}\p{N}_]*)\s*$/u;
748
+ function calleeName2(siteText, expression) {
749
+ let callee;
750
+ if (siteText.startsWith("@")) callee = siteText.slice(1);
751
+ else if (expression !== null && siteText.startsWith(expression)) callee = expression;
752
+ else {
753
+ let depth = 0;
754
+ let open = -1;
755
+ for (let i = 0; i < siteText.length; i++) {
756
+ const c = siteText[i];
757
+ if (c === "#") {
758
+ const nl = siteText.indexOf("\n", i);
759
+ i = nl < 0 ? siteText.length : nl;
760
+ } else if (c === "'" || c === '"') {
761
+ const triple = siteText.startsWith(c.repeat(3), i);
762
+ const q = triple ? c.repeat(3) : c;
763
+ let j = i + q.length;
764
+ while (j < siteText.length && !siteText.startsWith(q, j)) j += siteText[j] === "\\" ? 2 : 1;
765
+ i = j + q.length - 1;
766
+ } else if (c === "(" || c === "[" || c === "{") {
767
+ if (c === "(" && depth === 0) open = i;
768
+ depth++;
769
+ } else if (c === ")" || c === "]" || c === "}") depth--;
770
+ }
771
+ if (open < 0) return null;
772
+ callee = siteText.slice(0, open);
773
+ }
774
+ const m = NAME.exec(callee);
775
+ if (!m) return null;
776
+ const at = siteText.startsWith("@") ? 1 + m.index : m.index;
777
+ return { offset: at, name: m[1] };
778
+ }
779
+ function lastDescriptor(symbol) {
780
+ const m = /(?:`([^`]+)`|([\p{L}_][\p{L}\p{N}_]*))(#|\(\)\.)$/u.exec(symbol);
781
+ if (!m) return null;
782
+ return { name: m[1] ?? m[2], kind: m[3] === "#" ? "class" : "function" };
783
+ }
784
+ function translatePython(docs, adapter, texts) {
785
+ const byPath = new Map(docs.map((d) => [d.path, d]));
786
+ const lines = /* @__PURE__ */ new Map();
787
+ const linesOf = (path) => {
788
+ let l = lines.get(path);
789
+ if (!l) lines.set(path, l = new Lines(texts.get(path) ?? ""));
790
+ return l;
791
+ };
792
+ const definitions = /* @__PURE__ */ new Map();
793
+ const keyOf = (path, symbol) => symbol.startsWith("local ") ? `${path}\0${symbol}` : symbol;
794
+ for (const d of docs) {
795
+ if (!texts.has(d.path)) continue;
796
+ for (const o of d.occurrences) {
797
+ const key = keyOf(d.path, o.symbol);
798
+ if (o.roles & DEFINITION && !definitions.has(key))
799
+ definitions.set(key, {
800
+ file: d.path,
801
+ at: linesOf(d.path).position(o.range[0], o.range[1]),
802
+ line0: o.range[0],
803
+ char0: o.range[1]
804
+ });
805
+ }
806
+ }
807
+ const out = [];
808
+ for (const f of adapter) {
809
+ if (f.language !== "python") continue;
810
+ const text = texts.get(f.path);
811
+ const doc = byPath.get(f.path);
812
+ if (text === void 0 || !doc) continue;
813
+ const bytes = Buffer.from(text, "utf8");
814
+ const fileLines = linesOf(f.path);
815
+ let starts = null;
816
+ const indexOf = (offset) => {
817
+ if (bytes.length === text.length) return offset;
818
+ if (!starts) {
819
+ starts = [];
820
+ let b = 0;
821
+ for (let i = 0; i < text.length; i++) {
822
+ starts.push(b);
823
+ const c = text.charCodeAt(i);
824
+ b += c < 128 ? 1 : c < 2048 ? 2 : c >= 55296 && c <= 56319 ? 4 : c >= 56320 && c <= 57343 ? 0 : 3;
825
+ }
826
+ starts.push(b);
827
+ }
828
+ let lo = 0;
829
+ let hi = starts.length - 1;
830
+ while (lo < hi) {
831
+ const mid = lo + hi >> 1;
832
+ if (starts[mid] < offset) lo = mid + 1;
833
+ else hi = mid;
834
+ }
835
+ return lo;
836
+ };
837
+ const at = /* @__PURE__ */ new Map();
838
+ for (const o of doc.occurrences)
839
+ if (!(o.roles & DEFINITION)) at.set(`${o.range[0]}:${o.range[1]}`, o.symbol);
840
+ const sites = [
841
+ ...f.calls.map((c) => ({
842
+ site: c.site,
843
+ expression: null,
844
+ form: c.form === "new" ? "new" : c.form === "super" ? "super" : c.form === "decorator" ? "decorator" : "call"
845
+ })),
846
+ ...f.unresolved.map((u) => ({ site: u.site, expression: u.expression, form: "call" }))
847
+ ];
848
+ for (const s of sites) {
849
+ const siteText = bytes.subarray(s.site.start.offset, s.site.end.offset).toString("utf8");
850
+ const name = calleeName2(siteText, s.expression);
851
+ if (!name) continue;
852
+ const before2 = indexOf(s.site.start.offset);
853
+ const { line, char } = fileLines.at(before2 + name.offset);
854
+ const symbol = at.get(`${line}:${char}`);
855
+ if (symbol === void 0) continue;
856
+ const def = definitions.get(keyOf(f.path, symbol));
857
+ const descriptor = lastDescriptor(symbol);
858
+ const lambda = symbol.startsWith("local ") && def !== void 0 && /^[\p{L}_][\p{L}\p{N}_]*\s*=\s*lambda\b/u.test(
859
+ (texts.get(def.file) ?? "").slice(linesOf(def.file).starts[def.line0] + def.char0)
860
+ );
861
+ out.push({
862
+ file: f.path,
863
+ site: s.site,
864
+ form: s.form,
865
+ callee: def && (descriptor || lambda) ? {
866
+ file: def.file,
867
+ name: descriptor?.name ?? name.name,
868
+ at: def.at,
869
+ kind: descriptor?.kind ?? "function"
870
+ } : null,
871
+ symbol
872
+ });
873
+ }
874
+ }
875
+ return out;
876
+ }
877
+
878
+ // packages/checkers/src/go.ts
879
+ import { spawnSync as spawnSync3 } from "node:child_process";
880
+ import { cpSync as cpSync3, existsSync as existsSync4, mkdirSync as mkdirSync3, mkdtempSync as mkdtempSync3, readFileSync as readFileSync3, rmSync as rmSync3, writeFileSync as writeFileSync3 } from "node:fs";
881
+ import { homedir, tmpdir as tmpdir3 } from "node:os";
882
+ import { delimiter, dirname as dirname4, join as join4, posix } from "node:path";
883
+ var exe = (name) => process.platform === "win32" ? `${name}.exe` : name;
884
+ function which(name, extra = []) {
885
+ for (const dir of [...(process.env.PATH ?? "").split(delimiter), ...extra]) {
886
+ if (!dir) continue;
887
+ const p = join4(dir, exe(name));
888
+ if (existsSync4(p)) return p;
889
+ }
890
+ return null;
891
+ }
892
+ function goTools() {
893
+ const go = which("go");
894
+ if (!go) return { missing: "the Go toolchain (go) is not on the PATH: install Go from https://go.dev/dl/" };
895
+ const scipGo = (process.env.VEOFLOW_SCIP_GO && existsSync4(process.env.VEOFLOW_SCIP_GO) ? process.env.VEOFLOW_SCIP_GO : null) ?? which("scip-go", [join4(process.env.GOPATH ?? join4(homedir(), "go"), "bin")]);
896
+ if (!scipGo)
897
+ return {
898
+ missing: "scip-go is not installed: run `go install github.com/scip-code/scip-go/cmd/scip-go@latest` (or set VEOFLOW_SCIP_GO to it)"
899
+ };
900
+ const r = spawnSync3(go, ["env", "GOROOT"], { encoding: "utf8", timeout: 6e4 });
901
+ const goroot = r.status === 0 ? r.stdout.trim() : "";
902
+ if (!goroot || !existsSync4(join4(goroot, "bin", exe("go"))))
903
+ return { missing: `couldn't find Go's installation (go env GOROOT said "${goroot}")` };
904
+ return { go, scipGo, goroot };
905
+ }
906
+ function moduleFiles(root, files) {
907
+ const out = /* @__PURE__ */ new Set();
908
+ for (const f of files) {
909
+ let dir = posix.dirname(f);
910
+ for (; ; ) {
911
+ for (const name of ["go.mod", "go.sum", "go.work", "go.work.sum"]) {
912
+ const p = dir === "." ? name : `${dir}/${name}`;
913
+ if (existsSync4(join4(root, p))) out.add(p);
914
+ }
915
+ if (dir === "." || dir === "") break;
916
+ dir = posix.dirname(dir);
917
+ }
918
+ }
919
+ return [...out].sort();
920
+ }
921
+ function indexGo(root, files) {
922
+ const tools = goTools();
923
+ if ("missing" in tools) throw new Error(tools.missing);
924
+ const work = mkdtempSync3(join4(tmpdir3(), "veo-scip-go-"));
925
+ try {
926
+ const copy = join4(work, "src");
927
+ const manifests = moduleFiles(root, files);
928
+ for (const f of [...files, ...manifests]) {
929
+ mkdirSync3(dirname4(join4(copy, f)), { recursive: true });
930
+ cpSync3(join4(root, f), join4(copy, f));
931
+ }
932
+ const mods = manifests.filter((m) => posix.basename(m) === "go.mod").map((m) => posix.dirname(m) === "." ? "" : posix.dirname(m));
933
+ const nearest = (f) => mods.filter((m) => m === "" || f.startsWith(`${m}/`)).sort((a, b) => b.length - a.length)[0];
934
+ const used = [...new Set(files.map(nearest).filter((m) => m !== void 0))].sort();
935
+ if (!used.length) throw new Error("no go.mod above the files: scip-go indexes Go modules");
936
+ let workspace = false;
937
+ for (const w of manifests.filter((m) => posix.basename(m) === "go.work")) {
938
+ const dir = posix.dirname(w) === "." ? "" : posix.dirname(w);
939
+ const inside = used.filter((m) => dir === "" || m === dir || m.startsWith(`${dir}/`));
940
+ const goLine = /^\s*go\s+(\S+)/m.exec(readFileSync3(join4(copy, w), "utf8"))?.[1];
941
+ const rel = (m) => `./${posix.relative(dir || ".", m || ".") || "."}`;
942
+ writeFileSync3(
943
+ join4(copy, w),
944
+ `${goLine ? `go ${goLine}
945
+
946
+ ` : ""}use (
947
+ ${inside.map((m) => ` ${rel(m)}
948
+ `).join("")})
949
+ `
950
+ );
951
+ rmSync3(join4(copy, `${w}.sum`), { force: true });
952
+ if (inside.length) workspace = true;
953
+ }
954
+ const env = {
955
+ // The toolchain's own folder (a version manager's `go` is a shim needing more than a PATH).
956
+ PATH: [join4(tools.goroot, "bin"), dirname4(tools.scipGo)].join(delimiter),
957
+ GOROOT: tools.goroot,
958
+ HOME: work,
959
+ GOPATH: join4(work, "gopath"),
960
+ GOMODCACHE: join4(work, "gomod"),
961
+ GOCACHE: join4(work, "gocache"),
962
+ GOENV: "off",
963
+ GOPROXY: "off",
964
+ GOSUMDB: "off",
965
+ GOTOOLCHAIN: "local",
966
+ // Vendored code isn't copied with its modules.txt: read modules, not vendor/ (not allowed for a workspace).
967
+ GOFLAGS: workspace ? "-buildvcs=false" : "-buildvcs=false -mod=mod",
968
+ GO111MODULE: "on",
969
+ CGO_ENABLED: "0",
970
+ GOOS: "linux",
971
+ GOARCH: "amd64",
972
+ ...process.platform === "win32" ? { SYSTEMROOT: process.env.SYSTEMROOT ?? "C:\\Windows", TEMP: work, TMP: work } : { TMPDIR: work }
973
+ };
974
+ const docs = [];
975
+ const wanted = new Set(files);
976
+ used.forEach((mod, i) => {
977
+ const out = join4(work, `index-${i}.scip`);
978
+ const r = spawnSync3(
979
+ tools.scipGo,
980
+ [
981
+ "index",
982
+ "--module-version",
983
+ "0",
984
+ "--repository-remote",
985
+ "veoflow-check",
986
+ "--go-version",
987
+ "go1.22",
988
+ "--skip-implementations",
989
+ "--quiet",
990
+ "--output",
991
+ out,
992
+ "./..."
993
+ ],
994
+ { cwd: join4(copy, mod), env, encoding: "utf8", maxBuffer: 1 << 26, timeout: 30 * 60 * 1e3 }
995
+ );
996
+ if (r.status !== 0 || !existsSync4(out)) {
997
+ const lines = `${r.stderr ?? ""}
998
+ ${r.stdout ?? ""}`.split("\n").map((l) => l.trim()).filter(Boolean);
999
+ throw new Error(
1000
+ `scip-go failed on ${mod || "the root module"}${r.error ? ` (${r.error.message})` : ""}: ${lines.at(-1) ?? ""}`
1001
+ );
1002
+ }
1003
+ for (const d of decodeIndex(readFileSync3(out))) {
1004
+ const path = mod ? posix.join(mod, d.path) : posix.normalize(d.path);
1005
+ if (wanted.has(path)) docs.push({ ...d, path });
1006
+ }
1007
+ });
1008
+ return docs;
1009
+ } finally {
1010
+ rmSync3(work, { recursive: true, force: true });
1011
+ }
1012
+ }
1013
+ var ByteLines = class {
1014
+ starts = [0];
1015
+ bytes;
1016
+ constructor(text) {
1017
+ this.bytes = Buffer.from(text, "utf8");
1018
+ for (let i = 0; i < this.bytes.length; i++) if (this.bytes[i] === 10) this.starts.push(i + 1);
1019
+ }
1020
+ /** A UTF-8 byte offset's 0-based line and byte column. */
1021
+ at(offset) {
1022
+ let lo = 0;
1023
+ let hi = this.starts.length - 1;
1024
+ while (lo < hi) {
1025
+ const mid = lo + hi + 1 >> 1;
1026
+ if (this.starts[mid] <= offset) lo = mid;
1027
+ else hi = mid - 1;
1028
+ }
1029
+ return { line: lo, col: offset - this.starts[lo] };
1030
+ }
1031
+ /** A line's byte column as a UTF-16 column. */
1032
+ utf16(line, col) {
1033
+ const start = this.starts[line] ?? this.bytes.length;
1034
+ return this.bytes.subarray(start, start + col).toString("utf8").length;
1035
+ }
1036
+ };
1037
+ var NAME2 = /([\p{L}_][\p{L}\p{N}_]*)$/u;
1038
+ function goCalleeName(siteText, expression) {
1039
+ let callee;
1040
+ if (expression !== null && siteText.startsWith(expression)) callee = expression;
1041
+ else {
1042
+ let depth = 0;
1043
+ let open = -1;
1044
+ for (let i = 0; i < siteText.length; i++) {
1045
+ const c = siteText[i];
1046
+ if (c === "/" && siteText[i + 1] === "/") {
1047
+ const nl = siteText.indexOf("\n", i);
1048
+ i = nl < 0 ? siteText.length : nl;
1049
+ } else if (c === "/" && siteText[i + 1] === "*") {
1050
+ const end2 = siteText.indexOf("*/", i + 2);
1051
+ i = end2 < 0 ? siteText.length : end2 + 1;
1052
+ } else if (c === '"' || c === "'") {
1053
+ let j = i + 1;
1054
+ while (j < siteText.length && siteText[j] !== c) j += siteText[j] === "\\" ? 2 : 1;
1055
+ i = j;
1056
+ } else if (c === "`") {
1057
+ const end2 = siteText.indexOf("`", i + 1);
1058
+ i = end2 < 0 ? siteText.length : end2;
1059
+ } else if (c === "(" || c === "[" || c === "{") {
1060
+ if (c === "(" && depth === 0) open = i;
1061
+ depth++;
1062
+ } else if (c === ")" || c === "]" || c === "}") depth--;
1063
+ }
1064
+ if (open < 0) return null;
1065
+ callee = siteText.slice(0, open);
1066
+ }
1067
+ let end = callee.trimEnd();
1068
+ if (end.endsWith("]")) {
1069
+ let depth = 0;
1070
+ for (let i = end.length - 1; i >= 0; i--) {
1071
+ if (end[i] === "]") depth++;
1072
+ else if (end[i] === "[" && --depth === 0) {
1073
+ end = end.slice(0, i);
1074
+ break;
1075
+ }
1076
+ }
1077
+ }
1078
+ const m = NAME2.exec(end);
1079
+ if (!m) return null;
1080
+ return { offset: Buffer.byteLength(end.slice(0, m.index), "utf8"), name: m[1] };
1081
+ }
1082
+ function translateGo(docs, adapter, texts) {
1083
+ const byPath = new Map(docs.map((d) => [d.path, d]));
1084
+ const bytesOf = /* @__PURE__ */ new Map();
1085
+ const linesOf = /* @__PURE__ */ new Map();
1086
+ const byteLines = (path) => {
1087
+ let l = bytesOf.get(path);
1088
+ if (!l) bytesOf.set(path, l = new ByteLines(texts.get(path) ?? ""));
1089
+ return l;
1090
+ };
1091
+ const lines = (path) => {
1092
+ let l = linesOf.get(path);
1093
+ if (!l) linesOf.set(path, l = new Lines(texts.get(path) ?? ""));
1094
+ return l;
1095
+ };
1096
+ const toUtf16 = (path, encoding, line, char) => encoding === "utf8" ? byteLines(path).utf16(line, char) : char;
1097
+ const keyOf = (path, symbol) => symbol.startsWith("local ") ? `${path}\0${symbol}` : symbol.split(" ").filter((_, i) => i !== 3).join(" ");
1098
+ const definitions = /* @__PURE__ */ new Map();
1099
+ for (const d of docs) {
1100
+ if (!texts.has(d.path)) continue;
1101
+ for (const o of d.occurrences) {
1102
+ const key = keyOf(d.path, o.symbol);
1103
+ if (o.roles & DEFINITION && !definitions.has(key) && o.range.length >= 3)
1104
+ definitions.set(key, {
1105
+ file: d.path,
1106
+ line0: o.range[0],
1107
+ char16: toUtf16(d.path, d.encoding, o.range[0], o.range[1])
1108
+ });
1109
+ }
1110
+ }
1111
+ const out = [];
1112
+ for (const f of adapter) {
1113
+ if (f.language !== "go") continue;
1114
+ const text = texts.get(f.path);
1115
+ const doc = byPath.get(f.path);
1116
+ if (text === void 0 || !doc) continue;
1117
+ const bl = byteLines(f.path);
1118
+ const at = /* @__PURE__ */ new Map();
1119
+ for (const o of doc.occurrences)
1120
+ if (!(o.roles & DEFINITION) && o.range.length >= 3) at.set(`${o.range[0]}:${o.range[1]}`, o.symbol);
1121
+ const sites = [
1122
+ ...f.calls.map((c) => ({
1123
+ site: c.site,
1124
+ expression: null,
1125
+ form: c.form === "iife" ? "iife" : "call",
1126
+ iife: c.form === "iife"
1127
+ })),
1128
+ ...f.unresolved.map((u) => ({
1129
+ site: u.site,
1130
+ expression: u.expression,
1131
+ form: "call",
1132
+ iife: false
1133
+ }))
1134
+ ];
1135
+ for (const s of sites) {
1136
+ if (s.iife) {
1137
+ out.push({
1138
+ file: f.path,
1139
+ site: s.site,
1140
+ form: "iife",
1141
+ callee: {
1142
+ file: f.path,
1143
+ name: "",
1144
+ at: { line: s.site.start.line, column: s.site.start.column },
1145
+ kind: "function",
1146
+ exact: true
1147
+ },
1148
+ symbol: ""
1149
+ });
1150
+ continue;
1151
+ }
1152
+ const siteText = bl.bytes.subarray(s.site.start.offset, s.site.end.offset).toString("utf8");
1153
+ const name = goCalleeName(siteText, s.expression);
1154
+ if (!name) continue;
1155
+ const pos = bl.at(s.site.start.offset + name.offset);
1156
+ const char = doc.encoding === "utf8" ? pos.col : bl.utf16(pos.line, pos.col);
1157
+ const symbol = at.get(`${pos.line}:${char}`);
1158
+ if (symbol === void 0) continue;
1159
+ const def = definitions.get(keyOf(f.path, symbol));
1160
+ const descriptor = lastDescriptor(symbol);
1161
+ const literal = descriptor === null && def !== void 0 && /^[\p{L}_][\p{L}\p{N}_]*[^\n=]*:?=\s*func\b/u.test(
1162
+ (texts.get(def.file) ?? "").slice(lines(def.file).starts[def.line0] + def.char16)
1163
+ );
1164
+ const fn = (descriptor?.kind === "function" || literal) && def;
1165
+ out.push({
1166
+ file: f.path,
1167
+ site: s.site,
1168
+ form: s.form,
1169
+ callee: fn ? {
1170
+ file: def.file,
1171
+ name: descriptor?.name ?? name.name,
1172
+ at: lines(def.file).position(def.line0, def.char16),
1173
+ kind: "function"
1174
+ } : null,
1175
+ symbol
1176
+ });
1177
+ }
1178
+ }
1179
+ return out;
1180
+ }
574
1181
  export {
575
1182
  DEFINITION,
576
1183
  DISAGREEMENTS,
577
1184
  IMPORT_DISAGREEMENTS,
1185
+ Lines,
578
1186
  adapterDef,
579
1187
  callSiteOf,
580
1188
  compare,
581
1189
  compareImports,
1190
+ decodeIndex,
582
1191
  formatReport,
1192
+ goCalleeName,
1193
+ goTools,
583
1194
  indexFolder,
1195
+ indexGo,
1196
+ indexPython,
1197
+ lastDescriptor,
584
1198
  readIndex,
585
1199
  resolveWithCompiler,
586
1200
  translate,
1201
+ translateGo,
1202
+ translatePython,
587
1203
  workspacePackages
588
1204
  };