From ffc61e97e5471618323b888ab6cb0d40da3761b0 Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Sat, 17 Oct 2026 02:39:57 -0700 Subject: [PATCH 01/14] Resolve .js imports to .tsx when no .ts exists TypeScript names the output file, so './x.js' can be x.tsx. Mirrors upstream resolution.py. --- internal/extract/jsts_imports_test.go | 53 +++++++++++++++++++++++++++ internal/extract/resolve.go | 11 ++++-- 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 internal/extract/jsts_imports_test.go diff --git a/internal/extract/jsts_imports_test.go b/internal/extract/jsts_imports_test.go new file mode 100644 index 0000000..a239ea4 --- /dev/null +++ b/internal/extract/jsts_imports_test.go @@ -0,0 +1,53 @@ +package extract + +import "testing" + +// A .js specifier names a .ts or .tsx file; a real .js file wins. +func TestJSImportJsFallsBackToTsx(t *testing.T) { + vue := "\n" + _, edges := resolveSrc(t, [][2]string{ + {"Button.tsx", "export function Button() {}\n"}, + {"Widget.tsx", "export function render() {}\n"}, + {"other.ts", "export function render() {}\n"}, + {"Comp.tsx", "export function Comp() {}\n"}, + {"both.ts", "export function bothTs() {}\n"}, + {"both.tsx", "export function bothTsx() {}\n"}, + {"real.js", "export function realJs() {}\n"}, + {"real.ts", "export function realTs() {}\n"}, + {"jsx.ts", "export function jsxTs() {}\n"}, + {"barrel.ts", "export * from './Comp.js';\n"}, + {"main.ts", `import { Button } from './Button.js'; +import { render } from './Widget.js'; +import { bothTs } from './both.js'; +import { realJs } from './real.js'; +import { jsxTs } from './jsx.jsx'; +export function main() { Button(); render(); bothTs(); realJs(); jsxTs() } +`}, + {"plain.js", "import { Button } from './Button.js';\n"}, + {"App.vue", vue}, + }) + for _, want := range [][3]string{ + {"main.ts:main.ts", "imports_from", "Button.tsx:Button.tsx"}, + {"main.ts:main.ts", "imports_from", "Widget.tsx:Widget.tsx"}, + {"main.ts:main.ts", "imports_from", "both.ts:both.ts"}, + {"main.ts:main.ts", "imports_from", "real.js:real.js"}, + {"plain.js:plain.js", "imports_from", "Button.tsx:Button.tsx"}, + {"App.vue:App.vue", "imports_from", "Button.tsx:Button.tsx"}, + {"barrel.ts:barrel.ts", "re_exports", "Comp.tsx:Comp.tsx"}, + // bound by the alias, not the tie-break: render() is defined twice + {"main.ts:main()", "calls", "Widget.tsx:render()"}, + } { + if !edges[want] { + t.Errorf("missing edge %v", want) + } + } + for _, not := range [][3]string{ + {"main.ts:main.ts", "imports_from", "both.tsx:both.tsx"}, + {"main.ts:main.ts", "imports_from", "real.ts:real.ts"}, + {"main.ts:main.ts", "imports_from", "jsx.ts:jsx.ts"}, // .jsx never maps to .ts + } { + if edges[not] { + t.Errorf("unexpected edge %v", not) + } + } +} diff --git a/internal/extract/resolve.go b/internal/extract/resolve.go index c2a295e..f00eea9 100644 --- a/internal/extract/resolve.go +++ b/internal/extract/resolve.go @@ -1536,9 +1536,14 @@ func importTargets(im Imp, corpus map[string]bool, goPkgs map[string][]string, g } // TypeScript names the output file: `./x.js` is x.ts on disk. if ext := path.Ext(spec); jsLike(im.File) && (ext == ".js" || ext == ".jsx") { - ts := strings.TrimSuffix(spec, ext) + ".t" + ext[2:] - if t := resolveRelImport(im.File, ts, corpus); t != "" && t != from { - return []string{t} + tos := []string{".ts", ".tsx"} + if ext == ".jsx" { + tos = tos[1:] + } + for _, to := range tos { + if t := resolveRelImport(im.File, strings.TrimSuffix(spec, ext)+to, corpus); t != "" && t != from { + return []string{t} + } } } } From 7db15825e492a9922c181ed6e03c688e3595d984 Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Sat, 17 Oct 2026 02:40:09 -0700 Subject: [PATCH 02/14] Map .vue, .svelte, .astro and .ejs to the jsts family Stops component calls binding to same-named Python defs. Cross-family import edges from components now drop in build, as upstream does. --- internal/extract/jsmodules.go | 5 ----- internal/extract/jsts_imports_test.go | 13 +++++++++++++ internal/langfamily/langfamily.go | 2 ++ internal/langfamily/langfamily_test.go | 4 ++++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/internal/extract/jsmodules.go b/internal/extract/jsmodules.go index 5b3725f..890239e 100644 --- a/internal/extract/jsmodules.go +++ b/internal/extract/jsmodules.go @@ -1,7 +1,6 @@ package extract import ( - "path" "strings" "github.com/dobbo-ca/graphify-go/internal/idutil" @@ -11,10 +10,6 @@ import ( // jsLike reports whether a file's imports are JS/TS ones (see jsImp). func jsLike(file string) bool { - switch path.Ext(file) { - case ".vue", ".svelte", ".astro": - return true - } return langfamily.Of(file) == "jsts" } diff --git a/internal/extract/jsts_imports_test.go b/internal/extract/jsts_imports_test.go index a239ea4..73fa335 100644 --- a/internal/extract/jsts_imports_test.go +++ b/internal/extract/jsts_imports_test.go @@ -51,3 +51,16 @@ export function main() { Button(); render(); bothTs(); realJs(); jsxTs() } } } } + +// A call from a component never binds to a Python definition. +func TestSFCCallDoesNotCrossToPython(t *testing.T) { + _, edges := resolveSrc(t, [][2]string{ + {"App.vue", "\n"}, + {"util.py", "def processData():\n pass\n"}, + }) + for e := range edges { + if e[1] == "calls" && e[0][:3] == "App" { + t.Errorf("unexpected edge %v", e) + } + } +} diff --git a/internal/langfamily/langfamily.go b/internal/langfamily/langfamily.go index 4c7ccc9..03eee1c 100644 --- a/internal/langfamily/langfamily.go +++ b/internal/langfamily/langfamily.go @@ -26,6 +26,8 @@ var byExt = map[string]string{ // JS/TS module graph ".js": "jsts", ".jsx": "jsts", ".mjs": "jsts", ".cjs": "jsts", ".ts": "jsts", ".mts": "jsts", ".cts": "jsts", ".tsx": "jsts", + // Single-file components script as JS/TS + ".vue": "jsts", ".svelte": "jsts", ".astro": "jsts", ".ejs": "jsts", // Python ".py": "py", // JVM interop (Java/Kotlin/Scala) diff --git a/internal/langfamily/langfamily_test.go b/internal/langfamily/langfamily_test.go index 9ed6c23..22f4ec7 100644 --- a/internal/langfamily/langfamily_test.go +++ b/internal/langfamily/langfamily_test.go @@ -15,6 +15,10 @@ func TestCross(t *testing.T) { {"a.ts", "b.tsx", false}, // both jsts {"a.py", "b.py", false}, // same family {"a.go", "b.go", false}, // same family + {"App.vue", "util.py", true}, // sfc vs python + {"App.svelte", "lib.ts", false}, // sfc is jsts + {"Page.astro", "a.js", false}, // sfc is jsts + {"App.vue", "Other.vue", false}, // both jsts {"README.md", "a.py", false}, // unknown family stays permissive {"a.py", "", false}, // empty file stays permissive } From c8eea8c0befef50ba1a264717b39486bdcd8a57e Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Sat, 17 Oct 2026 02:49:21 -0700 Subject: [PATCH 03/14] Add new X() calls and a dynamic_import relation for JS/TS A new X() binds like a bare call, across files only with an import, and skips upstream's builtin names. import() gives a file-level dynamic_import plus a deferred imports_from from the enclosing function; ImportCycles ignores deferred edges and affected walks dynamic_import. Cache schema 22. --- internal/analyze/analyze.go | 5 +- internal/analyze/typeonly_cycle_test.go | 15 ++ internal/cache/cache.go | 2 +- internal/export/export.go | 6 +- internal/export/stream_test.go | 1 + internal/extract/component.go | 2 +- internal/extract/extract.go | 21 +++ internal/extract/javascript.go | 83 ++++++++-- internal/extract/javascript_test.go | 3 +- internal/extract/jsts_imports_test.go | 192 +++++++++++++++++++++++- internal/extract/resolve.go | 75 ++++++++- internal/extract/resultjson.go | 6 + internal/extract/resultjson_test.go | 8 +- internal/model/code.go | 21 +-- internal/model/code_test.go | 19 ++- internal/model/model.go | 35 ++++- internal/query/affected.go | 2 +- internal/query/affected_test.go | 14 ++ 18 files changed, 460 insertions(+), 50 deletions(-) diff --git a/internal/analyze/analyze.go b/internal/analyze/analyze.go index 27f9fee..ef55881 100644 --- a/internal/analyze/analyze.go +++ b/internal/analyze/analyze.go @@ -177,8 +177,9 @@ func ImportCycles(g *model.Graph, maxLen, topN int) []Cycle { raw := map[string][]string{} for _, e := range g.Edges() { // Type-only imports are erased at compile time, so they cannot form a - // runtime cycle (upstream find_import_cycles skips them too). - if e.Relation != model.RelImportsFrom || e.TypeOnly() { + // runtime cycle (upstream find_import_cycles skips them too). A deferred + // import() runs late, so it closes no static cycle either. + if e.Relation != model.RelImportsFrom || e.TypeOnly() || e.Deferred() { continue } uf, vf := g.Nodes[e.Source].SourceFile, g.Nodes[e.Target].SourceFile diff --git a/internal/analyze/typeonly_cycle_test.go b/internal/analyze/typeonly_cycle_test.go index b3c441d..2b0d7f5 100644 --- a/internal/analyze/typeonly_cycle_test.go +++ b/internal/analyze/typeonly_cycle_test.go @@ -64,3 +64,18 @@ func TestImportCyclesTypeAndValueImportSameModule(t *testing.T) { } } } + +// A deferred import() runs late, so it closes no static cycle. +func TestImportCyclesSkipsDeferredImports(t *testing.T) { + for _, imp := range []string{ + "export async function lazy() { return import('./b'); }", // from a function + "describe('x', () => { import('./b'); });", // from the file + } { + if cycles := ImportCycles(tsCycleGraph(t, imp), 5, 20); len(cycles) != 0 { + t.Errorf("expected no cycles for %q, got %v", imp, cycles) + } + } + if cycles := ImportCycles(tsCycleGraph(t, "import { B } from './b';"), 5, 20); len(cycles) != 1 { + t.Errorf("a static import still closes the cycle, got %v", cycles) + } +} diff --git a/internal/cache/cache.go b/internal/cache/cache.go index f43ef45..ab92290 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -103,7 +103,7 @@ type StatIndex map[string]StatEntry // schema is the on-disk cache layout revision. Bump it when the shape of Entry // or StatEntry changes so older files are discarded rather than misread. -const schema = 21 +const schema = 22 // Stamp is the cache identity for a binary: its version plus the on-disk schema // revision. A cache file written under a different stamp is discarded, so an diff --git a/internal/export/export.go b/internal/export/export.go index 0069302..1c77eab 100644 --- a/internal/export/export.go +++ b/internal/export/export.go @@ -57,6 +57,7 @@ type jsonLink struct { Target string `json:"target"` Relation string `json:"relation"` Confidence string `json:"confidence"` + Deferred bool `json:"deferred,omitempty"` SourceFile string `json:"source_file,omitempty"` SourceLocation model.Loc `json:"source_location,omitempty"` Weight float64 `json:"weight,omitempty"` @@ -258,6 +259,9 @@ func appendJSONLink(b []byte, l jsonLink) ([]byte, error) { b = model.AppendJSONString(b, l.Relation) b = append(b, ",\n \"confidence\": "...) b = model.AppendJSONString(b, l.Confidence) + if l.Deferred { + b = append(b, ",\n \"deferred\": true"...) + } if l.SourceFile != "" { b = append(b, ",\n \"source_file\": "...) b = model.AppendJSONString(b, l.SourceFile) @@ -307,7 +311,7 @@ func toJSONLink(e *model.Edge) jsonLink { } return jsonLink{ Source: e.Source, Target: e.Target, Relation: e.Relation.String(), - Confidence: e.Confidence.String(), SourceFile: e.SourceFile.String(), + Confidence: e.Confidence.String(), Deferred: e.Deferred(), SourceFile: e.SourceFile.String(), SourceLocation: e.Loc(), Weight: e.Weight, ConfidenceScore: score, } } diff --git a/internal/export/stream_test.go b/internal/export/stream_test.go index 6466979..f82ddaa 100644 --- a/internal/export/stream_test.go +++ b/internal/export/stream_test.go @@ -68,6 +68,7 @@ func streamGraph(n, m int) (*model.Graph, map[int][]string) { case 1: e.SourceFile, e.SourceLocation, e.Weight = model.FileOf("b/日.py"), model.Line(9), 0.1+float64(i)/3 e.SetTypeOnly(i%2 == 0) + e.SetDeferred(i%2 == 1) case 2: e.ConfidenceScore = 1e-7 * float64(i) if i%5 == 0 { diff --git a/internal/extract/component.go b/internal/extract/component.go index a807e5c..a4a0ccd 100644 --- a/internal/extract/component.go +++ b/internal/extract/component.go @@ -52,7 +52,7 @@ func extractComponent(rel string, src []byte, isAstro bool) Result { if spec == "" { continue } - res.Imps = append(res.Imps, Imp{FileID: fileID, File: rel, Spec: spec, Loc: model.Line(lineOf(src, m[0]))}) + res.Imps = append(res.Imps, Imp{FileID: fileID, File: rel, Spec: spec, Loc: model.Line(lineOf(src, m[0])), Dyn: dynImport}) } return res } diff --git a/internal/extract/extract.go b/internal/extract/extract.go index d7a265c..d18a008 100644 --- a/internal/extract/extract.go +++ b/internal/extract/extract.go @@ -111,6 +111,10 @@ type Imp struct { Rel bool // Export marks a JS/TS `export ... from Spec`. Export bool `json:",omitempty"` + // Dyn marks a JS/TS `import(Spec)`: dynImport asks for a file-level + // dynamic_import, dynDeferred for a deferred imports_from from FileID, + // which is then the function holding the call. + Dyn uint8 `json:",omitempty"` // Names are the names a Python `from Spec import ...` or Rust `use Spec::...` // binds; each may be a module. For JS/TS see jsImp: a name costs 16 bytes and // its text, a bare import nothing; Export fits the padding after Rel. @@ -526,6 +530,23 @@ func (b *builder) impTyped(spec string, loc model.Loc, typeOnly bool) { } } +const ( + dynImport uint8 = iota + 1 + dynDeferred +) + +// impDynamic records `import(spec)`: the file depends on it late, and so does +// caller, the function holding the call, when there is one. +func (b *builder) impDynamic(spec string, loc model.Loc, caller string) { + if spec == "" { + return + } + b.res.Imps = append(b.res.Imps, Imp{FileID: b.fileID, File: b.file, Spec: spec, Loc: loc, Dyn: dynImport}) + if caller != "" { + b.res.Imps = append(b.res.Imps, Imp{FileID: caller, File: b.file, Spec: spec, Loc: loc, Dyn: dynDeferred}) + } +} + // impRel is imp for a spec relative to the importing file's directory. func (b *builder) impRel(spec string, loc model.Loc) { if spec != "" { diff --git a/internal/extract/javascript.go b/internal/extract/javascript.go index b84197b..ea8464d 100644 --- a/internal/extract/javascript.go +++ b/internal/extract/javascript.go @@ -21,6 +21,7 @@ import ( func init() { for _, l := range []*gositter.Language{tsjs.Language(), tsts.Language(), tstsx.Language()} { l.KindMask("call_expression") + l.KindMask("new_expression") l.KindMask("export_statement") } } @@ -42,9 +43,8 @@ func extractJS(rel string, src []byte, langPtr *gositter.Language) Result { slices.SortStableFunc(b.jsBodies, func(x, y jsBody) int { return cmp.Compare(x.start, y.start) }) next := 0 var open []jsBody - walkKinds(root, langPtr.KindMask("call_expression"), func(cv ts.Node) bool { + walkKinds(root, langPtr.KindMask("call_expression")|langPtr.KindMask("new_expression"), func(cv ts.Node) bool { c := &cv - b.jsCallImport(c, src) for len(open) > 0 && c.StartByte() >= open[len(open)-1].end { open = open[:len(open)-1] } @@ -54,6 +54,11 @@ func extractJS(rel string, src []byte, langPtr *gositter.Language) Result { } next++ } + caller := "" + if len(open) > 0 { + caller = open[len(open)-1].callerID + } + b.jsCallImport(c, src, caller) if len(open) > 0 { b.jsCall(c, open[len(open)-1], src) } @@ -204,21 +209,32 @@ func (b *builder) jsChildren(n *ts.Node, src []byte, owner string) { } } -// jsCallImport records `require("m")` and dynamic `import("m")` as imports. -// Only a lone string literal counts; a computed spec names no file. -func (b *builder) jsCallImport(n *ts.Node, src []byte) { +// jsCallImport records `require("m")` and dynamic `import("m")` as imports; +// caller is the function holding the call. A computed spec names no file. +func (b *builder) jsCallImport(n *ts.Node, src []byte, caller string) { if n.Kind() != "call_expression" { return } fn, args := n.ChildByFieldName("function"), n.ChildByFieldName("arguments") - if fn == nil || args == nil || args.NamedChildCount() != 1 { - return - } - if fn.Kind() != "import" && (fn.Kind() != "identifier" || fn.Utf8Text(src) != "require") { + if fn == nil || args == nil || args.NamedChildCount() == 0 { return } - if a := args.NamedChild(0); a.Kind() == "string" { - b.imp(unquote(a.Utf8Text(src)), line(n)) + a := args.NamedChild(0) + switch { + case fn.Kind() == "identifier" && fn.Utf8Text(src) == "require": + if args.NamedChildCount() == 1 && a.Kind() == "string" { + b.imp(unquote(a.Utf8Text(src)), line(n)) + } + case fn.Kind() == "import": + switch a.Kind() { + case "string": + b.impDynamic(unquote(a.Utf8Text(src)), line(n), caller) + case "template_string": + // a substitution makes the spec computed + if spec := strings.Trim(a.Utf8Text(src), "`"); !strings.Contains(spec, "$") { + b.impDynamic(spec, line(n), caller) + } + } } } @@ -1117,11 +1133,44 @@ func (b *builder) jsCalls(body *ts.Node, callerID string) { b.jsBodies = append(b.jsBodies, jsBody{body.StartByte(), body.EndByte(), callerID, slices.Clone(b.jsNS)}) } -// jsCall records the call site c, a call_expression, for the body's caller. +// jsNewBuiltins are the names upstream never binds a `new` to: its one +// builtin list for every language, Swift types included. +var jsNewBuiltins = map[string]bool{} + +func init() { + for _, n := range strings.Fields(`String Number Boolean Object Array Symbol BigInt Date RegExp Error + TypeError RangeError SyntaxError ReferenceError EvalError URIError Promise Map Set WeakMap + WeakSet JSON Math Reflect Proxy Intl parseInt parseFloat isNaN isFinite encodeURIComponent + decodeURIComponent encodeURI decodeURI URL URLSearchParams FormData Blob File Headers + Request Response AbortController AbortSignal TextEncoder TextDecoder console str int float + bool list dict set tuple bytes len range enumerate zip map filter sum min max print open + isinstance type super sorted reversed any all abs round next iter hash id repr callable + getattr setattr hasattr delattr vars dir Int Int8 Int16 Int32 Int64 UInt UInt8 UInt16 + UInt32 UInt64 Double Float Bool Character Sendable Codable Decodable Encodable Equatable + Hashable Identifiable Comparable CaseIterable RawRepresentable CustomStringConvertible + CustomDebugStringConvertible AnyObject LocalizedError Data UUID Decimal Calendar Locale + TimeZone Bundle IndexPath IndexSet NotificationCenter UserDefaults FileManager URLSession + URLRequest URLComponents JSONDecoder JSONEncoder DateFormatter NumberFormatter + ISO8601DateFormatter NSObject NSString NSError NSLock NSAttributedString DispatchQueue + DispatchGroup OperationQueue RunLoop View Color Font`) { + jsNewBuiltins[n] = true + } +} + +// jsNewRecv marks the call of a `new X()`, which binds only with import evidence. +const jsNewRecv = "\x00new" + +// jsCall records the call site c, a call_expression or new_expression, for +// the body's caller. func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { callerID := body.callerID + isNew := c.Kind() == "new_expression" fn := c.ChildByFieldName("function") - if fn == nil || callerID == "" { + if isNew { + fn = c.ChildByFieldName("constructor") + } + // A file-level `new` makes no edge upstream. + if fn == nil || callerID == "" || isNew && callerID == b.fileID { return } // A method body is a boundary: its calls are no caller's. @@ -1133,6 +1182,12 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { switch fn.Kind() { case "identifier": name := fn.Utf8Text(src) + if isNew { + if !jsNewBuiltins[name] { + b.callRecv(callerID, name, jsNewRecv, line(c)) + } + return + } for s := callerID; s != ""; s = b.jsParent[s] { if id, ok := b.jsNested[[2]string{s, name}]; ok { b.jsNestedCall(callerID, id, line(c)) @@ -1153,7 +1208,7 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { } b.call(callerID, name, line(c)) case "member_expression": - if p := fn.ChildByFieldName("property"); p != nil { + if p := fn.ChildByFieldName("property"); p != nil && !isNew { b.callRecv(callerID, p.Utf8Text(src), recvText(fn, p, src), line(c)) } } diff --git a/internal/extract/javascript_test.go b/internal/extract/javascript_test.go index c302006..6b30084 100644 --- a/internal/extract/javascript_test.go +++ b/internal/extract/javascript_test.go @@ -57,14 +57,13 @@ func TestExtractJSRationale(t *testing.T) { } } -// CommonJS require, re-exports and dynamic import each link to the target file. +// CommonJS require and re-exports each link to the target file. func TestJSImportFormsResolveToFiles(t *testing.T) { cases := []struct{ name, src string }{ {"require", "const lib = require('./lib');\n"}, {"nested require", "function f() {\n return require(\"./lib\").x;\n}\n"}, {"export star", "export * from './lib';\n"}, {"export named", "export { thing } from './lib';\n"}, - {"dynamic import", "async function f() {\n const m = await import('./lib');\n}\n"}, } for _, c := range cases { got := importEdges(map[string]string{"src/a.ts": c.src, "src/lib.ts": "export const thing = 1;\n"}, nil) diff --git a/internal/extract/jsts_imports_test.go b/internal/extract/jsts_imports_test.go index 73fa335..bc769d7 100644 --- a/internal/extract/jsts_imports_test.go +++ b/internal/extract/jsts_imports_test.go @@ -1,6 +1,10 @@ package extract -import "testing" +import ( + "slices" + "strings" + "testing" +) // A .js specifier names a .ts or .tsx file; a real .js file wins. func TestJSImportJsFallsBackToTsx(t *testing.T) { @@ -64,3 +68,189 @@ func TestSFCCallDoesNotCrossToPython(t *testing.T) { } } } + +// edgeSet names each edge "file:label relation file:label", with " deferred" +// after a deferred one, for the relations that tests below look at. +func edgeSet(t *testing.T, srcs [][2]string, rels ...string) map[string]bool { + t.Helper() + var results []Result + var files []string + for _, s := range srcs { + results = append(results, FileFromBytes(s[0], []byte(s[1]))) + files = append(files, s[0]) + } + ext := Resolve(results, files) + name := map[string]string{} + for _, n := range ext.Nodes { + name[n.ID] = n.SourceFile + ":" + n.Label + } + out := map[string]bool{} + for _, e := range ext.Edges { + if !slices.Contains(rels, e.Relation.String()) { + continue + } + k := name[e.Source] + " " + e.Relation.String() + " " + name[e.Target] + if e.Deferred() { + k += " deferred" + } + out[k] = true + } + return out +} + +func checkEdges(t *testing.T, got map[string]bool, want, not []string) { + t.Helper() + for _, w := range want { + if !got[w] { + t.Errorf("missing edge %q", w) + } + } + for _, n := range not { + if got[n] { + t.Errorf("unexpected edge %q", n) + } + } +} + +// `new X()` binds like a bare call, but across files only with an import. +func TestJSNewExpression(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"shapes.ts", "export class Circle {}\nexport class Item {}\n"}, + {"other.ts", "export class Item {}\nexport class Lonely {}\n"}, + {"orphan.ts", "export class Orphan {}\n"}, + {"side.ts", "export class Side {}\n"}, + {"main.ts", `import { Circle, Item } from './shapes'; +import * as ns from './other'; +class Local {} +export function mk() { return new Local(); } +export function mkNoParens() { return new Local; } +export const arrow = () => new Local(); +export function mkCircle() { return new Circle(); } +export function mkItem() { return new Item(); } +export function mkLonely() { return new Lonely(); } +export function mkNs() { return new ns.Item(); } +export function mkMap() { return new Map(); } +export function mkOrphan() { return new Orphan(); } +export class Holder { make() { return new Circle(); } } +const top = new Circle(); +describe('x', () => { new Circle(); new Local(); }); +`}, + {"effect.ts", "import './side';\nexport function c() { return new Side(); }\n"}, + {"noeffect.ts", "export function c() { return new Side(); }\n"}, + {"plain.js", "class JsThing {}\nexport function mkJs() { return new JsThing(); }\n"}, + {"builtin.ts", "class Response {}\nexport function r() { return new Response(); }\n"}, + }, "calls") + checkEdges(t, got, []string{ + "main.ts:mk() calls main.ts:Local", + "main.ts:mkNoParens() calls main.ts:Local", + "main.ts:arrow() calls main.ts:Local", + "main.ts:mkCircle() calls shapes.ts:Circle", + "main.ts:mkItem() calls shapes.ts:Item", // the import picks one of two + "main.ts:mkLonely() calls other.ts:Lonely", + "main.ts:Holder.make() calls shapes.ts:Circle", + "effect.ts:c() calls side.ts:Side", + "plain.js:mkJs() calls plain.js:JsThing", + }, []string{ + "main.ts:mkItem() calls other.ts:Item", + "main.ts:mkNs() calls other.ts:Item", // a namespace member is no edge + "main.ts:mkNs() calls shapes.ts:Item", + "main.ts:mkOrphan() calls orphan.ts:Orphan", // never imported + "noeffect.ts:c() calls side.ts:Side", + "builtin.ts:r() calls builtin.ts:Response", + }) + // no edge for a builtin name, or for a new at the file level or in its closure + for k := range got { + if strings.HasPrefix(k, "main.ts:mkMap") || strings.HasPrefix(k, "main.ts:main.ts calls") { + t.Errorf("unexpected edge %q", k) + } + } +} + +// import() is a file-level dynamic_import once per target, and a deferred +// imports_from from the function holding it; require() stays imports_from. +func TestJSDynamicImport(t *testing.T) { + dyn := `const top = import("./a.js"); +export function f() { return import("./b"); } +export const g = async () => (await import("./c")).c; +export function t() { return import(` + "`./d`" + `); } +const r = require("./e"); +export function lazy() { const m = require("./f"); return m; } +export function ext() { return import("lodash"); } +export function twice() { import("./b"); return import("./b", { with: {} }); } +function inner() { return import("./h"); } +export function sub(n) { return import(` + "`./${n}`" + `); } +` + srcs := [][2]string{{"dyn.ts", dyn}, {"dyn2.js", "export function jf() { return import('./a.js'); }\n"}} + for _, f := range []string{"a", "b", "c", "d", "e", "f", "h"} { + srcs = append(srcs, [2]string{f + ".ts", "export function " + f + "() {}\n"}) + } + got := edgeSet(t, srcs, "imports_from", "dynamic_import", "imports") + checkEdges(t, got, []string{ + "dyn.ts:dyn.ts dynamic_import a.ts:a.ts", + "dyn.ts:dyn.ts dynamic_import b.ts:b.ts", + "dyn.ts:dyn.ts dynamic_import c.ts:c.ts", + "dyn.ts:dyn.ts dynamic_import d.ts:d.ts", + "dyn.ts:dyn.ts dynamic_import h.ts:h.ts", + "dyn.ts:dyn.ts dynamic_import :lodash", + "dyn2.js:dyn2.js dynamic_import a.ts:a.ts", + "dyn.ts:f() imports_from b.ts:b.ts deferred", + "dyn.ts:twice() imports_from b.ts:b.ts deferred", + "dyn.ts:g() imports_from c.ts:c.ts deferred", + "dyn.ts:t() imports_from d.ts:d.ts deferred", + "dyn.ts:inner() imports_from h.ts:h.ts deferred", + "dyn.ts:ext() imports_from :lodash deferred", + "dyn2.js:jf() imports_from a.ts:a.ts deferred", + "dyn.ts:dyn.ts imports_from e.ts:e.ts", + "dyn.ts:dyn.ts imports_from f.ts:f.ts", // require() stays at the file + }, []string{ + "dyn.ts:dyn.ts imports_from a.ts:a.ts", + "dyn.ts:dyn.ts imports_from b.ts:b.ts", + "dyn.ts:dyn.ts imports_from h.ts:h.ts", + "dyn.ts:dyn.ts dynamic_import e.ts:e.ts", + "dyn.ts:dyn.ts imports :lodash", + }) + n := 0 + for k := range got { + if strings.HasPrefix(k, "dyn.ts:dyn.ts dynamic_import") { + n++ + } + } + if n != 6 { + t.Errorf("dyn.ts has %d dynamic_import edges, want 6 (a b c d h lodash)", n) + } +} + +// An import() in a closure of a top-level call is the file's deferred import, +// and replaces its dynamic_import. In a function it is both. +func TestJSDynamicImportInTopLevelClosure(t *testing.T) { + a := [2]string{"a.ts", "export function a() {}\n"} + got := edgeSet(t, [][2]string{a, {"spec.ts", "describe('x', () => { import('./a'); });\n"}}, "imports_from", "dynamic_import") + checkEdges(t, got, []string{"spec.ts:spec.ts imports_from a.ts:a.ts deferred"}, + []string{"spec.ts:spec.ts dynamic_import a.ts:a.ts", "spec.ts:spec.ts imports_from a.ts:a.ts"}) + + // the closure of the file wins over a function, for the same target + got = edgeSet(t, [][2]string{a, {"spec.ts", "function f() { import('./a'); }\ndescribe('x', () => { import('./a'); });\n"}}, "imports_from", "dynamic_import") + checkEdges(t, got, []string{"spec.ts:spec.ts imports_from a.ts:a.ts deferred", "spec.ts:f() imports_from a.ts:a.ts deferred"}, + []string{"spec.ts:spec.ts dynamic_import a.ts:a.ts"}) +} + +// Components get a dynamic_import for the file and a deferred edge for the function. +func TestSFCDynamicImport(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"a.ts", "export function a() {}\n"}, + {"b.ts", "export function b() {}\n"}, + {"Comp.vue", "\n\n"}, + {"Page.svelte", "\n
\n"}, + {"Lay.astro", "---\nconst m = await import('./a');\n---\n
\n"}, + }, "imports_from", "dynamic_import") + checkEdges(t, got, []string{ + "Comp.vue:Comp.vue dynamic_import a.ts:a.ts", + "Comp.vue:A() imports_from a.ts:a.ts deferred", + "Page.svelte:Page.svelte dynamic_import b.ts:b.ts", + "Lay.astro:Lay.astro dynamic_import a.ts:a.ts", + }, []string{ + "Comp.vue:Comp.vue imports_from a.ts:a.ts", + "Page.svelte:Page.svelte imports_from b.ts:b.ts", + "Lay.astro:Lay.astro imports_from a.ts:a.ts", + }) +} diff --git a/internal/extract/resolve.go b/internal/extract/resolve.go index f00eea9..a93ea14 100644 --- a/internal/extract/resolve.go +++ b/internal/extract/resolve.go @@ -160,7 +160,9 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E type impRec struct { imp *Imp // source, file and location tgt string + rel model.Relation // 0 is imports_from, or imports if external typeOnly, external bool + deferred bool } var impEdges [][]impRec // parts of impPart type impBatch struct { @@ -185,6 +187,12 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E num uint32 } ids := map[string]tgtID{} + // An import() makes one edge of each kind per source and target. + type dynKey struct { + src, tgt string + rel model.Relation + } + dynSeen := map[dynKey]bool{} n := 0 add := func(e impRec) { if n%impPart == 0 { @@ -196,6 +204,21 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E for b := range batches { nextImp := 0 for _, r := range b.results { + // A closure of the file that defers a target replaces its dynamic_import. + var fileDeferred map[string]bool + for k := range r.Imps { + if im := &r.Imps[k]; im.Dyn == dynDeferred && im.FileID == idutil.MakeID(im.File) { + if fileDeferred == nil { + fileDeferred = map[string]bool{} + } + for _, t := range b.targets[nextImp+k] { + fileDeferred[idutil.MakeID(t)] = true + } + if len(b.targets[nextImp+k]) == 0 { + fileDeferred[idutil.MakeID(im.Spec)] = true + } + } + } for k := range r.Imps { im := &r.Imps[k] src, ok := srcNum[im.FileID] @@ -205,6 +228,37 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } targets := b.targets[nextImp] nextImp++ + if im.Dyn != 0 { + rel := model.RelDynamicImport + if im.Dyn == dynDeferred { + rel = model.RelImportsFrom + } + var tgts []string + for _, t := range targets { + tgts = append(tgts, idutil.MakeID(t)) + } + external := len(tgts) == 0 + if external { + depID := idutil.MakeID(im.Spec) + if depID == "" { + continue + } + if !extSeen[depID] { + extSeen[depID] = true + impNodes = append(impNodes, model.Node{ID: depID, Label: im.Spec, FileType: "concept"}) + } + tgts = append(tgts, depID) + } + for _, t := range tgts { + key := dynKey{im.FileID, t, rel} + if dynSeen[key] || rel == model.RelDynamicImport && fileDeferred[t] { + continue + } + dynSeen[key] = true + add(impRec{imp: im, tgt: t, rel: rel, external: external, deferred: im.Dyn == dynDeferred}) + } + continue + } for _, target := range targets { t, ok := ids[target] if !ok { @@ -254,7 +308,8 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E targets := importTargets(im, corpus, goPkgs, goMods, goTargets) b.targets = append(b.targets, targets) external[[2]string{im.File, im.Spec}] = len(targets) == 0 - if len(targets) > 0 { + // Only a closure of the file imports for the file; a function's import() runs late. + if len(targets) > 0 && (im.Dyn == 0 || im.Dyn == dynDeferred && im.FileID == idutil.MakeID(im.File)) { impTargets[im.File] = append(impTargets[im.File], targets) } } @@ -564,6 +619,10 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E continue } tgt, conf := "", model.ConfInferred + isNew := c.Recv == jsNewRecv + if isNew { + c.Recv = "" + } super, isSelf := selfRecv[c.Recv] if ext := langRecv[c.Recv]; ext != "" && path.Ext(r.File) != ext { isSelf = false @@ -679,6 +738,10 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E if tgt != "" && langfamily.Cross(r.File, idFile[tgt]) { tgt = "" } + // A class of another file binds to `new` only if the file imports it. + if isNew && tgt != "" && idFile[tgt] != r.File && jsAlias[[2]string{r.File, c.Callee}] != tgt && !imported.of(r.File)[idFile[tgt]] { + tgt = "" + } // A call on an external module has no definition to bind, so it // lands on the module's import node. if tgt == "" && i < len(r.CallMods) && r.CallMods[i] != "" && external[[2]string{r.File, r.CallMods[i]}] { @@ -760,15 +823,19 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E for _, recs := range impEdges { part := make([]model.Edge, 0, len(recs)) for _, e := range recs { - rel := model.RelImportsFrom - if e.external { - rel = model.RelImports + rel := e.rel + if rel == 0 { + rel = model.RelImportsFrom + if e.external { + rel = model.RelImports + } } part = append(part, model.Edge{ Source: e.imp.FileID, Target: e.tgt, Relation: rel, Confidence: model.ConfExtracted, SourceFile: fc.of(e.imp.File), SourceLocation: e.imp.Loc, }) part[len(part)-1].SetTypeOnly(e.typeOnly) + part[len(part)-1].SetDeferred(e.deferred) } edges = append(edges, part) } diff --git a/internal/extract/resultjson.go b/internal/extract/resultjson.go index 5b77da2..7bbeea1 100644 --- a/internal/extract/resultjson.go +++ b/internal/extract/resultjson.go @@ -70,6 +70,9 @@ func (r *Result) AppendJSON(b []byte) ([]byte, error) { if i.Export { b = append(b, `"Export":true,`...) } + if i.Dyn != 0 { + b = append(strconv.AppendUint(append(b, `"Dyn":`...), uint64(i.Dyn), 10), ',') + } b = append(b, `"Names":`...) b, _ = appendJSONArr(b, i.Names, func(b []byte, s *string) ([]byte, error) { return model.AppendJSONString(b, *s), nil @@ -196,6 +199,9 @@ func appendJSONEdge(b []byte, e *model.Edge) ([]byte, error) { if e.TypeOnly() { b = append(b, `,"type_only":true`...) } + if e.Deferred() { + b = append(b, `,"deferred":true`...) + } if e.SourceFile != 0 { b = appendJSONFields(b, `,"source_file":`, e.SourceFile.String()) } diff --git a/internal/extract/resultjson_test.go b/internal/extract/resultjson_test.go index c7efe80..be31167 100644 --- a/internal/extract/resultjson_test.go +++ b/internal/extract/resultjson_test.go @@ -24,7 +24,7 @@ func fillJSON(v reflect.Value, n *int) { v.SetFloat(float64(*n) / 4) case reflect.Int, reflect.Int64: v.SetInt(int64(*n)) - case reflect.Uint16, reflect.Uint32: // an edge code that both tables hold + case reflect.Uint8, reflect.Uint16, reflect.Uint32: // an edge code that both tables hold v.SetUint(uint64(1 + *n%3)) case reflect.Slice: v.Set(reflect.MakeSlice(v.Type(), 2, 2)) @@ -61,6 +61,8 @@ func TestResultAppendJSONMatchesMarshal(t *testing.T) { typeOnly, typeOnlyAt := model.Edge{Weight: 3}, model.Edge{SourceLocation: model.Line(7)} typeOnly.SetTypeOnly(true) typeOnlyAt.SetTypeOnly(true) + deferred := model.Edge{SourceLocation: model.Line(4)} + deferred.SetDeferred(true) cases := map[string]Result{ "zero": {}, "full": full, @@ -69,10 +71,10 @@ func TestResultAppendJSONMatchesMarshal(t *testing.T) { ImportAliases: []ImportAlias{}, MDRefs: []MDRef{}, GoMods: map[string]string{}}, "omitted and escaped": { Nodes: []model.Node{{ID: odd, Label: odd}, {Attributes: map[string]string{}}, {Attributes: map[string]string{"b": odd, "a": "1"}}}, - Edges: []model.Edge{{Source: odd}, {Weight: 1e-7, ConfidenceScore: 1e21}, {Weight: math.Copysign(0, -1), ConfidenceScore: -2.5}, typeOnly, typeOnlyAt}, + Edges: []model.Edge{{Source: odd}, {Weight: 1e-7, ConfidenceScore: 1e21}, {Weight: math.Copysign(0, -1), ConfidenceScore: -2.5}, typeOnly, typeOnlyAt, deferred}, Defs: []Def{{ID: odd, Abstract: true, Value: true}, {}}, Calls: []Call{{Callee: 1}}, CallText: []string{"", odd}, - Imps: []Imp{{Spec: odd, Names: []string{odd, ""}}, {Rel: true}, {TypeOnly: true}}, + Imps: []Imp{{Spec: odd, Names: []string{odd, ""}}, {Rel: true}, {TypeOnly: true}, {Dyn: 2}}, }, } for name, r := range cases { diff --git a/internal/model/code.go b/internal/model/code.go index 8baa4f0..3281783 100644 --- a/internal/model/code.go +++ b/internal/model/code.go @@ -31,6 +31,7 @@ const ( RelCites RelImplements RelReExports + RelDynamicImport ) const ( @@ -41,7 +42,7 @@ const ( var ( relTable = newCodeTable("", "contains", "calls", "imports", "imports_from", "references", "inherits", - "depends_on", "crate_depends_on", "case_of", "dispatches_to", "rationale_for", "cites", "implements", "re_exports") + "depends_on", "crate_depends_on", "case_of", "dispatches_to", "rationale_for", "cites", "implements", "re_exports", "dynamic_import") confTable = newCodeTable("", "EXTRACTED", "INFERRED", "AMBIGUOUS") ) @@ -111,19 +112,21 @@ func (c *Confidence) UnmarshalText(b []byte) error { // Loc is a source location. A line "L" is held as the number n+1 and any // other text as locText plus a code into a process-wide table; 0 is none. // It takes 4 bytes of an Edge or a call record, not 16 and a string. -// Bit 30 is an Edge's type-only flag, which the methods ignore. +// Bits 30 and 29 are an Edge's type-only and deferred flags, which the methods ignore. type Loc uint32 const ( locText = 1 << 31 locTypeOnly = 1 << 30 + locDeferred = 1 << 29 + locFlags = locTypeOnly | locDeferred ) var locTable = newCodeTable("") // Line returns the location of line n. func Line(n int) Loc { - if n < 0 || n > locTypeOnly-2 { + if n < 0 || n > locDeferred-2 { return LocOf("L" + strconv.Itoa(n)) } return Loc(n + 1) @@ -144,15 +147,15 @@ func LocOf(s string) Loc { } n = n*10 + int(s[i]-'0') } - if n >= 0 && n <= locTypeOnly-2 { + if n >= 0 && n <= locDeferred-2 { return Loc(n + 1) } } - return locText | Loc(locTable.code(s))&(locTypeOnly-1) + return locText | Loc(locTable.code(s))&(locDeferred-1) } func (l Loc) String() string { - l &^= locTypeOnly + l &^= locFlags if l == 0 || l >= locText { return locTable.name(uint32(l &^ locText)) } @@ -161,7 +164,7 @@ func (l Loc) String() string { // AppendJSON appends l as a JSON string. func (l Loc) AppendJSON(b []byte) []byte { - l &^= locTypeOnly + l &^= locFlags if l == 0 || l >= locText { return AppendJSONString(b, l.String()) } @@ -172,9 +175,9 @@ func (l Loc) AppendJSON(b []byte) []byte { func (l Loc) MarshalText() ([]byte, error) { return []byte(l.String()), nil } -// Keeps bit 30: "type_only" may come first. +// Keeps the flag bits: "type_only" may come first. func (l *Loc) UnmarshalText(b []byte) error { - *l = *l&locTypeOnly | LocOf(string(b)) + *l = *l&locFlags | LocOf(string(b)) return nil } diff --git a/internal/model/code_test.go b/internal/model/code_test.go index b005e22..c1f2e5d 100644 --- a/internal/model/code_test.go +++ b/internal/model/code_test.go @@ -62,7 +62,7 @@ func TestEdgeCodes(t *testing.T) { func TestLocAndFile(t *testing.T) { // Every text comes back as it went in, and equal texts have equal codes. - texts := []string{"", "L1", "L0", "L42", "L1073741821", "L1073741822", "L1073741823", "L2147483646", "L2147483647", "L99999999999", "L01", "L-1", "L+1", "L", "l5", "L5 ", "12", "L1é", "a\x00\""} + texts := []string{"", "L1", "L0", "L42", "L536870909", "L536870910", "L536870911", "L2147483646", "L2147483647", "L99999999999", "L01", "L-1", "L+1", "L", "l5", "L5 ", "12", "L1é", "a\x00\""} seen := map[Loc]string{} for _, s := range texts { l := LocOf(s) @@ -81,17 +81,18 @@ func TestLocAndFile(t *testing.T) { if LocOf("") != 0 || FileOf("") != 0 || Line(7) != LocOf("L7") || Line(-3).String() != "L-3" || Line(1<<31).String() != "L2147483648" { t.Fatal("zero values or Line disagree with LocOf") } - if LocOf("L7") >= locTypeOnly || LocOf("L1073741822") >= locTypeOnly || LocOf("L1073741823") < locText || LocOf("L01") < locText { + if LocOf("L7") >= locDeferred || LocOf("L536870910") >= locDeferred || LocOf("L536870911") < locText || LocOf("L01") < locText { t.Fatal("a line is not held as a number, or a text is") } - // Around the last line held as a number, with the type-only bit and without. - for _, n := range []int{0, 1, 1<<30 - 3, 1<<30 - 2, 1<<30 - 1, 1<<31 - 2} { + // Around the last line held as a number, with the flag bits and without. + for _, n := range []int{0, 1, 1<<29 - 3, 1<<29 - 2, 1<<29 - 1, 1<<31 - 2} { s := "L" + strconv.Itoa(n) l := Line(n) want, _ := json.Marshal(s) e := Edge{SourceLocation: l} e.SetTypeOnly(true) - if l != LocOf(s) || l&locTypeOnly != 0 || (l >= locText) != (n > 1<<30-2) || e.Loc() != l || e.SourceLocation == l { + e.SetDeferred(true) + if l != LocOf(s) || l&locFlags != 0 || (l >= locText) != (n > 1<<29-2) || e.Loc() != l || e.SourceLocation == l { t.Errorf("line %d: Loc %#x, with the bit %#x", n, l, e.SourceLocation) } for _, l := range []Loc{l, e.SourceLocation} { @@ -101,7 +102,8 @@ func TestLocAndFile(t *testing.T) { } } e.SetTypeOnly(false) - if e.SourceLocation != l || e.TypeOnly() { + e.SetDeferred(false) + if e.SourceLocation != l || e.TypeOnly() || e.Deferred() { t.Errorf("line %d: %#x after the bit is cleared", n, e.SourceLocation) } } @@ -265,11 +267,13 @@ func TestEdgeLayout(t *testing.T) { {"Relation", unsafe.Offsetof(e.Relation), 32}, {"Confidence", unsafe.Offsetof(e.Confidence), 36}, {"TypeOnlyJSON", unsafe.Offsetof(e.TypeOnlyJSON), 40}, + {"DeferredJSON", unsafe.Offsetof(e.DeferredJSON), 40}, {"SourceFile", unsafe.Offsetof(e.SourceFile), 40}, {"SourceLocation", unsafe.Offsetof(e.SourceLocation), 44}, {"Weight", unsafe.Offsetof(e.Weight), 48}, {"ConfidenceScore", unsafe.Offsetof(e.ConfidenceScore), 56}, {"size of TypeOnlyJSON", unsafe.Sizeof(e.TypeOnlyJSON), 0}, + {"size of DeferredJSON", unsafe.Sizeof(e.DeferredJSON), 0}, } { if f.got != f.want { t.Errorf("%s: %d, want %d", f.name, f.got, f.want) @@ -284,8 +288,9 @@ func TestEdgeLayout(t *testing.T) { {Source: "e", Target: "f", Relation: RelImplements, Confidence: ConfExtracted}, } want[1].SetTypeOnly(true) + want[2].SetDeferred(true) in, err := json.Marshal(want) - if err != nil || strings.Count(string(in), "type_only") != 1 { + if err != nil || strings.Count(string(in), "type_only") != 1 || strings.Count(string(in), "deferred") != 1 { t.Fatalf("%s %v", in, err) } got := buf[1:1:4] diff --git a/internal/model/model.go b/internal/model/model.go index 30d9252..d28752f 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -35,15 +35,17 @@ type Edge struct { Confidence Confidence `json:"confidence"` // Zero size: where json reads "type_only". TypeOnlyJSON typeOnlyJSON `json:"type_only"` + // Zero size: where json reads "deferred". + DeferredJSON deferredJSON `json:"deferred"` SourceFile File `json:"source_file,omitempty"` - // Bit 30 is the type-only flag: read with Loc. + // Bits 30 and 29 are the type-only and deferred flags: read with Loc. SourceLocation Loc `json:"source_location,omitempty"` Weight float64 `json:"weight,omitempty"` ConfidenceScore float64 `json:"confidence_score,omitempty"` } // Loc returns the edge's source location. -func (e Edge) Loc() Loc { return e.SourceLocation &^ locTypeOnly } +func (e Edge) Loc() Loc { return e.SourceLocation &^ locFlags } // TypeOnly reports an import edge erased at compile time (TypeScript // `import type`), which cannot form a runtime import cycle. @@ -57,7 +59,19 @@ func (e *Edge) SetTypeOnly(on bool) { } } -// MarshalJSON writes the flag as "type_only". +// Deferred reports an import() edge: a real dependency that runs late, so +// it cannot form a static import cycle. +func (e Edge) Deferred() bool { return e.SourceLocation&locDeferred != 0 } + +// SetDeferred marks the edge deferred, or not. +func (e *Edge) SetDeferred(on bool) { + e.SourceLocation &^= locDeferred + if on { + e.SourceLocation |= locDeferred + } +} + +// MarshalJSON writes the flags as "type_only" and "deferred". func (e Edge) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Source string `json:"source"` @@ -65,11 +79,12 @@ func (e Edge) MarshalJSON() ([]byte, error) { Relation Relation `json:"relation"` Confidence Confidence `json:"confidence"` TypeOnly bool `json:"type_only,omitempty"` + Deferred bool `json:"deferred,omitempty"` SourceFile File `json:"source_file,omitempty"` SourceLocation Loc `json:"source_location,omitempty"` Weight float64 `json:"weight,omitempty"` ConfidenceScore float64 `json:"confidence_score,omitempty"` - }{e.Source, e.Target, e.Relation, e.Confidence, e.TypeOnly(), e.SourceFile, e.Loc(), e.Weight, e.ConfidenceScore}) + }{e.Source, e.Target, e.Relation, e.Confidence, e.TypeOnly(), e.Deferred(), e.SourceFile, e.Loc(), e.Weight, e.ConfidenceScore}) } // typeOnlyJSON is the type of Edge.TypeOnlyJSON only. An UnmarshalJSON on @@ -85,6 +100,18 @@ func (t *typeOnlyJSON) UnmarshalJSON(b []byte) error { return err } +// deferredJSON is the type of Edge.DeferredJSON only, like typeOnlyJSON. +type deferredJSON struct{} + +// UnmarshalJSON sets the flag of the Edge around t. +func (t *deferredJSON) UnmarshalJSON(b []byte) error { + e := (*Edge)(unsafe.Add(unsafe.Pointer(t), -int(unsafe.Offsetof(Edge{}.DeferredJSON)))) + on := e.Deferred() // a null leaves it + err := json.Unmarshal(b, &on) + e.SetDeferred(on) + return err +} + // Extraction is one extractor's output for one file. type Extraction struct { Nodes []Node `json:"nodes"` diff --git a/internal/query/affected.go b/internal/query/affected.go index c0ebfbb..00fbd38 100644 --- a/internal/query/affected.go +++ b/internal/query/affected.go @@ -10,7 +10,7 @@ import ( // depRelations are the edge relations that mean "source depends on target", so // reversing them finds the things impacted when the target changes. var depRelations = map[string]bool{ - "calls": true, "imports": true, "imports_from": true, + "calls": true, "imports": true, "imports_from": true, "dynamic_import": true, "references": true, "depends_on": true, "inherits_context": true, } diff --git a/internal/query/affected_test.go b/internal/query/affected_test.go index 2a6b379..69efb5b 100644 --- a/internal/query/affected_test.go +++ b/internal/query/affected_test.go @@ -146,6 +146,20 @@ func TestAffectedRelation(t *testing.T) { } } +// A dynamic import() is a dependency: changing the target reaches the importer. +func TestAffectedDynamicImport(t *testing.T) { + g := loadJSON(t, `{"directed":true,"multigraph":false,"graph":{}, +"nodes":[ + {"id":"lazy","label":"lazy.ts","file_type":"code","source_file":"lazy.ts","source_location":"L1"}, + {"id":"app","label":"app.ts","file_type":"code","source_file":"app.ts","source_location":"L1"} +], +"links":[{"source":"app","target":"lazy","relation":"dynamic_import","confidence":"EXTRACTED"}], +"hyperedges":[]}`) + if got := impacted(Affected(g, []string{"lazy.ts"}, AffectedOptions{})); !got["app"] { + t.Errorf("app.ts should be impacted through dynamic_import, got %v", got) + } +} + // A seed naming the same file in bare, "./"-prefixed and absolute form must // return the identical result — agents hold whichever form they already have. func TestAffectedSeedPathForms(t *testing.T) { From ccdd21397c61b20eeda8cdf80fc6cb987c9c703e Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Sat, 17 Oct 2026 03:13:01 -0700 Subject: [PATCH 04/14] Fix import() shapes and new X() binding from review --- internal/analyze/analyze.go | 4 +- internal/extract/extract.go | 23 ++--- internal/extract/javascript.go | 109 ++++++++++++++++---- internal/extract/jsts_imports_test.go | 139 ++++++++++++++++++++++++++ internal/extract/resolve.go | 58 +++++++---- 5 files changed, 281 insertions(+), 52 deletions(-) diff --git a/internal/analyze/analyze.go b/internal/analyze/analyze.go index ef55881..9339671 100644 --- a/internal/analyze/analyze.go +++ b/internal/analyze/analyze.go @@ -177,8 +177,8 @@ func ImportCycles(g *model.Graph, maxLen, topN int) []Cycle { raw := map[string][]string{} for _, e := range g.Edges() { // Type-only imports are erased at compile time, so they cannot form a - // runtime cycle (upstream find_import_cycles skips them too). A deferred - // import() runs late, so it closes no static cycle either. + // runtime cycle (upstream find_import_cycles skips them too). Deferred + // ones run late. if e.Relation != model.RelImportsFrom || e.TypeOnly() || e.Deferred() { continue } diff --git a/internal/extract/extract.go b/internal/extract/extract.go index d18a008..e7e576e 100644 --- a/internal/extract/extract.go +++ b/internal/extract/extract.go @@ -111,9 +111,7 @@ type Imp struct { Rel bool // Export marks a JS/TS `export ... from Spec`. Export bool `json:",omitempty"` - // Dyn marks a JS/TS `import(Spec)`: dynImport asks for a file-level - // dynamic_import, dynDeferred for a deferred imports_from from FileID, - // which is then the function holding the call. + // Dyn: JS/TS import(Spec), see dynImport. Dyn uint8 `json:",omitempty"` // Names are the names a Python `from Spec import ...` or Rust `use Spec::...` // binds; each may be a module. For JS/TS see jsImp: a name costs 16 bytes and @@ -244,11 +242,11 @@ func fileFromBytes(rel string, src []byte) Result { case ".go": return extractGo(rel, src) case ".js", ".jsx", ".mjs", ".cjs": - return extractJS(rel, src, tsjs.Language()) + return extractJSFile(rel, src, tsjs.Language()) case ".tsx": - return extractJS(rel, src, tstsx.Language()) + return extractJSFile(rel, src, tstsx.Language()) case ".ts", ".mts", ".cts": - return extractJS(rel, src, tsts.Language()) + return extractJSFile(rel, src, tsts.Language()) case ".tf", ".tfvars", ".hcl": return extractTerraform(rel, src) case ".json": @@ -360,6 +358,7 @@ type builder struct { rubyMethods map[string]bool jsBodies []jsBody // function bodies awaiting the JS call walk jsNested map[[2]string]string // parent id, name -> nested function id + jsLocalCls map[[2]string]bool // parent id, name of a class declared inside jsParent map[string]string // nested function id -> parent id jsLinked map[[2]string]bool // nested calls already linked jsNS []string // enclosing namespace names @@ -530,19 +529,15 @@ func (b *builder) impTyped(spec string, loc model.Loc, typeOnly bool) { } } +// Imp.Dyn values: a file's dynamic_import, a function's deferred import. const ( dynImport uint8 = iota + 1 dynDeferred ) -// impDynamic records `import(spec)`: the file depends on it late, and so does -// caller, the function holding the call, when there is one. -func (b *builder) impDynamic(spec string, loc model.Loc, caller string) { - if spec == "" { - return - } - b.res.Imps = append(b.res.Imps, Imp{FileID: b.fileID, File: b.file, Spec: spec, Loc: loc, Dyn: dynImport}) - if caller != "" { +// impDeferred records an import() inside caller, a function of the file. +func (b *builder) impDeferred(spec string, loc model.Loc, caller string) { + if spec != "" && caller != "" { b.res.Imps = append(b.res.Imps, Imp{FileID: caller, File: b.file, Spec: spec, Loc: loc, Dyn: dynDeferred}) } } diff --git a/internal/extract/javascript.go b/internal/extract/javascript.go index ea8464d..048b48e 100644 --- a/internal/extract/javascript.go +++ b/internal/extract/javascript.go @@ -82,6 +82,56 @@ func extractJS(rel string, src []byte, langPtr *gositter.Language) Result { return b.res } +// extractJSFile adds dynamic_import edges, found by regex as upstream does. +func extractJSFile(rel string, src []byte, langPtr *gositter.Language) Result { + res := extractJS(rel, src, langPtr) + fileID := idutil.MakeID(rel) + isWord := func(c byte) bool { + return c == '_' || c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= 0x80 + } + skipSpace := func(i int) int { + for i < len(src) && strings.IndexByte(" \t\n\r\f\v", src[i]) >= 0 { + i++ + } + return i + } + for i := 0; ; { + j := bytes.Index(src[i:], []byte("import")) + if j < 0 { + break + } + at := i + j + i = at + len("import") + if at > 0 && isWord(src[at-1]) { + continue + } + k := skipSpace(i) + if k >= len(src) || src[k] != '(' { + continue + } + k = skipSpace(k + 1) + if k >= len(src) || src[k] != '\'' && src[k] != '"' && src[k] != '`' { + continue + } + q := src[k] + end := k + 1 + for end < len(src) && src[end] != q && src[end] != '\n' && (q != '`' || src[end] != '$') { + end++ + } + if end == k+1 || end >= len(src) || src[end] != q { + continue + } + if c := skipSpace(end + 1); c >= len(src) || src[c] != ')' { + continue + } + if bytes.Contains(src[bytes.LastIndexByte(src[:at], '\n')+1:at], []byte("//")) { + continue + } + res.Imps = append(res.Imps, Imp{FileID: fileID, File: rel, Spec: string(src[k+1 : end]), Loc: model.Line(lineOf(src, at)), Dyn: dynImport}) + } + return res +} + // jsExportOrphans gives every exported declarator that has no node yet a // node with no edge, as upstream's export facts do. Any export statement // counts, nested or under a parse error. A const or let with an identifier @@ -209,8 +259,8 @@ func (b *builder) jsChildren(n *ts.Node, src []byte, owner string) { } } -// jsCallImport records `require("m")` and dynamic `import("m")` as imports; -// caller is the function holding the call. A computed spec names no file. +// jsCallImport records `require("m")` and `import("m")` as imports; caller is +// the function holding the call. A computed spec names no file. func (b *builder) jsCallImport(n *ts.Node, src []byte, caller string) { if n.Kind() != "call_expression" { return @@ -219,23 +269,37 @@ func (b *builder) jsCallImport(n *ts.Node, src []byte, caller string) { if fn == nil || args == nil || args.NamedChildCount() == 0 { return } - a := args.NamedChild(0) + one := args.NamedChildCount() == 1 && args.NamedChild(0).Kind() == "string" switch { case fn.Kind() == "identifier" && fn.Utf8Text(src) == "require": - if args.NamedChildCount() == 1 && a.Kind() == "string" { - b.imp(unquote(a.Utf8Text(src)), line(n)) + if one { + b.imp(unquote(args.NamedChild(0).Utf8Text(src)), line(n)) } case fn.Kind() == "import": - switch a.Kind() { + // import x = require() recovers as a call + if strings.TrimSpace(string(src[fn.EndByte():args.StartByte()])) != "" && one { + b.imp(unquote(args.NamedChild(0).Utf8Text(src)), line(n)) + } + b.impDeferred(jsImportSpec(args, src), line(n), caller) + } +} + +// jsImportSpec is the first literal argument of an import(), "" if computed. +func jsImportSpec(args *ts.Node, src []byte) string { + for i := uint(0); i < args.NamedChildCount(); i++ { + switch a := args.NamedChild(i); a.Kind() { case "string": - b.impDynamic(unquote(a.Utf8Text(src)), line(n), caller) + return unquote(a.Utf8Text(src)) case "template_string": - // a substitution makes the spec computed - if spec := strings.Trim(a.Utf8Text(src), "`"); !strings.Contains(spec, "$") { - b.impDynamic(spec, line(n), caller) + for j := uint(0); j < a.NamedChildCount(); j++ { + if a.NamedChild(j).Kind() == "template_substitution" { + return "" + } } + return strings.Trim(a.Utf8Text(src), "`") } } + return "" } // jsTypeOnlyImport reports whether an import or re-export statement is a @@ -1095,6 +1159,12 @@ func (b *builder) jsNestedFuncs(n *ts.Node, parent string, src []byte) { b.jsScope(parent, name, id) } b.jsFnBody(c.ChildByFieldName("body"), id, src) + case "class_declaration": + if b.jsLocalCls == nil { + b.jsLocalCls = map[[2]string]bool{} + } + b.jsLocalCls[[2]string{parent, fieldText(c, "name", src)}] = true + b.jsNestedFuncs(c, parent, src) case "method_definition": b.jsCalls(c.ChildByFieldName("body"), "") b.jsNestedFuncs(c, parent, src) @@ -1169,7 +1239,7 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { if isNew { fn = c.ChildByFieldName("constructor") } - // A file-level `new` makes no edge upstream. + // no edge upstream if fn == nil || callerID == "" || isNew && callerID == b.fileID { return } @@ -1182,18 +1252,23 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { switch fn.Kind() { case "identifier": name := fn.Utf8Text(src) + for s := callerID; s != ""; s = b.jsParent[s] { + if isNew && b.jsLocalCls[[2]string{s, name}] { + return + } + if id, ok := b.jsNested[[2]string{s, name}]; ok { + if !isNew { + b.jsNestedCall(callerID, id, line(c)) + } + return + } + } if isNew { if !jsNewBuiltins[name] { b.callRecv(callerID, name, jsNewRecv, line(c)) } return } - for s := callerID; s != ""; s = b.jsParent[s] { - if id, ok := b.jsNested[[2]string{s, name}]; ok { - b.jsNestedCall(callerID, id, line(c)) - return - } - } // Innermost namespace first; a name in none is a plain global call. for k := len(body.ns); k > 0; k-- { id := idutil.MakeID(append(append([]string{b.stem, "ns"}, body.ns[:k]...), name)...) diff --git a/internal/extract/jsts_imports_test.go b/internal/extract/jsts_imports_test.go index bc769d7..7f3dadd 100644 --- a/internal/extract/jsts_imports_test.go +++ b/internal/extract/jsts_imports_test.go @@ -254,3 +254,142 @@ func TestSFCDynamicImport(t *testing.T) { "Lay.astro:Lay.astro imports_from a.ts:a.ts", }) } + +// Only a lone literal in `import(` makes a file dynamic_import; a recovered +// call, a comment argument or a `$` template still defers. +func TestJSDynamicImportShapes(t *testing.T) { + srcs := [][2]string{ + {"main.js", "import a = require(\"./a\");\nfunction f() { import c = require(\"./c\") }\nimport.defer(\"./e\");\n"}, + {"m.ts", `export function x() { return import("./a", { with: { type: "json" } }); } +export const top = import("./b", { with: {} }); +export function w() { return import(/* chunk */ './c'); } +export function d() { return import(` + "`./e$f`" + `); } +`}, + } + for _, f := range []string{"a", "b", "c", "e"} { + srcs = append(srcs, [2]string{f + ".ts", "export const " + f + " = 1;\n"}) + } + got := edgeSet(t, srcs, "imports_from", "dynamic_import") + checkEdges(t, got, []string{ + "main.js:f() imports_from c.ts:c.ts deferred", + "m.ts:x() imports_from a.ts:a.ts deferred", + "m.ts:w() imports_from c.ts:c.ts deferred", + "m.ts:d() imports_from :./e$f deferred", + "main.js:main.js imports_from a.ts:a.ts", // as on main + }, []string{ + "main.js:main.js dynamic_import a.ts:a.ts", + "main.js:main.js dynamic_import c.ts:c.ts", + "main.js:main.js dynamic_import e.ts:e.ts", + "m.ts:m.ts dynamic_import a.ts:a.ts", + "m.ts:m.ts dynamic_import b.ts:b.ts", + "m.ts:m.ts dynamic_import c.ts:c.ts", + }) +} + +// Upstream finds dynamic_import by regex: comments and strings count, a +// line comment or a longer name does not, and a static import hides it. +func TestJSDynamicImportRegex(t *testing.T) { + srcs := [][2]string{ + {"r.ts", `import { s } from './s'; +import type { ty } from './ty'; +export * from './re'; +/** @type {import("./a").A} */ +const str = "import('./b')"; +/* import('./c') */ +// import('./d') +const x = reimport('./e'), y = import('./s'), z = import('./ty'), w = import('./re'); +`}, + } + for _, f := range []string{"a", "b", "c", "d", "e", "s", "ty", "re"} { + srcs = append(srcs, [2]string{f + ".ts", "export const " + f + " = 1;\n"}) + } + got := edgeSet(t, srcs, "imports_from", "dynamic_import", "re_exports") + checkEdges(t, got, []string{ + "r.ts:r.ts dynamic_import a.ts:a.ts", + "r.ts:r.ts dynamic_import b.ts:b.ts", + "r.ts:r.ts dynamic_import c.ts:c.ts", + "r.ts:r.ts imports_from ty.ts:ty.ts", + }, []string{ + "r.ts:r.ts dynamic_import d.ts:d.ts", + "r.ts:r.ts dynamic_import e.ts:e.ts", + "r.ts:r.ts dynamic_import s.ts:s.ts", + "r.ts:r.ts dynamic_import ty.ts:ty.ts", + "r.ts:r.ts dynamic_import re.ts:re.ts", + }) +} + +// A component's dynamic_import is its regex alone: no template literal, no +// extra argument. The script still defers. +func TestSFCDynamicImportShapes(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"a.ts", "export const a = 1;\n"}, + {"b.ts", "export const b = 1;\n"}, + {"c.ts", "export const c = 1;\n"}, + {"R.vue", "\n"}, + }, "imports_from", "dynamic_import") + checkEdges(t, got, []string{ + "R.vue:load() imports_from a.ts:a.ts deferred", + "R.vue:load() imports_from b.ts:b.ts deferred", + "R.vue:f() imports_from c.ts:c.ts deferred", + }, []string{ + "R.vue:R.vue dynamic_import a.ts:a.ts", + "R.vue:R.vue dynamic_import b.ts:b.ts", + "R.vue:R.vue dynamic_import c.ts:c.ts", + }) +} + +// './self.js' in self.ts never falls through to the self.tsx sibling. +func TestJSImportJsSelfSkip(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"self.ts", "import { X } from './self.js';\nexport class X {}\nexport function s() { return new X(); }\n"}, + {"self.tsx", "export class Z {}\n"}, + }, "imports_from", "imports", "calls") + checkEdges(t, got, []string{"self.ts:s() calls self.ts:X"}, + []string{"self.ts:self.ts imports_from self.tsx:self.tsx", "self.ts:s() calls self.tsx:X"}) +} + +// `new X()` binds an imported class by its own name, once, outside its own +// members and any local declaration of the name. +func TestJSNewBinding(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"x.ts", "export class X {}\nexport class Y {}\nexport class Widget {}\nexport default class Thing {}\n"}, + {"item.ts", "export class Other {}\n"}, + {"barrel.ts", "export { Other as Renamed } from './item';\n"}, + {"b.ts", `import { X, Y, Widget as W } from './x'; +import Def from './x'; +import { Renamed } from './barrel'; +export class B { + retM(): X { return new X(1); } + noRetM() { return new Y(2); } + paramM(x: X) { return new X(3); } + static make() { return new B(); } +} +export function alias() { return new W(); } +export function dflt() { return new Def(); } +export function barrel() { return new Renamed(); } +export function shadowFn() { function Y() {} return new Y(); } +export function shadowCls() { class Y {} return new Y(); } +`}, + }, "calls") + checkEdges(t, got, []string{"b.ts:B.noRetM() calls x.ts:Y"}, []string{ + "b.ts:B.retM() calls x.ts:X", // a references edge is there already + "b.ts:B.paramM() calls x.ts:X", + "b.ts:B.make() calls b.ts:B", // upstream drops the owner pair + "b.ts:alias() calls x.ts:Widget", + "b.ts:dflt() calls x.ts:Thing", + "b.ts:barrel() calls item.ts:Other", + "b.ts:shadowFn() calls x.ts:Y", + "b.ts:shadowCls() calls x.ts:Y", + }) +} + +// A static import of the target hides the file's dynamic_import. +func TestJSDynamicImportBehindStaticImport(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"a.ts", "export const a = 1;\n"}, + {"s.ts", "import './a';\nexport function g() { return import('./a'); }\n"}, + {"late.ts", "export function g() { return import('./a'); }\nimport './a';\n"}, + }, "imports_from", "dynamic_import") + checkEdges(t, got, []string{"s.ts:g() imports_from a.ts:a.ts deferred", "late.ts:late.ts imports_from a.ts:a.ts"}, + []string{"s.ts:s.ts dynamic_import a.ts:a.ts", "late.ts:late.ts dynamic_import a.ts:a.ts"}) +} diff --git a/internal/extract/resolve.go b/internal/extract/resolve.go index a93ea14..c34eb07 100644 --- a/internal/extract/resolve.go +++ b/internal/extract/resolve.go @@ -187,7 +187,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E num uint32 } ids := map[string]tgtID{} - // An import() makes one edge of each kind per source and target. + // one edge per kind, source, target type dynKey struct { src, tgt string rel model.Relation @@ -204,19 +204,24 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E for b := range batches { nextImp := 0 for _, r := range b.results { - // A closure of the file that defers a target replaces its dynamic_import. - var fileDeferred map[string]bool + // a file-level import hides dynamic_import + var fileHas map[string]bool for k := range r.Imps { - if im := &r.Imps[k]; im.Dyn == dynDeferred && im.FileID == idutil.MakeID(im.File) { - if fileDeferred == nil { - fileDeferred = map[string]bool{} - } - for _, t := range b.targets[nextImp+k] { - fileDeferred[idutil.MakeID(t)] = true - } - if len(b.targets[nextImp+k]) == 0 { - fileDeferred[idutil.MakeID(im.Spec)] = true - } + if r.Imps[k].Dyn == dynImport { + fileHas = map[string]bool{} + break + } + } + for k := range r.Imps { + im := &r.Imps[k] + if fileHas == nil || im.Dyn == dynImport || im.Dyn == dynDeferred && im.FileID != idutil.MakeID(im.File) { + continue + } + for _, t := range b.targets[nextImp+k] { + fileHas[idutil.MakeID(t)] = true + } + if len(b.targets[nextImp+k]) == 0 { + fileHas[idutil.MakeID(im.Spec)] = true } } for k := range r.Imps { @@ -251,7 +256,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } for _, t := range tgts { key := dynKey{im.FileID, t, rel} - if dynSeen[key] || rel == model.RelDynamicImport && fileDeferred[t] { + if dynSeen[key] || rel == model.RelDynamicImport && fileHas[t] { continue } dynSeen[key] = true @@ -308,7 +313,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E targets := importTargets(im, corpus, goPkgs, goMods, goTargets) b.targets = append(b.targets, targets) external[[2]string{im.File, im.Spec}] = len(targets) == 0 - // Only a closure of the file imports for the file; a function's import() runs late. + // function import() runs late if len(targets) > 0 && (im.Dyn == 0 || im.Dyn == dynDeferred && im.FileID == idutil.MakeID(im.File)) { impTargets[im.File] = append(impTargets[im.File], targets) } @@ -607,6 +612,13 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E edges[n-1] = append(edges[n-1], e) } imported := importedSet{targets: impTargets} + refd := sync.OnceValue(func() map[[2]string]bool { // signature already links these + m := make(map[[2]string]bool, len(inherit)) + for _, e := range inherit { + m[[2]string{e.Source, e.Target}] = true + } + return m + }) pyMod := map[[2]string]string{} // file, module -> the corpus file of a Python import kept := map[string][]string{} // callee\x00qualifier -> the definitions it qualifies allQuals := map[string][]qualSet{} // callee -> quals of each id in its global list @@ -738,9 +750,13 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E if tgt != "" && langfamily.Cross(r.File, idFile[tgt]) { tgt = "" } - // A class of another file binds to `new` only if the file imports it. - if isNew && tgt != "" && idFile[tgt] != r.File && jsAlias[[2]string{r.File, c.Callee}] != tgt && !imported.of(r.File)[idFile[tgt]] { - tgt = "" + // new needs import evidence + if isNew && tgt != "" { + cross := idFile[tgt] != r.File + if tgt == owner[c.CallerID] || !slices.Contains(local[[2]string{idFile[tgt], c.Callee}], tgt) || + cross && (jsAlias[[2]string{r.File, c.Callee}] != tgt && !imported.of(r.File)[idFile[tgt]] || refd()[[2]string{c.CallerID, tgt}]) { + tgt = "" + } } // A call on an external module has no definition to bind, so it // lands on the module's import node. @@ -1608,7 +1624,11 @@ func importTargets(im Imp, corpus map[string]bool, goPkgs map[string][]string, g tos = tos[1:] } for _, to := range tos { - if t := resolveRelImport(im.File, strings.TrimSuffix(spec, ext)+to, corpus); t != "" && t != from { + t := resolveRelImport(im.File, strings.TrimSuffix(spec, ext)+to, corpus) + if t == from { + break // upstream stops here + } + if t != "" { return []string{t} } } From da9965550c8cc19fb1037fbf1c3176a24dd89ea4 Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Sat, 17 Oct 2026 03:40:47 -0700 Subject: [PATCH 05/14] Fix new X() and import() review defects Bind new X() in namespaces and by the class's own name, keep a nested-block class from shadowing an import, keep dynamic_import for externals behind a static import, and let a deferred import win its pair. --- internal/extract/javascript.go | 26 +++++++------ internal/extract/jsts_imports_test.go | 54 +++++++++++++++++++++++++++ internal/extract/resolve.go | 32 ++++++++++++++-- 3 files changed, 98 insertions(+), 14 deletions(-) diff --git a/internal/extract/javascript.go b/internal/extract/javascript.go index 048b48e..f27ea08 100644 --- a/internal/extract/javascript.go +++ b/internal/extract/javascript.go @@ -276,7 +276,7 @@ func (b *builder) jsCallImport(n *ts.Node, src []byte, caller string) { b.imp(unquote(args.NamedChild(0).Utf8Text(src)), line(n)) } case fn.Kind() == "import": - // import x = require() recovers as a call + // TS import-equals parses as a call if strings.TrimSpace(string(src[fn.EndByte():args.StartByte()])) != "" && one { b.imp(unquote(args.NamedChild(0).Utf8Text(src)), line(n)) } @@ -1131,6 +1131,15 @@ type jsBody struct { // jsFnBody registers a function body and the functions declared inside it. func (b *builder) jsFnBody(body *ts.Node, callerID string, src []byte) { b.jsCalls(body, callerID) + // only a class of the body itself shadows an imported one + for i := uint(0); body != nil && i < body.ChildCount(); i++ { + if c := body.Child(i); c.Kind() == "class_declaration" { + if b.jsLocalCls == nil { + b.jsLocalCls = map[[2]string]bool{} + } + b.jsLocalCls[[2]string{callerID, fieldText(c, "name", src)}] = true + } + } b.jsNestedFuncs(body, callerID, src) } @@ -1159,12 +1168,6 @@ func (b *builder) jsNestedFuncs(n *ts.Node, parent string, src []byte) { b.jsScope(parent, name, id) } b.jsFnBody(c.ChildByFieldName("body"), id, src) - case "class_declaration": - if b.jsLocalCls == nil { - b.jsLocalCls = map[[2]string]bool{} - } - b.jsLocalCls[[2]string{parent, fieldText(c, "name", src)}] = true - b.jsNestedFuncs(c, parent, src) case "method_definition": b.jsCalls(c.ChildByFieldName("body"), "") b.jsNestedFuncs(c, parent, src) @@ -1263,10 +1266,7 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { return } } - if isNew { - if !jsNewBuiltins[name] { - b.callRecv(callerID, name, jsNewRecv, line(c)) - } + if isNew && jsNewBuiltins[name] { return } // Innermost namespace first; a name in none is a plain global call. @@ -1277,6 +1277,10 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { return } } + if isNew { + b.callRecv(callerID, name, jsNewRecv, line(c)) + return + } // The file's contains edge to its own function hides this call upstream. if callerID == b.fileID && b.jsFileOwns(name) { return diff --git a/internal/extract/jsts_imports_test.go b/internal/extract/jsts_imports_test.go index 7f3dadd..97dc48a 100644 --- a/internal/extract/jsts_imports_test.go +++ b/internal/extract/jsts_imports_test.go @@ -383,6 +383,60 @@ export function shadowCls() { class Y {} return new Y(); } }) } +// A namespace class binds like a bare call, a nested block's class shadows +// nothing, and an aliased import falls back to the class of that name. +func TestJSNewScopes(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"ns.ts", `namespace Foo.Bar { class Greeter {} function foo() { return new Greeter(); } } +module m1 { class C2 {} export function f11() { return new C2(); } } +namespace Solo { export class Box {} export function mk() { return new Box(); } } +`}, + {"m.ts", "export class Widget {}\n"}, + {"u.ts", `import { Widget } from './m'; +export function sh3() { if (true) { class Widget {} return new Widget(); } } +export function sh5() { { class Widget {} } return new Widget(); } +export function sh6() { const g = () => { class Widget {} return 1; }; return new Widget(); } +export function own() { class Widget {} return new Widget(); } +`}, + {"lib.ts", "export class ChannelClient {}\nexport class IPCClient {}\n"}, + {"use.ts", "import { ChannelClient as IPCClient } from './lib';\nexport function mk() { return new IPCClient(); }\n"}, + {"st/a.ts", "export class Store {}\n"}, + {"st/b.ts", "export class Store {}\n"}, + {"two.ts", "import { Store as S } from './st/a';\nimport './st/b';\nexport function b2() { return new Store(); }\n"}, + }, "calls") + checkEdges(t, got, []string{ + "ns.ts:foo() calls ns.ts:Greeter", + "ns.ts:f11() calls ns.ts:C2", + "ns.ts:mk() calls ns.ts:Box", + "u.ts:sh3() calls m.ts:Widget", + "u.ts:sh5() calls m.ts:Widget", + "u.ts:sh6() calls m.ts:Widget", + "use.ts:mk() calls lib.ts:IPCClient", + "two.ts:b2() calls st/a.ts:Store", + }, []string{"u.ts:own() calls m.ts:Widget", "use.ts:mk() calls lib.ts:ChannelClient"}) +} + +// An external module imported statically still gets the file's dynamic_import. +func TestJSDynamicImportExternalBehindStatic(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"a.ts", "import React from 'react';\nexport function f() { return import('react'); }\n"}, + }, "imports_from", "dynamic_import") + checkEdges(t, got, []string{"a.ts:a.ts dynamic_import :react", "a.ts:f() imports_from :react deferred"}, nil) +} + +// A file-level deferred import marks the static one of the pair, whatever the order. +func TestJSDeferredBeatsStaticEitherOrder(t *testing.T) { + b := [2]string{"b.js", "const a = require('./a');\n"} + for _, a := range []string{ + "describe('x', () => { import('./b'); });\nconst b = require('./b');\n", + "const b = require('./b');\ndescribe('x', () => { import('./b'); });\n", + "import './b';\ndescribe('x', () => { import('./b'); });\n", + } { + got := edgeSet(t, [][2]string{{"a.js", a}, b}, "imports_from") + checkEdges(t, got, []string{"a.js:a.js imports_from b.js:b.js deferred"}, []string{"a.js:a.js imports_from b.js:b.js"}) + } +} + // A static import of the target hides the file's dynamic_import. func TestJSDynamicImportBehindStaticImport(t *testing.T) { got := edgeSet(t, [][2]string{ diff --git a/internal/extract/resolve.go b/internal/extract/resolve.go index c34eb07..4679d17 100644 --- a/internal/extract/resolve.go +++ b/internal/extract/resolve.go @@ -187,7 +187,6 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E num uint32 } ids := map[string]tgtID{} - // one edge per kind, source, target type dynKey struct { src, tgt string rel model.Relation @@ -220,7 +219,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E for _, t := range b.targets[nextImp+k] { fileHas[idutil.MakeID(t)] = true } - if len(b.targets[nextImp+k]) == 0 { + if len(b.targets[nextImp+k]) == 0 && im.Dyn == dynDeferred { fileHas[idutil.MakeID(im.Spec)] = true } } @@ -260,6 +259,13 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E continue } dynSeen[key] = true + // upstream lets deferred win a static pair + if tn, ok := tgtNum[t]; ok { + if i, ok := impEdge[uint64(src)<<32|uint64(tn)]; ok && im.Dyn == dynDeferred { + impEdges[i/impPart][i%impPart].deferred = true + continue + } + } add(impRec{imp: im, tgt: t, rel: rel, external: external, deferred: im.Dyn == dynDeferred}) } continue @@ -274,6 +280,9 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } ids[target] = t } + if dynSeen[dynKey{im.FileID, t.id, model.RelImportsFrom}] { + continue + } ekey := uint64(src)<<32 | uint64(t.num) if i, ok := impEdge[ekey]; ok { if !im.TypeOnly { @@ -625,6 +634,16 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E bareJS := map[string][]string{} // callee -> its global list without class members for _, r := range results { rf := model.FileOf(r.File) + var named map[string]bool // definitions a name import of r binds + symImp := func(id string) bool { + if named == nil { + named = map[string]bool{} + for _, d := range jm.bound(&r) { + named[d] = true + } + } + return named[id] + } for i, n := range r.Calls { c := r.call(n) if len(resolved) > 0 && resolved[callSite{c.CallerID, c.Callee, c.Loc}] { @@ -734,10 +753,17 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E // to disambiguate rather than guess. if c.Recv == "" { tgt = jsAlias[lkey] + // new binds by the class's own name + if isNew && !slices.Contains(local[[2]string{idFile[tgt], c.Callee}], tgt) { + tgt = "" + } } if tgt == "" && len(here) == 1 { tgt = here[0] } + if tgt == "" && isNew { + tgt = unique(all, symImp) + } if tgt == "" { tgt = disambiguate(all, r.File, idFile, &imported) } @@ -754,7 +780,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E if isNew && tgt != "" { cross := idFile[tgt] != r.File if tgt == owner[c.CallerID] || !slices.Contains(local[[2]string{idFile[tgt], c.Callee}], tgt) || - cross && (jsAlias[[2]string{r.File, c.Callee}] != tgt && !imported.of(r.File)[idFile[tgt]] || refd()[[2]string{c.CallerID, tgt}]) { + cross && (!symImp(tgt) && !imported.of(r.File)[idFile[tgt]] || refd()[[2]string{c.CallerID, tgt}]) { tgt = "" } } From 5da8c1a63cb59a933c11f74bbb11ded595adcb32 Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Sat, 17 Oct 2026 05:35:50 -0700 Subject: [PATCH 06/14] Fix new X() scoping, ambiguity and import scan review defects Cache schema is 23 because csharp-parity and xfile-returns branches use 22. --- internal/cache/cache.go | 2 +- internal/extract/extract.go | 1 - internal/extract/javascript.go | 32 ++++++++------- internal/extract/jsts_imports_test.go | 58 ++++++++++++++++++++++++--- internal/extract/resolve.go | 20 ++++----- 5 files changed, 81 insertions(+), 32 deletions(-) diff --git a/internal/cache/cache.go b/internal/cache/cache.go index ab92290..4aab91c 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -103,7 +103,7 @@ type StatIndex map[string]StatEntry // schema is the on-disk cache layout revision. Bump it when the shape of Entry // or StatEntry changes so older files are discarded rather than misread. -const schema = 22 +const schema = 23 // Stamp is the cache identity for a binary: its version plus the on-disk schema // revision. A cache file written under a different stamp is discarded, so an diff --git a/internal/extract/extract.go b/internal/extract/extract.go index e7e576e..833dc0d 100644 --- a/internal/extract/extract.go +++ b/internal/extract/extract.go @@ -358,7 +358,6 @@ type builder struct { rubyMethods map[string]bool jsBodies []jsBody // function bodies awaiting the JS call walk jsNested map[[2]string]string // parent id, name -> nested function id - jsLocalCls map[[2]string]bool // parent id, name of a class declared inside jsParent map[string]string // nested function id -> parent id jsLinked map[[2]string]bool // nested calls already linked jsNS []string // enclosing namespace names diff --git a/internal/extract/javascript.go b/internal/extract/javascript.go index f27ea08..97ed013 100644 --- a/internal/extract/javascript.go +++ b/internal/extract/javascript.go @@ -95,6 +95,7 @@ func extractJSFile(rel string, src []byte, langPtr *gositter.Language) Result { } return i } + line, prev, slash := 1, 0, false // slash: a // opens the current line for i := 0; ; { j := bytes.Index(src[i:], []byte("import")) if j < 0 { @@ -124,10 +125,18 @@ func extractJSFile(rel string, src []byte, langPtr *gositter.Language) Result { if c := skipSpace(end + 1); c >= len(src) || src[c] != ')' { continue } - if bytes.Contains(src[bytes.LastIndexByte(src[:at], '\n')+1:at], []byte("//")) { + // incremental, keeps it linear + seg := src[prev:at] + prev = at + if n := bytes.Count(seg, []byte{'\n'}); n > 0 { + line, slash = line+n, false + seg = seg[bytes.LastIndexByte(seg, '\n')+1:] + } + slash = slash || bytes.Contains(seg, []byte("//")) + if slash { continue } - res.Imps = append(res.Imps, Imp{FileID: fileID, File: rel, Spec: string(src[k+1 : end]), Loc: model.Line(lineOf(src, at)), Dyn: dynImport}) + res.Imps = append(res.Imps, Imp{FileID: fileID, File: rel, Spec: string(src[k+1 : end]), Loc: model.Line(line), Dyn: dynImport}) } return res } @@ -1131,15 +1140,6 @@ type jsBody struct { // jsFnBody registers a function body and the functions declared inside it. func (b *builder) jsFnBody(body *ts.Node, callerID string, src []byte) { b.jsCalls(body, callerID) - // only a class of the body itself shadows an imported one - for i := uint(0); body != nil && i < body.ChildCount(); i++ { - if c := body.Child(i); c.Kind() == "class_declaration" { - if b.jsLocalCls == nil { - b.jsLocalCls = map[[2]string]bool{} - } - b.jsLocalCls[[2]string{callerID, fieldText(c, "name", src)}] = true - } - } b.jsNestedFuncs(body, callerID, src) } @@ -1256,9 +1256,6 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { case "identifier": name := fn.Utf8Text(src) for s := callerID; s != ""; s = b.jsParent[s] { - if isNew && b.jsLocalCls[[2]string{s, name}] { - return - } if id, ok := b.jsNested[[2]string{s, name}]; ok { if !isNew { b.jsNestedCall(callerID, id, line(c)) @@ -1273,7 +1270,12 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { for k := len(body.ns); k > 0; k-- { id := idutil.MakeID(append(append([]string{b.stem, "ns"}, body.ns[:k]...), name)...) if b.seen[id] && !b.jsBare[id] { - b.jsNestedCall(callerID, id, line(c)) + // no self edge + if !isNew || !slices.ContainsFunc(b.res.Edges, func(e model.Edge) bool { + return e.Source == id && e.Target == callerID && e.Relation == model.RelContains + }) { + b.jsNestedCall(callerID, id, line(c)) + } return } } diff --git a/internal/extract/jsts_imports_test.go b/internal/extract/jsts_imports_test.go index 97dc48a..e4056ad 100644 --- a/internal/extract/jsts_imports_test.go +++ b/internal/extract/jsts_imports_test.go @@ -349,7 +349,7 @@ func TestJSImportJsSelfSkip(t *testing.T) { } // `new X()` binds an imported class by its own name, once, outside its own -// members and any local declaration of the name. +// members; a local class does not hide it, as upstream has no node for one. func TestJSNewBinding(t *testing.T) { got := edgeSet(t, [][2]string{ {"x.ts", "export class X {}\nexport class Y {}\nexport class Widget {}\nexport default class Thing {}\n"}, @@ -371,7 +371,7 @@ export function shadowFn() { function Y() {} return new Y(); } export function shadowCls() { class Y {} return new Y(); } `}, }, "calls") - checkEdges(t, got, []string{"b.ts:B.noRetM() calls x.ts:Y"}, []string{ + checkEdges(t, got, []string{"b.ts:B.noRetM() calls x.ts:Y", "b.ts:shadowCls() calls x.ts:Y"}, []string{ "b.ts:B.retM() calls x.ts:X", // a references edge is there already "b.ts:B.paramM() calls x.ts:X", "b.ts:B.make() calls b.ts:B", // upstream drops the owner pair @@ -379,17 +379,19 @@ export function shadowCls() { class Y {} return new Y(); } "b.ts:dflt() calls x.ts:Thing", "b.ts:barrel() calls item.ts:Other", "b.ts:shadowFn() calls x.ts:Y", - "b.ts:shadowCls() calls x.ts:Y", }) } -// A namespace class binds like a bare call, a nested block's class shadows -// nothing, and an aliased import falls back to the class of that name. +// A namespace class binds like a bare call, but not from its own member; a +// local class shadows nothing, and an aliased import falls back to the class +// of that name. func TestJSNewScopes(t *testing.T) { got := edgeSet(t, [][2]string{ {"ns.ts", `namespace Foo.Bar { class Greeter {} function foo() { return new Greeter(); } } module m1 { class C2 {} export function f11() { return new C2(); } } namespace Solo { export class Box {} export function mk() { return new Box(); } } +namespace N { export class A { m() { const f = () => new A(); } static s() { return new A(); } } export class C { n() { return new C(); } } export class D { g() { return new A(); } } } +module P.Q { export class E { static mk() { return new E(); } } } `}, {"m.ts", "export class Widget {}\n"}, {"u.ts", `import { Widget } from './m'; @@ -411,9 +413,14 @@ export function own() { class Widget {} return new Widget(); } "u.ts:sh3() calls m.ts:Widget", "u.ts:sh5() calls m.ts:Widget", "u.ts:sh6() calls m.ts:Widget", + "u.ts:own() calls m.ts:Widget", + "ns.ts:D.g() calls ns.ts:A", "use.ts:mk() calls lib.ts:IPCClient", "two.ts:b2() calls st/a.ts:Store", - }, []string{"u.ts:own() calls m.ts:Widget", "use.ts:mk() calls lib.ts:ChannelClient"}) + }, []string{ + "use.ts:mk() calls lib.ts:ChannelClient", + "ns.ts:A.m() calls ns.ts:A", "ns.ts:A.s() calls ns.ts:A", "ns.ts:C.n() calls ns.ts:C", "ns.ts:E.mk() calls ns.ts:E", + }) } // An external module imported statically still gets the file's dynamic_import. @@ -447,3 +454,42 @@ func TestJSDynamicImportBehindStaticImport(t *testing.T) { checkEdges(t, got, []string{"s.ts:g() imports_from a.ts:a.ts deferred", "late.ts:late.ts imports_from a.ts:a.ts"}, []string{"s.ts:s.ts dynamic_import a.ts:a.ts", "late.ts:late.ts dynamic_import a.ts:a.ts"}) } + +// An ambiguous new X() binds only by a unique symbol import, then by a unique +// imported file; never by directory, and never past a second symbol import. +func TestJSNewAmbiguous(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"a.ts", "export class X {}\n"}, + {"sub/b.ts", "export class X {}\n"}, + {"c.ts", "import './a';\nimport './sub/b';\nexport function f() { return new X(); }\n"}, + {"d.ts", "import * as A from './a';\nimport * as B from './sub/b';\nexport function g() { return new X(); }\n"}, + {"sub/s.ts", "import '../a';\nimport './b';\nexport function k() { return new X(); }\n"}, + {"e.ts", "import './sub/b';\nexport function h() { return new X(); }\n"}, + {"st/a.ts", "export class Store {}\n"}, + {"st/b.ts", "export class Store {}\n"}, + {"u1.ts", "import { Store } from './st/a';\nimport { Store as S2 } from './st/b';\nexport function f1() { return new Store(); }\n"}, + {"u2.ts", "import { Store } from './st/a';\nimport type { Store as T2 } from './st/b';\nexport function g2() { return new Store(); }\n"}, + {"u3.ts", "import { Store } from './st/a';\nexport function h3() { return new Store(); }\n"}, + }, "calls") + checkEdges(t, got, []string{"e.ts:h() calls sub/b.ts:X", "u3.ts:h3() calls st/a.ts:Store"}, []string{ + "c.ts:f() calls a.ts:X", "c.ts:f() calls sub/b.ts:X", + "d.ts:g() calls a.ts:X", "d.ts:g() calls sub/b.ts:X", + "sub/s.ts:k() calls a.ts:X", "sub/s.ts:k() calls sub/b.ts:X", + "u1.ts:f1() calls st/a.ts:Store", "u1.ts:f1() calls st/b.ts:Store", + "u2.ts:g2() calls st/a.ts:Store", "u2.ts:g2() calls st/b.ts:Store", + }) +} + +// The regex pass tracks lines and `//` per line while it scans once. +func TestJSDynamicImportLines(t *testing.T) { + src := "import('./a');\n\nconst x = 1; import('./b'); // import('./c')\n// import('./d')\nf(import('./e'), import('./f'));\n" + var got []string + for _, im := range FileFromBytes("m.ts", []byte(src)).Imps { + if im.Dyn == dynImport { + got = append(got, im.Spec+"@"+im.Loc.String()) + } + } + if want := "./a@L1 ./b@L3 ./e@L5 ./f@L5"; strings.Join(got, " ") != want { + t.Errorf("got %v, want %s", got, want) + } +} diff --git a/internal/extract/resolve.go b/internal/extract/resolve.go index 4679d17..4754d0b 100644 --- a/internal/extract/resolve.go +++ b/internal/extract/resolve.go @@ -160,7 +160,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E type impRec struct { imp *Imp // source, file and location tgt string - rel model.Relation // 0 is imports_from, or imports if external + rel model.Relation // imports if external typeOnly, external bool deferred bool } @@ -259,7 +259,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E continue } dynSeen[key] = true - // upstream lets deferred win a static pair + // deferred wins, as upstream if tn, ok := tgtNum[t]; ok { if i, ok := impEdge[uint64(src)<<32|uint64(tn)]; ok && im.Dyn == dynDeferred { impEdges[i/impPart][i%impPart].deferred = true @@ -634,7 +634,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E bareJS := map[string][]string{} // callee -> its global list without class members for _, r := range results { rf := model.FileOf(r.File) - var named map[string]bool // definitions a name import of r binds + var named map[string]bool symImp := func(id string) bool { if named == nil { named = map[string]bool{} @@ -751,9 +751,9 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E // Two types in one file can each own a method of the same name; a // bare call then has no unambiguous local target, so fall through // to disambiguate rather than guess. - if c.Recv == "" { + if c.Recv == "" && !(isNew && len(all) > 1) { tgt = jsAlias[lkey] - // new binds by the class's own name + // new binds by class name if isNew && !slices.Contains(local[[2]string{idFile[tgt], c.Callee}], tgt) { tgt = "" } @@ -761,10 +761,12 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E if tgt == "" && len(here) == 1 { tgt = here[0] } - if tgt == "" && isNew { - tgt = unique(all, symImp) - } - if tgt == "" { + if tgt == "" && isNew && len(all) > 1 { + // upstream never breaks a tie by directory + if tgt = unique(all, symImp); tgt == "" { + tgt = unique(all, func(id string) bool { return imported.of(r.File)[idFile[id]] }) + } + } else if tgt == "" { tgt = disambiguate(all, r.File, idFile, &imported) } } From 432f9d38e540f0ccc17a5ebcccef0a94e064fd16 Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Sat, 17 Oct 2026 08:07:53 -0700 Subject: [PATCH 07/14] Fix import() absolute, new X() ambiguity, js self fallback and SFC edge review defects Drop import("/x") from dynamic_import as upstream does. Count namespaces, values and class members when a new X() is ambiguous. A ./x.js that falls back to its importer makes no edge and no external stub. Keep SFC edges in the graph build, as upstream's edge family map has no SFC entries. --- internal/analyze/analyze.go | 4 +- internal/extract/component.go | 3 ++ internal/extract/javascript.go | 3 ++ internal/extract/jsts_imports_test.go | 74 ++++++++++++++++++++++++-- internal/extract/resolve.go | 59 +++++++++++++++++--- internal/graph/build.go | 4 +- internal/graph/build_test.go | 15 ++++++ internal/langfamily/langfamily.go | 10 ++++ internal/langfamily/langfamily_test.go | 8 +++ 9 files changed, 168 insertions(+), 12 deletions(-) diff --git a/internal/analyze/analyze.go b/internal/analyze/analyze.go index 9339671..63260c3 100644 --- a/internal/analyze/analyze.go +++ b/internal/analyze/analyze.go @@ -177,8 +177,8 @@ func ImportCycles(g *model.Graph, maxLen, topN int) []Cycle { raw := map[string][]string{} for _, e := range g.Edges() { // Type-only imports are erased at compile time, so they cannot form a - // runtime cycle (upstream find_import_cycles skips them too). Deferred - // ones run late. + // runtime cycle (upstream find_import_cycles skips them too). + // Deferred imports run late. if e.Relation != model.RelImportsFrom || e.TypeOnly() || e.Deferred() { continue } diff --git a/internal/extract/component.go b/internal/extract/component.go index a4a0ccd..5c799c4 100644 --- a/internal/extract/component.go +++ b/internal/extract/component.go @@ -49,6 +49,9 @@ func extractComponent(rel string, src []byte, isAstro bool) Result { fileID := idutil.MakeID(rel) for _, m := range dynamicImportRE.FindAllSubmatchIndex(src, -1) { spec := string(src[m[2]:m[3]]) + if strings.HasPrefix(spec, "/") { // upstream drops an empty package name + continue + } if spec == "" { continue } diff --git a/internal/extract/javascript.go b/internal/extract/javascript.go index 97ed013..faeb914 100644 --- a/internal/extract/javascript.go +++ b/internal/extract/javascript.go @@ -136,6 +136,9 @@ func extractJSFile(rel string, src []byte, langPtr *gositter.Language) Result { if slash { continue } + if src[k+1] == '/' { // upstream drops an empty package name + continue + } res.Imps = append(res.Imps, Imp{FileID: fileID, File: rel, Spec: string(src[k+1 : end]), Loc: model.Line(line), Dyn: dynImport}) } return res diff --git a/internal/extract/jsts_imports_test.go b/internal/extract/jsts_imports_test.go index e4056ad..0acaffc 100644 --- a/internal/extract/jsts_imports_test.go +++ b/internal/extract/jsts_imports_test.go @@ -38,7 +38,7 @@ export function main() { Button(); render(); bothTs(); realJs(); jsxTs() } {"plain.js:plain.js", "imports_from", "Button.tsx:Button.tsx"}, {"App.vue:App.vue", "imports_from", "Button.tsx:Button.tsx"}, {"barrel.ts:barrel.ts", "re_exports", "Comp.tsx:Comp.tsx"}, - // bound by the alias, not the tie-break: render() is defined twice + // the alias binds, not a tie-break {"main.ts:main()", "calls", "Widget.tsx:render()"}, } { if !edges[want] { @@ -158,7 +158,7 @@ describe('x', () => { new Circle(); new Local(); }); "noeffect.ts:c() calls side.ts:Side", "builtin.ts:r() calls builtin.ts:Response", }) - // no edge for a builtin name, or for a new at the file level or in its closure + // no edge: builtin, file level or closure for k := range got { if strings.HasPrefix(k, "main.ts:mkMap") || strings.HasPrefix(k, "main.ts:main.ts calls") { t.Errorf("unexpected edge %q", k) @@ -228,7 +228,7 @@ func TestJSDynamicImportInTopLevelClosure(t *testing.T) { checkEdges(t, got, []string{"spec.ts:spec.ts imports_from a.ts:a.ts deferred"}, []string{"spec.ts:spec.ts dynamic_import a.ts:a.ts", "spec.ts:spec.ts imports_from a.ts:a.ts"}) - // the closure of the file wins over a function, for the same target + // closure beats function got = edgeSet(t, [][2]string{a, {"spec.ts", "function f() { import('./a'); }\ndescribe('x', () => { import('./a'); });\n"}}, "imports_from", "dynamic_import") checkEdges(t, got, []string{"spec.ts:spec.ts imports_from a.ts:a.ts deferred", "spec.ts:f() imports_from a.ts:a.ts deferred"}, []string{"spec.ts:spec.ts dynamic_import a.ts:a.ts"}) @@ -493,3 +493,71 @@ func TestJSDynamicImportLines(t *testing.T) { t.Errorf("got %v, want %s", got, want) } } + +// import("/x") names no package: only the function defers it. +func TestJSDynamicImportAbsolute(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"a.ts", "/** @type {typeof import(\"/some-mod\")} */\nconst top = import(\"/abs/top\");\nexport function f() { return import(\"/abs/inner\"); }\n"}, + {"R.vue", "\n"}, + }, "imports_from", "dynamic_import") + checkEdges(t, got, []string{"a.ts:f() imports_from :/abs/inner deferred", "R.vue:g() imports_from :/abs/vue deferred"}, []string{ + "a.ts:a.ts dynamic_import :/some-mod", "a.ts:a.ts dynamic_import :/abs/top", "a.ts:a.ts dynamic_import :/abs/inner", + "R.vue:R.vue dynamic_import :/abs/vue", + }) +} + +// A .js specifier that falls back to its own file is resolved, not external. +func TestJSImportJsSelfNotExternal(t *testing.T) { + srcs := [][2]string{ + {"self.ts", "import './self.js';\nexport async function s() { return import('./self.js'); }\n"}, + {"self.tsx", "export class Z {}\n"}, + {"me.ts", "export class M {}\n"}, + {"me.tsx", "import './me.jsx';\n"}, + } + var results []Result + var files []string + for _, s := range srcs { + results = append(results, FileFromBytes(s[0], []byte(s[1]))) + files = append(files, s[0]) + } + for _, n := range Resolve(results, files).Nodes { + if strings.Contains(n.Label, ".js") { + t.Errorf("external stub node %q", n.Label) + } + } + got := edgeSet(t, srcs, "imports", "imports_from", "dynamic_import") + checkEdges(t, got, nil, []string{ + "self.ts:self.ts imports :./self.js", "me.tsx:me.tsx imports :./me.jsx", "self.ts:self.ts imports_from self.tsx:self.tsx", + "self.ts:self.ts imports_from self.ts:self.ts", "me.tsx:me.tsx imports_from me.tsx:me.tsx", + "self.ts:self.ts dynamic_import self.ts:self.ts", "self.ts:s() imports_from self.ts:self.ts deferred", + }) +} + +// A component keeps an import and a call bound through it, whatever the language. +func TestSFCImportKeepsPythonCall(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"util.py", "def helper(): pass\n"}, + {"App.vue", "\n"}, + {"o.py", "def other(): pass\n"}, + }, "imports_from", "calls") + checkEdges(t, got, []string{"App.vue:App.vue imports_from util.py:util.py", "App.vue:run() calls util.py:helper()"}, + []string{"App.vue:run() calls o.py:other()"}) +} + +// Namespaces, values and class members of the same name make a new X() ambiguous. +func TestJSNewAmbiguousWithOtherNodes(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"s1.ts", "export class A {}\nexport class D {}\nexport class E {}\nexport class Order {}\nexport class User {}\nexport class Solo {}\nexport namespace M { export const z = 1; }\n"}, + {"t.ts", "export namespace A { export const x = 1; }\nexport const D = 1;\nexport class K { E() {} }\n"}, + {"api.ts", "export class Client { User() { return 1; } }\n"}, + {"m.ts", "export class M {}\n"}, + {"u.ts", "import * as S from './s1';\nimport * as T from './t';\nimport * as Q from './m';\nexport function f() { new A(); new D(); new E(); new M(); }\n"}, + {"w.ts", "import { A, D } from './s1';\nimport './t';\nexport function h() { new A(); new D(); new E(); }\n"}, + {"svc.ts", "import { Order as O } from './s1';\nimport { Client } from './api';\nexport function make() { new User(); new Order(); return new Client(); }\n"}, + {"solo.ts", "import { Solo } from './s1';\nexport function k() { return new Solo(); }\n"}, + }, "calls") + checkEdges(t, got, []string{"svc.ts:make() calls api.ts:Client", "solo.ts:k() calls s1.ts:Solo", "w.ts:h() calls s1.ts:A", "w.ts:h() calls s1.ts:D"}, []string{ + "u.ts:f() calls s1.ts:A", "u.ts:f() calls s1.ts:D", "u.ts:f() calls s1.ts:E", "u.ts:f() calls m.ts:M", "u.ts:f() calls s1.ts:M", + "w.ts:h() calls s1.ts:E", "svc.ts:make() calls s1.ts:User", + }) +} diff --git a/internal/extract/resolve.go b/internal/extract/resolve.go index 4754d0b..e1feeb2 100644 --- a/internal/extract/resolve.go +++ b/internal/extract/resolve.go @@ -232,6 +232,9 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } targets := b.targets[nextImp] nextImp++ + if jsSelfFallback(im, targets) { + continue + } if im.Dyn != 0 { rel := model.RelDynamicImport if im.Dyn == dynDeferred { @@ -597,6 +600,41 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } } + // Upstream indexes every node, not just defs. + newIDs, newFile := map[string][]string{}, map[string]string{} + newIndex := sync.OnceFunc(func() { + names := map[string]bool{} + for _, r := range results { + if k := slices.Index(r.CallText, jsNewRecv); k > 0 { + for _, n := range r.Calls { + if n.Recv == uint32(k) { + names[r.CallText[n.Callee]] = true + } + } + } + } + for _, r := range results { + if !jsLike(r.File) { + continue + } + // member labels carry the owner + for _, d := range r.Defs { + if _, ok := newFile[d.ID]; names[d.Name] && !ok { + newIDs[d.Name] = append(newIDs[d.Name], d.ID) + newFile[d.ID] = r.File + } + } + for _, n := range r.Nodes { + if _, ok := newFile[n.ID]; !ok && n.FileType != "rationale" { + if l := strings.TrimLeft(strings.Trim(n.Label, "()"), "."); names[l] { + newIDs[l] = append(newIDs[l], n.ID) + newFile[n.ID] = r.File + } + } + } + } + }) + // Calls: prefer a definition in the same file, else disambiguate among the // definitions sharing the called name (unique global, imported file, or same // package) rather than guessing. A member call binds on its receiver: self/this @@ -719,6 +757,10 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } all = ids } + if isNew { + newIndex() + all = newIDs[c.Callee] + } if c.Recv != "" { q := recvTail(c.Recv) ok := func(id string) bool { return q != "" && quals[id].has(q) } @@ -762,9 +804,9 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E tgt = here[0] } if tgt == "" && isNew && len(all) > 1 { - // upstream never breaks a tie by directory + // no directory tie-break upstream if tgt = unique(all, symImp); tgt == "" { - tgt = unique(all, func(id string) bool { return imported.of(r.File)[idFile[id]] }) + tgt = unique(all, func(id string) bool { return imported.of(r.File)[newFile[id]] }) } } else if tgt == "" { tgt = disambiguate(all, r.File, idFile, &imported) @@ -775,7 +817,8 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E // and a name match across a family boundary is a phantom edge, not a // real call. Unknown families (non-code, unrecognized ext) stay // permissive. - if tgt != "" && langfamily.Cross(r.File, idFile[tgt]) { + if tgt != "" && langfamily.Cross(r.File, idFile[tgt]) && + !(langfamily.Component(r.File) && c.Recv == "" && tgt == jsAlias[[2]string{r.File, c.Callee}]) { tgt = "" } // new needs import evidence @@ -1653,9 +1696,6 @@ func importTargets(im Imp, corpus map[string]bool, goPkgs map[string][]string, g } for _, to := range tos { t := resolveRelImport(im.File, strings.TrimSuffix(spec, ext)+to, corpus) - if t == from { - break // upstream stops here - } if t != "" { return []string{t} } @@ -1665,6 +1705,13 @@ func importTargets(im Imp, corpus map[string]bool, goPkgs map[string][]string, g return out } +// jsSelfFallback reports a './x.js' that fell back to the importing file +// itself, which upstream drops. +func jsSelfFallback(im *Imp, targets []string) bool { + ext := path.Ext(im.Spec) + return len(targets) == 1 && targets[0] == im.File && (ext == ".js" || ext == ".jsx") && path.Ext(im.File) != ext +} + // resolveRustPath maps a `crate::`, `self::` or `super::` path to the corpus // file of the deepest module it names (a/b.rs or a/b/mod.rs), dropping trailing // segments that are items rather than modules. Any other path is external. diff --git a/internal/graph/build.go b/internal/graph/build.go index f5f99d4..38b653e 100644 --- a/internal/graph/build.go +++ b/internal/graph/build.go @@ -84,7 +84,9 @@ func BuildParts(nodes [][]model.Node, parts [][]model.Edge) *model.Graph { if s == nil || t == nil { continue // dangling edge to an external/stdlib node — expected } - cross := func() bool { return langfamily.Cross(s.SourceFile, t.SourceFile) } + cross := func() bool { + return langfamily.Cross(s.SourceFile, t.SourceFile) && !langfamily.Component(s.SourceFile) && !langfamily.Component(t.SourceFile) + } switch e.Relation { case model.RelCalls: if e.Confidence == model.ConfInferred && cross() { diff --git a/internal/graph/build_test.go b/internal/graph/build_test.go index 508a2b4..49437af 100644 --- a/internal/graph/build_test.go +++ b/internal/graph/build_test.go @@ -106,3 +106,18 @@ func TestBuildFillsMissingAttributesFromSameFile(t *testing.T) { t.Errorf("same-file re-add lost attributes: %+v", *n) } } + +func TestBuildKeepsComponentEdges(t *testing.T) { + ext := model.Extraction{ + Nodes: []model.Node{ + {ID: "app", Label: "App.vue", FileType: "code", SourceFile: "App.vue"}, + {ID: "util", Label: "util.py", FileType: "code", SourceFile: "util.py"}, + }, + Edges: []model.Edge{ + {Source: "app", Target: "util", Relation: model.RelImportsFrom, Confidence: model.ConfExtracted}, + }, + } + if !Build(ext).HasEdge("app", "util") { + t.Error("component -> python import was dropped") + } +} diff --git a/internal/langfamily/langfamily.go b/internal/langfamily/langfamily.go index 03eee1c..a0691de 100644 --- a/internal/langfamily/langfamily.go +++ b/internal/langfamily/langfamily.go @@ -70,3 +70,13 @@ func Cross(a, b string) bool { fa, fb := Of(a), Of(b) return fa != "" && fb != "" && fa != fb } + +// Component reports a single-file component. Extractors treat it as jsts, but +// upstream's edge filter has no family for it, so its edges are never dropped. +func Component(sourceFile string) bool { + switch strings.ToLower(filepath.Ext(sourceFile)) { + case ".vue", ".svelte", ".astro", ".ejs": + return true + } + return false +} diff --git a/internal/langfamily/langfamily_test.go b/internal/langfamily/langfamily_test.go index 22f4ec7..916f9df 100644 --- a/internal/langfamily/langfamily_test.go +++ b/internal/langfamily/langfamily_test.go @@ -28,3 +28,11 @@ func TestCross(t *testing.T) { } } } + +func TestComponent(t *testing.T) { + for f, want := range map[string]bool{"App.vue": true, "P.SVELTE": true, "a.astro": true, "t.ejs": true, "a.ts": false, "": false} { + if Component(f) != want { + t.Errorf("Component(%q) = %v", f, !want) + } + } +} From 03f4dd89508ae6545c4ddca6e65ddde4e7bc21ea Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Tue, 20 Oct 2026 08:36:42 -0700 Subject: [PATCH 08/14] Fix nested import() signatures, masked type args, js self import() and comments Nested callable signatures and malformed generic type arguments no longer give a deferred edge. Self .js/.jsx import() keeps its edges. dynamic_import carries no location, as upstream. --- internal/extract/javascript.go | 74 +++++++++++++++++-- internal/extract/jsts_imports_test.go | 102 +++++++++++++++++++++++++- internal/extract/resolve.go | 11 ++- internal/model/code.go | 2 +- 4 files changed, 175 insertions(+), 14 deletions(-) diff --git a/internal/extract/javascript.go b/internal/extract/javascript.go index faeb914..87c765d 100644 --- a/internal/extract/javascript.go +++ b/internal/extract/javascript.go @@ -7,6 +7,7 @@ import ( tsjs "github.com/dobbo-ca/gositter/grammars/javascript" tstsx "github.com/dobbo-ca/gositter/grammars/tsx" tsts "github.com/dobbo-ca/gositter/grammars/typescript" + "path" "regexp" "slices" "strings" @@ -54,13 +55,13 @@ func extractJS(rel string, src []byte, langPtr *gositter.Language) Result { } next++ } - caller := "" + var body jsBody if len(open) > 0 { - caller = open[len(open)-1].callerID + body = open[len(open)-1] } - b.jsCallImport(c, src, caller) + b.jsCallImport(c, src, body.callerID, body) if len(open) > 0 { - b.jsCall(c, open[len(open)-1], src) + b.jsCall(c, body, src) } return true }) @@ -95,7 +96,7 @@ func extractJSFile(rel string, src []byte, langPtr *gositter.Language) Result { } return i } - line, prev, slash := 1, 0, false // slash: a // opens the current line + line, prev, slash := 1, 0, false // slash: // seen on this line for i := 0; ; { j := bytes.Index(src[i:], []byte("import")) if j < 0 { @@ -271,9 +272,63 @@ func (b *builder) jsChildren(n *ts.Node, src []byte, owner string) { } } +// jsInSignature reports whether n lies in the params or type of a nested +// callable, which upstream's closure walk skips. +func jsInSignature(n *ts.Node, body jsBody) bool { + for p := n; p != nil && (p.StartByte() != body.start || p.EndByte() != body.end); p = p.Parent() { + switch p.Kind() { + case "function_declaration", "generator_function_declaration", "method_definition": + return true + } + } + return false +} + +// jsMaskedImport reports an import() in the type arguments of a generic call +// that tree-sitter takes for `a < b > (c)`. Upstream blanks it before parsing. +func (b *builder) jsMaskedImport(n *ts.Node, src []byte) bool { + if e := path.Ext(b.file); e == ".js" || e == ".jsx" || e == ".mjs" || e == ".cjs" { + return false + } + var bin *ts.Node + for p, q := n, n.Parent(); q != nil; p, q = q, q.Parent() { + k := q.Kind() + if k == "binary_expression" { + r, op := q.ChildByFieldName("right"), q.ChildByFieldName("operator") + if r != nil && op != nil && op.Kind() == "<" && r.StartByte() == p.StartByte() && r.EndByte() == p.EndByte() { + bin = q + } + break + } + if k != "unary_expression" && k != "member_expression" && k != "subscript_expression" { + break + } + } + if bin == nil { + return false + } + end := n.EndByte() + for end < uint(len(src)) && src[end] != '>' && !strings.ContainsRune("();{}", rune(src[end])) { + end++ + } + if end >= uint(len(src)) || src[end] != '>' { + return false + } + end++ + // A real comparison parses clean; only a failed generic leaves an error here. + for a := bin; a.Parent() != nil; a = a.Parent() { + for _, ch := range a.Parent().Children() { + if ch.Kind() == "ERROR" && ch.StartByte() <= end+2 && ch.EndByte()+1 >= end { + return true + } + } + } + return false +} + // jsCallImport records `require("m")` and `import("m")` as imports; caller is // the function holding the call. A computed spec names no file. -func (b *builder) jsCallImport(n *ts.Node, src []byte, caller string) { +func (b *builder) jsCallImport(n *ts.Node, src []byte, caller string, body jsBody) { if n.Kind() != "call_expression" { return } @@ -292,6 +347,9 @@ func (b *builder) jsCallImport(n *ts.Node, src []byte, caller string) { if strings.TrimSpace(string(src[fn.EndByte():args.StartByte()])) != "" && one { b.imp(unquote(args.NamedChild(0).Utf8Text(src)), line(n)) } + if caller != "" && (jsInSignature(n, body) || b.jsMaskedImport(n, src)) { + caller = "" + } b.impDeferred(jsImportSpec(args, src), line(n), caller) } } @@ -1245,7 +1303,7 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { if isNew { fn = c.ChildByFieldName("constructor") } - // no edge upstream + // Upstream binds no module-scope new. if fn == nil || callerID == "" || isNew && callerID == b.fileID { return } @@ -1273,7 +1331,7 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { for k := len(body.ns); k > 0; k-- { id := idutil.MakeID(append(append([]string{b.stem, "ns"}, body.ns[:k]...), name)...) if b.seen[id] && !b.jsBare[id] { - // no self edge + // Own-member new X() makes no edge if !isNew || !slices.ContainsFunc(b.res.Edges, func(e model.Edge) bool { return e.Source == id && e.Target == callerID && e.Relation == model.RelContains }) { diff --git a/internal/extract/jsts_imports_test.go b/internal/extract/jsts_imports_test.go index 0acaffc..88e8c25 100644 --- a/internal/extract/jsts_imports_test.go +++ b/internal/extract/jsts_imports_test.go @@ -4,6 +4,8 @@ import ( "slices" "strings" "testing" + + "github.com/dobbo-ca/graphify-go/internal/model" ) // A .js specifier names a .ts or .tsx file; a real .js file wins. @@ -529,7 +531,85 @@ func TestJSImportJsSelfNotExternal(t *testing.T) { checkEdges(t, got, nil, []string{ "self.ts:self.ts imports :./self.js", "me.tsx:me.tsx imports :./me.jsx", "self.ts:self.ts imports_from self.tsx:self.tsx", "self.ts:self.ts imports_from self.ts:self.ts", "me.tsx:me.tsx imports_from me.tsx:me.tsx", - "self.ts:self.ts dynamic_import self.ts:self.ts", "self.ts:s() imports_from self.ts:self.ts deferred", + }) +} + +// Only the static './x.js' self import is dropped: import() of the file +// itself still gives the file's dynamic_import and the function's deferred edge. +func TestJSDynamicImportSelfFallback(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"self.ts", "export function s1() { return import('./self.js'); }\n"}, + {"tx.tsx", "export function s2() { return import('./tx.jsx'); }\n"}, + {"st.ts", "import './st.js';\nexport function s3() { return import('./st.js'); }\n"}, + }, "imports_from", "dynamic_import") + checkEdges(t, got, []string{ + "self.ts:self.ts dynamic_import self.ts:self.ts", "self.ts:s1() imports_from self.ts:self.ts deferred", + "tx.tsx:tx.tsx dynamic_import tx.tsx:tx.tsx", "tx.tsx:s2() imports_from tx.tsx:tx.tsx deferred", + "st.ts:st.ts dynamic_import st.ts:st.ts", + }, []string{"st.ts:st.ts imports_from st.ts:st.ts"}) +} + +// Upstream's closure walk skips the params and type of a nested callable, so +// an import() there gives the file its dynamic_import and no deferred edge. +func TestJSDynamicImportNestedSignature(t *testing.T) { + srcs := [][2]string{{"main.ts", `declare function setup(f: () => void): void; +declare function stub(o: any): void; +setup(() => { + stub({ get(m: import('./a.js').Ta) { return 1; } }); + stub({ get(): import('./c.js').Tc | undefined { return undefined; } }); + class K { m(): import('./d.js').Td | undefined { return undefined; } } + class L { constructor(p: import('./e.js').Te) {} } + function inner(x: import('./f.js').Tf) { return import('./g.js'); } + const g = (t: import('./b.js').Tb[]) => t; +}); +export function f() { + function nested(x = import('./h.js')) { return x; } +} +`}} + for _, f := range []string{"a", "b", "c", "d", "e", "f", "g", "h"} { + srcs = append(srcs, [2]string{f + ".ts", "export const " + f + " = 1;\n"}) + } + got := edgeSet(t, srcs, "imports_from", "dynamic_import") + checkEdges(t, got, []string{ + "main.ts:main.ts imports_from b.ts:b.ts deferred", // arrow params stay + "main.ts:inner() imports_from g.ts:g.ts deferred", // bodies stay + "main.ts:main.ts dynamic_import a.ts:a.ts", + "main.ts:main.ts dynamic_import c.ts:c.ts", + "main.ts:main.ts dynamic_import d.ts:d.ts", + "main.ts:main.ts dynamic_import e.ts:e.ts", + "main.ts:main.ts dynamic_import f.ts:f.ts", + "main.ts:main.ts dynamic_import h.ts:h.ts", + }, []string{ + "main.ts:main.ts imports_from a.ts:a.ts deferred", + "main.ts:main.ts imports_from c.ts:c.ts deferred", + "main.ts:main.ts imports_from d.ts:d.ts deferred", + "main.ts:main.ts imports_from e.ts:e.ts deferred", + "main.ts:main.ts imports_from f.ts:f.ts deferred", + "main.ts:f() imports_from h.ts:h.ts deferred", + }) +} + +// A generic call that tree-sitter misreads as a comparison has its import() +// type masked upstream, so only the regex pass sees it. +func TestJSDynamicImportMaskedTypeArgs(t *testing.T) { + srcs := [][2]string{{"s.ts", `declare function foo(...a: any[]): Promise; +declare function suite(n: string, f: () => void): void; +export function a1() { return foo(); } +export async function a2() { return await foo(1, ''); } +export function a3() { return foo(); } +export function a4() { return new Map(); } +suite("x", () => { foo(); }); +`}} + for _, f := range []string{"t1", "t2", "t3", "t4", "u"} { + srcs = append(srcs, [2]string{f + ".ts", "export const " + f + " = 1;\n"}) + } + got := edgeSet(t, srcs, "imports_from", "dynamic_import") + checkEdges(t, got, []string{ + "s.ts:s.ts dynamic_import t1.ts:t1.ts", "s.ts:s.ts dynamic_import t3.ts:t3.ts", "s.ts:s.ts dynamic_import u.ts:u.ts", + "s.ts:a2() imports_from t2.ts:t2.ts deferred", "s.ts:a4() imports_from t4.ts:t4.ts deferred", + }, []string{ + "s.ts:a1() imports_from t1.ts:t1.ts deferred", "s.ts:a3() imports_from t3.ts:t3.ts deferred", + "s.ts:s.ts imports_from u.ts:u.ts deferred", }) } @@ -561,3 +641,23 @@ func TestJSNewAmbiguousWithOtherNodes(t *testing.T) { "w.ts:h() calls s1.ts:E", "svc.ts:make() calls s1.ts:User", }) } + +// Upstream's dynamic_import carries no location. +func TestJSDynamicImportNoLocation(t *testing.T) { + results := []Result{ + FileFromBytes("a.ts", []byte("\nexport const x = import('./b');\n")), + FileFromBytes("b.ts", []byte("export const b = 1;\n")), + } + n := 0 + for _, e := range Resolve(results, []string{"a.ts", "b.ts"}).Edges { + if e.Relation == model.RelDynamicImport { + n++ + if e.SourceLocation != 0 { + t.Errorf("dynamic_import has location %v", e.SourceLocation) + } + } + } + if n != 1 { + t.Errorf("got %d dynamic_import edges, want 1", n) + } +} diff --git a/internal/extract/resolve.go b/internal/extract/resolve.go index e1feeb2..672ae2b 100644 --- a/internal/extract/resolve.go +++ b/internal/extract/resolve.go @@ -160,7 +160,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E type impRec struct { imp *Imp // source, file and location tgt string - rel model.Relation // imports if external + rel model.Relation // 0: imports_from, or imports typeOnly, external bool deferred bool } @@ -213,7 +213,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } for k := range r.Imps { im := &r.Imps[k] - if fileHas == nil || im.Dyn == dynImport || im.Dyn == dynDeferred && im.FileID != idutil.MakeID(im.File) { + if fileHas == nil || im.Dyn == dynImport || im.Dyn == dynDeferred && im.FileID != idutil.MakeID(im.File) || im.Dyn == 0 && jsSelfFallback(im, b.targets[nextImp+k]) { continue } for _, t := range b.targets[nextImp+k] { @@ -232,7 +232,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } targets := b.targets[nextImp] nextImp++ - if jsSelfFallback(im, targets) { + if im.Dyn == 0 && jsSelfFallback(im, targets) { continue } if im.Dyn != 0 { @@ -600,7 +600,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } } - // Upstream indexes every node, not just defs. + // Upstream indexes all nodes, not defs. newIDs, newFile := map[string][]string{}, map[string]string{} newIndex := sync.OnceFunc(func() { names := map[string]bool{} @@ -921,6 +921,9 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E Source: e.imp.FileID, Target: e.tgt, Relation: rel, Confidence: model.ConfExtracted, SourceFile: fc.of(e.imp.File), SourceLocation: e.imp.Loc, }) + if rel == model.RelDynamicImport { + part[len(part)-1].SourceLocation = 0 // upstream writes none + } part[len(part)-1].SetTypeOnly(e.typeOnly) part[len(part)-1].SetDeferred(e.deferred) } diff --git a/internal/model/code.go b/internal/model/code.go index 3281783..a3ef566 100644 --- a/internal/model/code.go +++ b/internal/model/code.go @@ -47,7 +47,7 @@ var ( ) // codeTable gives each string a number. Lookups by number take no lock. -// No cap: a code wraps past 2^30 strings (Loc), a table over 20 GB. +// No cap: a code wraps past 2^29 strings (Loc), a table over 10 GB. type codeTable struct { mu sync.RWMutex ids map[string]uint32 From 3552ed406c4f076aeb09c953f17f9cc67ad475a3 Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Tue, 20 Oct 2026 09:50:39 -0700 Subject: [PATCH 09/14] Fix round-6 JS/TS import() and new X() review defects Nested function bodies keep the caller, masked generics climb parens and tuples, file closures bind new X(), x.js swaps before extension probing, import() site wins a merge, spec spaces trimmed. --- internal/extract/javascript.go | 31 +++++++--- internal/extract/jsts_imports_test.go | 81 ++++++++++++++++++++++++++- internal/extract/resolve.go | 40 ++++++++----- 3 files changed, 128 insertions(+), 24 deletions(-) diff --git a/internal/extract/javascript.go b/internal/extract/javascript.go index 87c765d..c878e82 100644 --- a/internal/extract/javascript.go +++ b/internal/extract/javascript.go @@ -275,10 +275,14 @@ func (b *builder) jsChildren(n *ts.Node, src []byte, owner string) { // jsInSignature reports whether n lies in the params or type of a nested // callable, which upstream's closure walk skips. func jsInSignature(n *ts.Node, body jsBody) bool { - for p := n; p != nil && (p.StartByte() != body.start || p.EndByte() != body.end); p = p.Parent() { + for c, p := n, n; p != nil && (p.StartByte() != body.start || p.EndByte() != body.end); c, p = p, p.Parent() { switch p.Kind() { - case "function_declaration", "generator_function_declaration", "method_definition": + case "method_definition": return true + case "function_declaration", "generator_function_declaration": + if bd := p.ChildByFieldName("body"); bd == nil || bd.StartByte() != c.StartByte() || bd.EndByte() != c.EndByte() { + return true + } } } return false @@ -291,6 +295,7 @@ func (b *builder) jsMaskedImport(n *ts.Node, src []byte) bool { return false } var bin *ts.Node + parens := 0 for p, q := n, n.Parent(); q != nil; p, q = q, q.Parent() { k := q.Kind() if k == "binary_expression" { @@ -300,7 +305,10 @@ func (b *builder) jsMaskedImport(n *ts.Node, src []byte) bool { } break } - if k != "unary_expression" && k != "member_expression" && k != "subscript_expression" { + if k == "parenthesized_expression" { + parens++ + } + if k != "unary_expression" && k != "member_expression" && k != "subscript_expression" && k != "parenthesized_expression" && k != "array" { break } } @@ -308,14 +316,17 @@ func (b *builder) jsMaskedImport(n *ts.Node, src []byte) bool { return false } end := n.EndByte() - for end < uint(len(src)) && src[end] != '>' && !strings.ContainsRune("();{}", rune(src[end])) { + for end < uint(len(src)) && src[end] != '>' && (src[end] == ')' && parens > 0 || !strings.ContainsRune("();{}", rune(src[end]))) { + if src[end] == ')' { + parens-- + } end++ } if end >= uint(len(src)) || src[end] != '>' { return false } end++ - // A real comparison parses clean; only a failed generic leaves an error here. + // only failed generics leave ERROR for a := bin; a.Parent() != nil; a = a.Parent() { for _, ch := range a.Parent().Children() { if ch.Kind() == "ERROR" && ch.StartByte() <= end+2 && ch.EndByte()+1 >= end { @@ -359,7 +370,7 @@ func jsImportSpec(args *ts.Node, src []byte) string { for i := uint(0); i < args.NamedChildCount(); i++ { switch a := args.NamedChild(i); a.Kind() { case "string": - return unquote(a.Utf8Text(src)) + return strings.Trim(a.Utf8Text(src), "'\" ") case "template_string": for j := uint(0); j < a.NamedChildCount(); j++ { if a.NamedChild(j).Kind() == "template_substitution" { @@ -1303,8 +1314,7 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { if isNew { fn = c.ChildByFieldName("constructor") } - // Upstream binds no module-scope new. - if fn == nil || callerID == "" || isNew && callerID == b.fileID { + if fn == nil || callerID == "" { return } // A method body is a boundary: its calls are no caller's. @@ -1341,7 +1351,10 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { } } if isNew { - b.callRecv(callerID, name, jsNewRecv, line(c)) + // contains edge already links the pair + if callerID != b.fileID || !b.jsFileOwns(name) { + b.callRecv(callerID, name, jsNewRecv, line(c)) + } return } // The file's contains edge to its own function hides this call upstream. diff --git a/internal/extract/jsts_imports_test.go b/internal/extract/jsts_imports_test.go index 88e8c25..afe30bb 100644 --- a/internal/extract/jsts_imports_test.go +++ b/internal/extract/jsts_imports_test.go @@ -425,6 +425,74 @@ export function own() { class Widget {} return new Widget(); } }) } +// A new X() in a closure passed to a top-level call is the file's, unless an +// imports or contains edge already links the pair; module scope makes none. +func TestJSNewFileClosure(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"x/a.ts", "export class Foo {}\nexport class Bar {}\nexport function helper() {}\n"}, + {"t.ts", "import './x/a';\nsuite('s', () => { new Foo(); });\n"}, + {"u.ts", "import * as ns from './x/a';\nsuite('s', async () => { new Bar(); });\nnew Foo();\n"}, + {"v.ts", "import { helper } from './x/a';\nsuite(() => { new Foo(); new Bar(); });\n"}, + {"n.ts", "import { Foo } from './x/a';\nsuite('s', () => { new Foo(); });\n"}, + {"w.ts", "export class Loc {}\nsuite('s', () => { new Loc(); });\n"}, + }, "calls") + checkEdges(t, got, []string{ + "t.ts:t.ts calls x/a.ts:Foo", "u.ts:u.ts calls x/a.ts:Bar", "v.ts:v.ts calls x/a.ts:Foo", "v.ts:v.ts calls x/a.ts:Bar", + }, []string{"u.ts:u.ts calls x/a.ts:Foo", "n.ts:n.ts calls x/a.ts:Foo", "w.ts:w.ts calls w.ts:Loc"}) +} + +// './x.js' is x.ts before x.js.ts or x.js/index.ts, as upstream tries it. +func TestJSImportJsSwapFirst(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"lib/x.ts", "export class A {}\n"}, {"lib/x.js.ts", "export class A {}\n"}, + {"lib2/y.ts", "export class B {}\n"}, {"lib2/y.js/index.ts", "export class B {}\n"}, + {"m.ts", "import './lib/x.js';\nimport './lib2/y.js';\n"}, + }, "imports_from") + checkEdges(t, got, []string{"m.ts:m.ts imports_from lib/x.ts:x.ts", "m.ts:m.ts imports_from lib2/y.ts:y.ts"}, + []string{"m.ts:m.ts imports_from lib/x.js.ts:x.js.ts", "m.ts:m.ts imports_from lib2/y.js/index.ts:index.ts"}) +} + +// A file-level import() replaces the static edge's line, either order. +func TestJSDeferredMergeTakesImportSite(t *testing.T) { + srcs := [][2]string{ + {"s1.ts", "import {y1} from './y1';\nsetup(() => import('./y1'));\n"}, + {"s2.ts", "setup(() => import('./y2'));\nimport {y2} from './y2';\n"}, + {"s3.ts", "import type {y3} from './y3';\nsetup(() => import('./y3'));\n"}, + {"s4.ts", "setup(() => import('./y4'));\nimport type {y4} from './y4';\n"}, + } + for _, y := range []string{"y1", "y2", "y3", "y4"} { + srcs = append(srcs, [2]string{y + ".ts", "export const y = 1;\n"}) + } + var results []Result + var files []string + for _, s := range srcs { + results = append(results, FileFromBytes(s[0], []byte(s[1]))) + files = append(files, s[0]) + } + line := map[string]string{"s1.ts": "L2", "s2.ts": "L1", "s3.ts": "L2", "s4.ts": "L1"} + n := 0 + for _, e := range Resolve(results, files).Edges { + if e.Relation == model.RelImportsFrom && e.Deferred() { + if f := e.SourceFile.String(); line[f] != e.SourceLocation.String() { + t.Errorf("%s: location %s, want %s", f, e.SourceLocation, line[f]) + } + n++ + } + } + if n != 4 { + t.Errorf("got %d deferred edges, want 4", n) + } +} + +// import(' ./d ') trims spaces inside the quotes. +func TestJSDynamicImportSpaces(t *testing.T) { + got := edgeSet(t, [][2]string{ + {"a.ts", "export function f2() { return import(' ./d2 '); }\n"}, + {"d2.ts", "export const d = 1;\n"}, + }, "imports_from") + checkEdges(t, got, []string{"a.ts:f2() imports_from d2.ts:d2.ts deferred"}, nil) +} + // An external module imported statically still gets the file's dynamic_import. func TestJSDynamicImportExternalBehindStatic(t *testing.T) { got := edgeSet(t, [][2]string{ @@ -565,8 +633,9 @@ setup(() => { export function f() { function nested(x = import('./h.js')) { return x; } } +export let sys = (() => { function nf() { return import('./i1.js'); } const o = { om() { return import('./i2.js'); } }; class K { cm() { return import('./i3.js'); } } function* g() { yield import('./i4.js'); } })(); `}} - for _, f := range []string{"a", "b", "c", "d", "e", "f", "g", "h"} { + for _, f := range []string{"a", "b", "c", "d", "e", "f", "g", "h", "i1", "i2", "i3", "i4"} { srcs = append(srcs, [2]string{f + ".ts", "export const " + f + " = 1;\n"}) } got := edgeSet(t, srcs, "imports_from", "dynamic_import") @@ -579,7 +648,11 @@ export function f() { "main.ts:main.ts dynamic_import e.ts:e.ts", "main.ts:main.ts dynamic_import f.ts:f.ts", "main.ts:main.ts dynamic_import h.ts:h.ts", + "main.ts:sys imports_from i1.ts:i1.ts deferred", // var IIFE body stays + "main.ts:sys imports_from i4.ts:i4.ts deferred", }, []string{ + "main.ts:sys imports_from i2.ts:i2.ts deferred", + "main.ts:sys imports_from i3.ts:i3.ts deferred", "main.ts:main.ts imports_from a.ts:a.ts deferred", "main.ts:main.ts imports_from c.ts:c.ts deferred", "main.ts:main.ts imports_from d.ts:d.ts deferred", @@ -599,8 +672,11 @@ export async function a2() { return await foo(1, ''); } export function a3() { return foo(); } export function a4() { return new Map(); } suite("x", () => { foo(); }); +export function n1() { return foo<(typeof import('./a'))>(); } +export function n3() { return foo<[typeof import('./c')]>(); } +export function m12() { return foo<(typeof import('./l'))['vl']>(); } `}} - for _, f := range []string{"t1", "t2", "t3", "t4", "u"} { + for _, f := range []string{"t1", "t2", "t3", "t4", "u", "a", "c", "l"} { srcs = append(srcs, [2]string{f + ".ts", "export const " + f + " = 1;\n"}) } got := edgeSet(t, srcs, "imports_from", "dynamic_import") @@ -610,6 +686,7 @@ suite("x", () => { foo(); }); }, []string{ "s.ts:a1() imports_from t1.ts:t1.ts deferred", "s.ts:a3() imports_from t3.ts:t3.ts deferred", "s.ts:s.ts imports_from u.ts:u.ts deferred", + "s.ts:n1() imports_from a.ts:a.ts deferred", "s.ts:n3() imports_from c.ts:c.ts deferred", "s.ts:m12() imports_from l.ts:l.ts deferred", }) } diff --git a/internal/extract/resolve.go b/internal/extract/resolve.go index 672ae2b..b3a290c 100644 --- a/internal/extract/resolve.go +++ b/internal/extract/resolve.go @@ -265,7 +265,8 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E // deferred wins, as upstream if tn, ok := tgtNum[t]; ok { if i, ok := impEdge[uint64(src)<<32|uint64(tn)]; ok && im.Dyn == dynDeferred { - impEdges[i/impPart][i%impPart].deferred = true + e := &impEdges[i/impPart][i%impPart] + e.deferred, e.imp, e.typeOnly = true, im, false // import() site wins continue } } @@ -825,7 +826,8 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E if isNew && tgt != "" { cross := idFile[tgt] != r.File if tgt == owner[c.CallerID] || !slices.Contains(local[[2]string{idFile[tgt], c.Callee}], tgt) || - cross && (!symImp(tgt) && !imported.of(r.File)[idFile[tgt]] || refd()[[2]string{c.CallerID, tgt}]) { + cross && (!symImp(tgt) && !imported.of(r.File)[idFile[tgt]] || refd()[[2]string{c.CallerID, tgt}]) || + c.CallerID == idutil.MakeID(r.File) && symImp(tgt) { // imports edge links the pair tgt = "" } } @@ -1688,22 +1690,26 @@ func importTargets(im Imp, corpus map[string]bool, goPkgs map[string][]string, g if path.Ext(from) == ".rb" && path.Ext(spec) != ".rb" { spec += ".rb" } - if t := resolveRelImport(im.File, spec, corpus); t != "" { - return []string{t} - } - // TypeScript names the output file: `./x.js` is x.ts on disk. - if ext := path.Ext(spec); jsLike(im.File) && (ext == ".js" || ext == ".jsx") { + // TypeScript names the output file: `./x.js` is x.ts on disk. It beats + // x.js.ts and x.js/index.ts, as upstream tries it before appending. + if base, ok := relBase(im.File, spec); ok && jsLike(im.File) && !corpus[base] { tos := []string{".ts", ".tsx"} - if ext == ".jsx" { + switch path.Ext(base) { + case ".js": + case ".jsx": tos = tos[1:] + default: + tos = nil } for _, to := range tos { - t := resolveRelImport(im.File, strings.TrimSuffix(spec, ext)+to, corpus) - if t != "" { + if t := strings.TrimSuffix(base, path.Ext(base)) + to; corpus[t] { return []string{t} } } } + if t := resolveRelImport(im.File, spec, corpus); t != "" { + return []string{t} + } } return out } @@ -1809,16 +1815,24 @@ func resolvePyModule(from, mod string, corpus map[string]bool) string { // trying common extensions and index files. Returns "" for bare (external) // specifiers or unresolved paths. func resolveRelImport(fromFile, spec string, corpus map[string]bool) string { + if base, ok := relBase(fromFile, spec); ok { + return resolveModulePath(base, corpus) + } + return "" +} + +// relBase is the corpus path a relative specifier names, before any probing. +func relBase(fromFile, spec string) (string, bool) { // The `@/` alias is the de-facto project-root convention in Next.js/Vite/ // modern-TS repos; resolve it against the corpus root so those imports // become imports_from edges instead of external dependency nodes. if strings.HasPrefix(spec, "@/") { - return resolveModulePath(path.Clean(strings.TrimPrefix(spec, "@/")), corpus) + return path.Clean(strings.TrimPrefix(spec, "@/")), true } if spec == "" || (spec[0] != '.' && spec[0] != '/') { - return "" // bare specifier — an external package + return "", false // bare specifier — an external package } - return resolveModulePath(path.Clean(path.Join(path.Dir(filepath.ToSlash(fromFile)), spec)), corpus) + return path.Clean(path.Join(path.Dir(filepath.ToSlash(fromFile)), spec)), true } // resolveModulePath probes a corpus-relative module path as-is, then with each From 23443a9d98f743724bc9e0b470a7269e09c1cc96 Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Tue, 20 Oct 2026 11:24:26 -0700 Subject: [PATCH 10/14] Fix round-7 JS/TS new X() same-file and nested binding defects --- internal/extract/javascript.go | 3 ++- internal/extract/jsts_imports_test.go | 13 ++++++++++--- internal/extract/resolve.go | 9 +++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/internal/extract/javascript.go b/internal/extract/javascript.go index c878e82..737df4b 100644 --- a/internal/extract/javascript.go +++ b/internal/extract/javascript.go @@ -1328,7 +1328,8 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { name := fn.Utf8Text(src) for s := callerID; s != ""; s = b.jsParent[s] { if id, ok := b.jsNested[[2]string{s, name}]; ok { - if !isNew { + // no edge when the caller owns the def: contains links them + if !isNew || s != callerID { b.jsNestedCall(callerID, id, line(c)) } return diff --git a/internal/extract/jsts_imports_test.go b/internal/extract/jsts_imports_test.go index afe30bb..eafba70 100644 --- a/internal/extract/jsts_imports_test.go +++ b/internal/extract/jsts_imports_test.go @@ -369,18 +369,25 @@ export class B { export function alias() { return new W(); } export function dflt() { return new Def(); } export function barrel() { return new Renamed(); } -export function shadowFn() { function Y() {} return new Y(); } export function shadowCls() { class Y {} return new Y(); } `}, + {"s.ts", "import { Y } from './x';\nexport function shadowFn() { function Y() {} return new Y(); }\n"}, + {"c5a.ts", "const Thing1 = mk();\nfunction f1() { return new Thing1(); }\nexport namespace Evt { export const v = 1; }\nfunction bar() { return new Evt(); }\n"}, + {"c5b.ts", "export class Thing1 {}\nexport class Evt {}\n"}, + {"e1.ts", "export namespace Solo1 { export const v = 1; }\nfunction baz() { return new Solo1(); }\n"}, + {"nest.ts", "function o() { function Q() {} function d() { return new Q(); } function self() { return new Q(); } }\nfunction p() { function R() { return new R(); } }\n"}, }, "calls") - checkEdges(t, got, []string{"b.ts:B.noRetM() calls x.ts:Y", "b.ts:shadowCls() calls x.ts:Y"}, []string{ + checkEdges(t, got, []string{"b.ts:B.noRetM() calls x.ts:Y", "b.ts:shadowCls() calls x.ts:Y", + "c5a.ts:f1() calls c5a.ts:Thing1", "c5a.ts:bar() calls c5a.ts:Evt", "e1.ts:baz() calls e1.ts:Solo1", + "nest.ts:d() calls nest.ts:Q()", "nest.ts:self() calls nest.ts:Q()"}, []string{ + "nest.ts:o() calls nest.ts:Q()", "nest.ts:R() calls nest.ts:R()", "c5a.ts:f1() calls c5b.ts:Thing1", "c5a.ts:bar() calls c5b.ts:Evt", "b.ts:B.retM() calls x.ts:X", // a references edge is there already "b.ts:B.paramM() calls x.ts:X", "b.ts:B.make() calls b.ts:B", // upstream drops the owner pair "b.ts:alias() calls x.ts:Widget", "b.ts:dflt() calls x.ts:Thing", "b.ts:barrel() calls item.ts:Other", - "b.ts:shadowFn() calls x.ts:Y", + "s.ts:shadowFn() calls x.ts:Y", "s.ts:shadowFn() calls s.ts:Y()", }) } diff --git a/internal/extract/resolve.go b/internal/extract/resolve.go index b3a290c..fbc196d 100644 --- a/internal/extract/resolve.go +++ b/internal/extract/resolve.go @@ -761,6 +761,8 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E if isNew { newIndex() all = newIDs[c.Callee] + // upstream's in-file label map covers every node + here = keep(all, func(id string) bool { return newFile[id] == r.File }) } if c.Recv != "" { q := recvTail(c.Recv) @@ -824,8 +826,8 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } // new needs import evidence if isNew && tgt != "" { - cross := idFile[tgt] != r.File - if tgt == owner[c.CallerID] || !slices.Contains(local[[2]string{idFile[tgt], c.Callee}], tgt) || + cross := newFile[tgt] != r.File + if tgt == owner[c.CallerID] || newFile[tgt] != r.File && !slices.Contains(local[[2]string{idFile[tgt], c.Callee}], tgt) || cross && (!symImp(tgt) && !imported.of(r.File)[idFile[tgt]] || refd()[[2]string{c.CallerID, tgt}]) || c.CallerID == idutil.MakeID(r.File) && symImp(tgt) { // imports edge links the pair tgt = "" @@ -1690,8 +1692,7 @@ func importTargets(im Imp, corpus map[string]bool, goPkgs map[string][]string, g if path.Ext(from) == ".rb" && path.Ext(spec) != ".rb" { spec += ".rb" } - // TypeScript names the output file: `./x.js` is x.ts on disk. It beats - // x.js.ts and x.js/index.ts, as upstream tries it before appending. + // TypeScript names the output file: `./x.js` is x.ts on disk. if base, ok := relBase(im.File, spec); ok && jsLike(im.File) && !corpus[base] { tos := []string{".ts", ".tsx"} switch path.Ext(base) { From fe0ea420832e03c456ba6f9919009232ecd549af Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Tue, 20 Oct 2026 11:39:54 -0700 Subject: [PATCH 11/14] Bind same-file new X() to the last same-label node A same-file new X() with two nodes of that label dropped its edge. It now binds the last one in source order, as upstream's label map does, ignoring a namespace twin of a plain class. --- internal/extract/javascript.go | 8 +++---- internal/extract/jsts_imports_test.go | 32 +++++++++++++++++++++++++++ internal/extract/resolve.go | 20 ++++++++++++----- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/internal/extract/javascript.go b/internal/extract/javascript.go index 737df4b..e14dff8 100644 --- a/internal/extract/javascript.go +++ b/internal/extract/javascript.go @@ -1326,12 +1326,10 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { switch fn.Kind() { case "identifier": name := fn.Utf8Text(src) - for s := callerID; s != ""; s = b.jsParent[s] { + // new binds in the resolver + for s := callerID; s != "" && !isNew; s = b.jsParent[s] { if id, ok := b.jsNested[[2]string{s, name}]; ok { - // no edge when the caller owns the def: contains links them - if !isNew || s != callerID { - b.jsNestedCall(callerID, id, line(c)) - } + b.jsNestedCall(callerID, id, line(c)) return } } diff --git a/internal/extract/jsts_imports_test.go b/internal/extract/jsts_imports_test.go index eafba70..adb0660 100644 --- a/internal/extract/jsts_imports_test.go +++ b/internal/extract/jsts_imports_test.go @@ -745,3 +745,35 @@ func TestJSDynamicImportNoLocation(t *testing.T) { t.Errorf("got %d dynamic_import edges, want 1", n) } } + +// A same-file new X() binds the last node labelled X, as upstream's label +// map does; a namespace twin of a plain class is no candidate. +func TestJSNewSameLabel(t *testing.T) { + for _, c := range []struct{ name, file, src, from, to string }{ + {"ns twin", "m1.ts", "class Repo {}\nnamespace inner { export class Repo {} }\nfunction use() { return new Repo(); }\n", "m1_ts_use", "m1_ts_repo"}, + {"nested after top", "a15.ts", "function T15() {}\nexport function o15() { function T15() {} return new T15(); }\nexport function p15() { return new T15(); }\n", "a15_ts_p15", "a15_ts_o15_t15"}, + {"nested after top js", "j3.js", "function T3() {}\nfunction o3() { function T3() {} return new T3(); }\nfunction p3() { return new T3(); }\n", "j3_js_p3", "j3_js_o3_t3"}, + {"method after class", "a1.ts", "class Foo1 {}\nclass Other1 { Foo1() { return 1; } }\nexport function h1() { return new Foo1(); }\n", "a1_ts_h1", "a1_ts_other1_foo1"}, + {"nested fn after class", "a2.ts", "class Foo2 {}\nfunction g2() { function Foo2() {} return Foo2; }\nexport function h2() { return new Foo2(); }\n", "a2_ts_h2", "a2_ts_g2_foo2"}, + {"caller nested, top last", "n3.ts", "export function o3() { function Q3() {} return new Q3(); }\nexport function Q3() {}\n", "n3_ts_o3", "n3_ts_q3"}, + {"nested outer, top last", "n2.ts", "export function o2() { function Q2() {} function d2() { return new Q2(); } }\nexport function Q2() {}\n", "n2_ts_o2_d2", "n2_ts_q2"}, + } { + ext := Resolve([]Result{FileFromBytes(c.file, []byte(c.src))}, []string{c.file}) + got := "" + for _, e := range ext.Edges { + if e.Relation == model.RelCalls && e.Source == c.from { + got = e.Target + } + } + if got != c.to { + t.Errorf("%s: %s calls %q, want %q", c.name, c.from, got, c.to) + } + } + // the nested def is last, and contains links it + ext := Resolve([]Result{FileFromBytes("q.ts", []byte("export function Q2() {}\nexport function o2() { function Q2() {} return new Q2(); }\n"))}, []string{"q.ts"}) + for _, e := range ext.Edges { + if e.Relation == model.RelCalls && e.Source == "q_ts_o2" { + t.Errorf("o2 calls %q, want no edge", e.Target) + } + } +} diff --git a/internal/extract/resolve.go b/internal/extract/resolve.go index fbc196d..12ba9d4 100644 --- a/internal/extract/resolve.go +++ b/internal/extract/resolve.go @@ -602,7 +602,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } // Upstream indexes all nodes, not defs. - newIDs, newFile := map[string][]string{}, map[string]string{} + newIDs, newFile, newPos := map[string][]string{}, map[string]string{}, map[string]int{} newIndex := sync.OnceFunc(func() { names := map[string]bool{} for _, r := range results { @@ -625,11 +625,14 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E newFile[d.ID] = r.File } } - for _, n := range r.Nodes { - if _, ok := newFile[n.ID]; !ok && n.FileType != "rationale" { + for i, n := range r.Nodes { + if _, ok := newFile[n.ID]; ok { + newPos[n.ID] = i + } else if n.FileType != "rationale" { if l := strings.TrimLeft(strings.Trim(n.Label, "()"), "."); names[l] { newIDs[l] = append(newIDs[l], n.ID) newFile[n.ID] = r.File + newPos[n.ID] = i } } } @@ -761,8 +764,15 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E if isNew { newIndex() all = newIDs[c.Callee] - // upstream's in-file label map covers every node here = keep(all, func(id string) bool { return newFile[id] == r.File }) + if len(here) > 1 { + // upstream: last same-label node wins + nsPrefix := idutil.MakeID(r.File, "ns") + "_" + if d := keep(here, func(id string) bool { return !strings.HasPrefix(id, nsPrefix) }); len(d) > 0 { + here = d + } + here = []string{slices.MaxFunc(here, func(a, b string) int { return newPos[a] - newPos[b] })} + } } if c.Recv != "" { q := recvTail(c.Recv) @@ -827,7 +837,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E // new needs import evidence if isNew && tgt != "" { cross := newFile[tgt] != r.File - if tgt == owner[c.CallerID] || newFile[tgt] != r.File && !slices.Contains(local[[2]string{idFile[tgt], c.Callee}], tgt) || + if tgt == owner[c.CallerID] || owner[tgt] == c.CallerID || newFile[tgt] != r.File && !slices.Contains(local[[2]string{idFile[tgt], c.Callee}], tgt) || cross && (!symImp(tgt) && !imported.of(r.File)[idFile[tgt]] || refd()[[2]string{c.CallerID, tgt}]) || c.CallerID == idutil.MakeID(r.File) && symImp(tgt) { // imports edge links the pair tgt = "" From ab7ad4f1f80868f0b454c705ff0ce836f65426ee Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Tue, 20 Oct 2026 12:20:25 -0700 Subject: [PATCH 12/14] Build refd once and start newIndex beside inheritance --- internal/extract/resolve.go | 92 +++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 45 deletions(-) diff --git a/internal/extract/resolve.go b/internal/extract/resolve.go index 12ba9d4..6bc0d36 100644 --- a/internal/extract/resolve.go +++ b/internal/extract/resolve.go @@ -366,6 +366,45 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } resolved := resolveImportGuided(results, idFile, &out, corpus, local, owner, reexports) + // Upstream indexes all nodes, not defs. + newIDs, newFile, newPos := map[string][]string{}, map[string]string{}, map[string]int{} + newIndex := sync.OnceFunc(func() { + names := map[string]bool{} + for _, r := range results { + if k := slices.Index(r.CallText, jsNewRecv); k > 0 { + for _, n := range r.Calls { + if n.Recv == uint32(k) { + names[r.CallText[n.Callee]] = true + } + } + } + } + for _, r := range results { + if !jsLike(r.File) { + continue + } + // member labels carry the owner + for _, d := range r.Defs { + if _, ok := newFile[d.ID]; names[d.Name] && !ok { + newIDs[d.Name] = append(newIDs[d.Name], d.ID) + newFile[d.ID] = r.File + } + } + for i, n := range r.Nodes { + if _, ok := newFile[n.ID]; ok { + newPos[n.ID] = i + } else if n.FileType != "rationale" { + if l := strings.TrimLeft(strings.Trim(n.Label, "()"), "."); names[l] { + newIDs[l] = append(newIDs[l], n.ID) + newFile[n.ID] = r.File + newPos[n.ID] = i + } + } + } + } + }) + go newIndex() // beside the inheritance pass + // Inheritance: a declared supertype name binds the same way a call does — // same file first, then disambiguated among the definitions sharing the name. // An unresolvable name (a library type outside the corpus) drops, or binds to @@ -601,44 +640,6 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } } - // Upstream indexes all nodes, not defs. - newIDs, newFile, newPos := map[string][]string{}, map[string]string{}, map[string]int{} - newIndex := sync.OnceFunc(func() { - names := map[string]bool{} - for _, r := range results { - if k := slices.Index(r.CallText, jsNewRecv); k > 0 { - for _, n := range r.Calls { - if n.Recv == uint32(k) { - names[r.CallText[n.Callee]] = true - } - } - } - } - for _, r := range results { - if !jsLike(r.File) { - continue - } - // member labels carry the owner - for _, d := range r.Defs { - if _, ok := newFile[d.ID]; names[d.Name] && !ok { - newIDs[d.Name] = append(newIDs[d.Name], d.ID) - newFile[d.ID] = r.File - } - } - for i, n := range r.Nodes { - if _, ok := newFile[n.ID]; ok { - newPos[n.ID] = i - } else if n.FileType != "rationale" { - if l := strings.TrimLeft(strings.Trim(n.Label, "()"), "."); names[l] { - newIDs[l] = append(newIDs[l], n.ID) - newFile[n.ID] = r.File - newPos[n.ID] = i - } - } - } - } - }) - // Calls: prefer a definition in the same file, else disambiguate among the // definitions sharing the called name (unique global, imported file, or same // package) rather than guessing. A member call binds on its receiver: self/this @@ -646,6 +647,14 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E // else only to a definition the receiver's last segment qualifies. // Each run of files binds against the indexes above, which no longer change, // so the runs are bound side by side and their edges joined in file order. + // shared by all runs + refd := sync.OnceValue(func() map[[2]string]bool { // signature already links these + m := make(map[[2]string]bool, len(inherit)) + for _, e := range inherit { + m[[2]string{e.Source, e.Target}] = true + } + return m + }) bind := func(results []Result) [][]model.Edge { // In parts of impPart: one slice a run grew by copies, and the outgrown // ones were live with it at the collection that sets the peak. Only @@ -663,13 +672,6 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E edges[n-1] = append(edges[n-1], e) } imported := importedSet{targets: impTargets} - refd := sync.OnceValue(func() map[[2]string]bool { // signature already links these - m := make(map[[2]string]bool, len(inherit)) - for _, e := range inherit { - m[[2]string{e.Source, e.Target}] = true - } - return m - }) pyMod := map[[2]string]string{} // file, module -> the corpus file of a Python import kept := map[string][]string{} // callee\x00qualifier -> the definitions it qualifies allQuals := map[string][]qualSet{} // callee -> quals of each id in its global list From c56796c633b050968f15097ad5bfd06df86ff3f1 Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Tue, 20 Oct 2026 13:05:02 -0700 Subject: [PATCH 13/14] Skip importTargets until a name matches; index new X() by file Cuts the no-change update on typescript. Output is byte-identical. --- internal/extract/jsmodules.go | 7 +++++-- internal/extract/resolve.go | 26 ++++++++++++-------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/internal/extract/jsmodules.go b/internal/extract/jsmodules.go index 890239e..26e3b63 100644 --- a/internal/extract/jsmodules.go +++ b/internal/extract/jsmodules.go @@ -72,13 +72,16 @@ func (m *jsMods) exports(r *Result) { as = local } for _, im := range r.Imps { - t := importTargets(im, m.corpus, nil, nil, nil) for _, n := range im.Names { name, l, _ := strings.Cut(n, " as ") if l == "" { l = name } - if im.Export || len(t) == 0 || l != local || name == "*" { + if im.Export || l != local || name == "*" { + continue + } + t := importTargets(im, m.corpus, nil, nil, nil) + if len(t) == 0 { continue } m.reex[r.File] = append(m.reex[r.File], jsRe{t[0], name, as}) diff --git a/internal/extract/resolve.go b/internal/extract/resolve.go index 6bc0d36..9d561d1 100644 --- a/internal/extract/resolve.go +++ b/internal/extract/resolve.go @@ -368,6 +368,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E // Upstream indexes all nodes, not defs. newIDs, newFile, newPos := map[string][]string{}, map[string]string{}, map[string]int{} + newHere := map[[2]string][]string{} // file, name -> its newIDs newIndex := sync.OnceFunc(func() { names := map[string]bool{} for _, r := range results { @@ -387,6 +388,8 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E for _, d := range r.Defs { if _, ok := newFile[d.ID]; names[d.Name] && !ok { newIDs[d.Name] = append(newIDs[d.Name], d.ID) + k := [2]string{r.File, d.Name} + newHere[k] = append(newHere[k], d.ID) newFile[d.ID] = r.File } } @@ -396,6 +399,8 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } else if n.FileType != "rationale" { if l := strings.TrimLeft(strings.Trim(n.Label, "()"), "."); names[l] { newIDs[l] = append(newIDs[l], n.ID) + k := [2]string{r.File, l} + newHere[k] = append(newHere[k], n.ID) newFile[n.ID] = r.File newPos[n.ID] = i } @@ -766,7 +771,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E if isNew { newIndex() all = newIDs[c.Callee] - here = keep(all, func(id string) bool { return newFile[id] == r.File }) + here = newHere[lkey] if len(here) > 1 { // upstream: last same-label node wins nsPrefix := idutil.MakeID(r.File, "ns") + "_" @@ -1705,7 +1710,8 @@ func importTargets(im Imp, corpus map[string]bool, goPkgs map[string][]string, g spec += ".rb" } // TypeScript names the output file: `./x.js` is x.ts on disk. - if base, ok := relBase(im.File, spec); ok && jsLike(im.File) && !corpus[base] { + base, ok := relBase(im.File, spec) + if ok && jsLike(im.File) && !corpus[base] { tos := []string{".ts", ".tsx"} switch path.Ext(base) { case ".js": @@ -1720,8 +1726,10 @@ func importTargets(im Imp, corpus map[string]bool, goPkgs map[string][]string, g } } } - if t := resolveRelImport(im.File, spec, corpus); t != "" { - return []string{t} + if ok { + if t := resolveModulePath(base, corpus); t != "" { + return []string{t} + } } } return out @@ -1824,16 +1832,6 @@ func resolvePyModule(from, mod string, corpus map[string]bool) string { return found } -// resolveRelImport maps a relative import specifier to a file in the corpus, -// trying common extensions and index files. Returns "" for bare (external) -// specifiers or unresolved paths. -func resolveRelImport(fromFile, spec string, corpus map[string]bool) string { - if base, ok := relBase(fromFile, spec); ok { - return resolveModulePath(base, corpus) - } - return "" -} - // relBase is the corpus path a relative specifier names, before any probing. func relBase(fromFile, spec string) (string, bool) { // The `@/` alias is the de-facto project-root convention in Next.js/Vite/ From 524e9498fc6f23eb9ce7f22d4d36de6082c9ac27 Mon Sep 17 00:00:00 2001 From: Chris Dobbyn Date: Tue, 20 Oct 2026 14:06:39 -0700 Subject: [PATCH 14/14] Track method spans in the walk; keep refd to new-call callers Replaces the per-call Parent() climb in jsCall and shrinks the refd map. Output is byte-identical on the corpora and fixtures checked. --- internal/extract/javascript.go | 22 +++++++++++++++------- internal/extract/resolve.go | 9 +++++++-- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/internal/extract/javascript.go b/internal/extract/javascript.go index e14dff8..0f796a7 100644 --- a/internal/extract/javascript.go +++ b/internal/extract/javascript.go @@ -23,6 +23,7 @@ func init() { for _, l := range []*gositter.Language{tsjs.Language(), tsts.Language(), tstsx.Language()} { l.KindMask("call_expression") l.KindMask("new_expression") + l.KindMask("method_definition") l.KindMask("export_statement") } } @@ -44,8 +45,16 @@ func extractJS(rel string, src []byte, langPtr *gositter.Language) Result { slices.SortStableFunc(b.jsBodies, func(x, y jsBody) int { return cmp.Compare(x.start, y.start) }) next := 0 var open []jsBody - walkKinds(root, langPtr.KindMask("call_expression")|langPtr.KindMask("new_expression"), func(cv ts.Node) bool { + var meths [][2]uint // open method_definition spans + walkKinds(root, langPtr.KindMask("call_expression")|langPtr.KindMask("new_expression")|langPtr.KindMask("method_definition"), func(cv ts.Node) bool { c := &cv + for len(meths) > 0 && c.StartByte() >= meths[len(meths)-1][1] { + meths = meths[:len(meths)-1] + } + if c.Kind() == "method_definition" { + meths = append(meths, [2]uint{c.StartByte(), c.EndByte()}) + return true + } for len(open) > 0 && c.StartByte() >= open[len(open)-1].end { open = open[:len(open)-1] } @@ -61,7 +70,8 @@ func extractJS(rel string, src []byte, langPtr *gositter.Language) Result { } b.jsCallImport(c, src, body.callerID, body) if len(open) > 0 { - b.jsCall(c, body, src) + inMethod := len(meths) > 0 && meths[len(meths)-1][0] >= body.start && meths[len(meths)-1] != [2]uint{body.start, body.end} + b.jsCall(c, body, src, inMethod) } return true }) @@ -1307,7 +1317,7 @@ const jsNewRecv = "\x00new" // jsCall records the call site c, a call_expression or new_expression, for // the body's caller. -func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { +func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte, inMethod bool) { callerID := body.callerID isNew := c.Kind() == "new_expression" fn := c.ChildByFieldName("function") @@ -1318,10 +1328,8 @@ func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { return } // A method body is a boundary: its calls are no caller's. - for p := c; p != nil && (p.StartByte() != body.start || p.EndByte() != body.end); p = p.Parent() { - if p.Kind() == "method_definition" { - return - } + if inMethod { + return } switch fn.Kind() { case "identifier": diff --git a/internal/extract/resolve.go b/internal/extract/resolve.go index 9d561d1..a073d79 100644 --- a/internal/extract/resolve.go +++ b/internal/extract/resolve.go @@ -369,6 +369,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E // Upstream indexes all nodes, not defs. newIDs, newFile, newPos := map[string][]string{}, map[string]string{}, map[string]int{} newHere := map[[2]string][]string{} // file, name -> its newIDs + newCallers := map[string]bool{} // who calls a new X() newIndex := sync.OnceFunc(func() { names := map[string]bool{} for _, r := range results { @@ -376,6 +377,7 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E for _, n := range r.Calls { if n.Recv == uint32(k) { names[r.CallText[n.Callee]] = true + newCallers[r.CallText[n.CallerID]] = true } } } @@ -654,9 +656,12 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E // so the runs are bound side by side and their edges joined in file order. // shared by all runs refd := sync.OnceValue(func() map[[2]string]bool { // signature already links these - m := make(map[[2]string]bool, len(inherit)) + newIndex() + m := map[[2]string]bool{} for _, e := range inherit { - m[[2]string{e.Source, e.Target}] = true + if newCallers[e.Source] { // only they ask + m[[2]string{e.Source, e.Target}] = true + } } return m })