diff --git a/internal/analyze/analyze.go b/internal/analyze/analyze.go index 27f9fee..63260c3 100644 --- a/internal/analyze/analyze.go +++ b/internal/analyze/analyze.go @@ -178,7 +178,8 @@ func ImportCycles(g *model.Graph, maxLen, topN int) []Cycle { 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() { + // Deferred imports run late. + 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 7d84a04..7fc494d 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 = 24 +const schema = 25 // 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..5c799c4 100644 --- a/internal/extract/component.go +++ b/internal/extract/component.go @@ -49,10 +49,13 @@ 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 } - 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 0132761..120389b 100644 --- a/internal/extract/extract.go +++ b/internal/extract/extract.go @@ -114,6 +114,8 @@ type Imp struct { Rel bool // Export marks a JS/TS `export ... from Spec`. Export bool `json:",omitempty"` + // 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 // its text, a bare import nothing; Export fits the padding after Rel. @@ -250,11 +252,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": @@ -547,6 +549,19 @@ 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 +) + +// 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}) + } +} + // 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..0f796a7 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" @@ -21,6 +22,8 @@ import ( 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") } } @@ -42,9 +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"), 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 - b.jsCallImport(c, src) + 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] } @@ -54,8 +64,14 @@ func extractJS(rel string, src []byte, langPtr *gositter.Language) Result { } next++ } + var body jsBody if len(open) > 0 { - b.jsCall(c, open[len(open)-1], src) + body = open[len(open)-1] + } + b.jsCallImport(c, src, body.callerID, body) + if len(open) > 0 { + 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 }) @@ -77,6 +93,68 @@ 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 + } + line, prev, slash := 1, 0, false // slash: // seen on this line + 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 + } + // 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 + } + 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 +} + // 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 @@ -204,22 +282,115 @@ 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) { +// 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 c, p := n, n; p != nil && (p.StartByte() != body.start || p.EndByte() != body.end); c, p = p, p.Parent() { + switch p.Kind() { + 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 +} + +// 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 + parens := 0 + 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 == "parenthesized_expression" { + parens++ + } + if k != "unary_expression" && k != "member_expression" && k != "subscript_expression" && k != "parenthesized_expression" && k != "array" { + break + } + } + if bin == nil { + return false + } + end := n.EndByte() + 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++ + // 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 { + 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, body jsBody) { if n.Kind() != "call_expression" { return } fn, args := n.ChildByFieldName("function"), n.ChildByFieldName("arguments") - if fn == nil || args == nil || args.NamedChildCount() != 1 { + if fn == nil || args == nil || args.NamedChildCount() == 0 { return } - if fn.Kind() != "import" && (fn.Kind() != "identifier" || fn.Utf8Text(src) != "require") { - return + one := args.NamedChildCount() == 1 && args.NamedChild(0).Kind() == "string" + switch { + case fn.Kind() == "identifier" && fn.Utf8Text(src) == "require": + if one { + b.imp(unquote(args.NamedChild(0).Utf8Text(src)), line(n)) + } + case fn.Kind() == "import": + // 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)) + } + if caller != "" && (jsInSignature(n, body) || b.jsMaskedImport(n, src)) { + caller = "" + } + b.impDeferred(jsImportSpec(args, src), line(n), caller) } - if a := args.NamedChild(0); a.Kind() == "string" { - b.imp(unquote(a.Utf8Text(src)), line(n)) +} + +// 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": + return strings.Trim(a.Utf8Text(src), "'\" ") + case "template_string": + 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 @@ -1117,43 +1288,89 @@ 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. -func (b *builder) jsCall(c *ts.Node, body jsBody, src []byte) { +// 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, inMethod bool) { callerID := body.callerID + isNew := c.Kind() == "new_expression" fn := c.ChildByFieldName("function") + if isNew { + fn = c.ChildByFieldName("constructor") + } if fn == nil || callerID == "" { 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": 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 { b.jsNestedCall(callerID, id, line(c)) return } } + if isNew && jsNewBuiltins[name] { + 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)...) if b.seen[id] && !b.jsBare[id] { - b.jsNestedCall(callerID, id, line(c)) + // 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 + }) { + b.jsNestedCall(callerID, id, line(c)) + } return } } + if isNew { + // 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. if callerID == b.fileID && b.jsFileOwns(name) { return } 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/jsmodules.go b/internal/extract/jsmodules.go index 5b3725f..26e3b63 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" } @@ -77,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/jsts_imports_test.go b/internal/extract/jsts_imports_test.go new file mode 100644 index 0000000..adb0660 --- /dev/null +++ b/internal/extract/jsts_imports_test.go @@ -0,0 +1,779 @@ +package extract + +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. +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"}, + // the alias binds, not a tie-break + {"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) + } + } +} + +// 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) + } + } +} + +// 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: 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) + } + } +} + +// 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"}) + + // 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"}) +} + +// 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", + }) +} + +// 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; 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"}, + {"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 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", + "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", + "s.ts:shadowFn() calls x.ts:Y", "s.ts:shadowFn() calls s.ts:Y()", + }) +} + +// 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'; +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", + "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{ + "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", + }) +} + +// 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{ + {"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{ + {"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"}) +} + +// 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) + } +} + +// 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", + }) +} + +// 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; } +} +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", "i1", "i2", "i3", "i4"} { + 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", + "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", + "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(); }); +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", "a", "c", "l"} { + 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", + "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", + }) +} + +// 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", + }) +} + +// 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) + } +} + +// 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 28b416c..38c7ecf 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: imports_from, or imports typeOnly, external bool + deferred bool } var impEdges [][]impRec // parts of impPart type impBatch struct { @@ -185,6 +187,11 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E num uint32 } ids := map[string]tgtID{} + 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 +203,26 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E for b := range batches { nextImp := 0 for _, r := range b.results { + // a file-level import hides dynamic_import + var fileHas map[string]bool + for k := range r.Imps { + 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) || im.Dyn == 0 && jsSelfFallback(im, b.targets[nextImp+k]) { + continue + } + for _, t := range b.targets[nextImp+k] { + fileHas[idutil.MakeID(t)] = true + } + if len(b.targets[nextImp+k]) == 0 && im.Dyn == dynDeferred { + fileHas[idutil.MakeID(im.Spec)] = true + } + } for k := range r.Imps { im := &r.Imps[k] src, ok := srcNum[im.FileID] @@ -205,6 +232,48 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } targets := b.targets[nextImp] nextImp++ + if im.Dyn == 0 && jsSelfFallback(im, targets) { + continue + } + 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 && fileHas[t] { + continue + } + dynSeen[key] = true + // deferred wins, as upstream + if tn, ok := tgtNum[t]; ok { + if i, ok := impEdge[uint64(src)<<32|uint64(tn)]; ok && im.Dyn == dynDeferred { + e := &impEdges[i/impPart][i%impPart] + e.deferred, e.imp, e.typeOnly = true, im, false // import() site wins + continue + } + } + 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 { @@ -215,6 +284,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 { @@ -254,7 +326,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 { + // 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) } } @@ -293,6 +366,52 @@ 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{} + 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 { + 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 + newCallers[r.CallText[n.CallerID]] = 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) + k := [2]string{r.File, d.Name} + newHere[k] = append(newHere[k], 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) + k := [2]string{r.File, l} + newHere[k] = append(newHere[k], 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 @@ -542,6 +661,17 @@ 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 + newIndex() + m := map[[2]string]bool{} + for _, e := range inherit { + if newCallers[e.Source] { // only they ask + 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 @@ -567,6 +697,16 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E for _, r := range results { rf := model.FileOf(r.File) java := strings.HasSuffix(r.File, ".java") + var named map[string]bool + 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}] { @@ -577,6 +717,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 @@ -644,6 +788,19 @@ func ResolveParts(results []Result, files []string) ([][]model.Node, [][]model.E } all = ids } + if isNew { + newIndex() + all = newIDs[c.Callee] + here = newHere[lkey] + 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) ok := func(id string) bool { return q != "" && quals[id].has(q) } @@ -676,13 +833,22 @@ 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 class name + if isNew && !slices.Contains(local[[2]string{idFile[tgt], c.Callee}], tgt) { + tgt = "" + } } if tgt == "" && len(here) == 1 { tgt = here[0] } - if tgt == "" { + if tgt == "" && isNew && len(all) > 1 { + // no directory tie-break upstream + if tgt = unique(all, symImp); tgt == "" { + tgt = unique(all, func(id string) bool { return imported.of(r.File)[newFile[id]] }) + } + } else if tgt == "" { tgt = disambiguate(all, r.File, idFile, &imported) } } @@ -691,9 +857,19 @@ 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 + if isNew && tgt != "" { + cross := newFile[tgt] != r.File + 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 = "" + } + } // 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]}] { @@ -779,15 +955,22 @@ 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, }) + 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) } edges = append(edges, part) } @@ -1552,13 +1735,25 @@ 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") { - ts := strings.TrimSuffix(spec, ext) + ".t" + ext[2:] - if t := resolveRelImport(im.File, ts, corpus); t != "" && t != from { + base, ok := relBase(im.File, spec) + if ok && jsLike(im.File) && !corpus[base] { + tos := []string{".ts", ".tsx"} + switch path.Ext(base) { + case ".js": + case ".jsx": + tos = tos[1:] + default: + tos = nil + } + for _, to := range tos { + if t := strings.TrimSuffix(base, path.Ext(base)) + to; corpus[t] { + return []string{t} + } + } + } + if ok { + if t := resolveModulePath(base, corpus); t != "" { return []string{t} } } @@ -1566,6 +1761,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. @@ -1656,20 +1858,18 @@ 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 { +// 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 diff --git a/internal/extract/resultjson.go b/internal/extract/resultjson.go index 183960f..a55857f 100644 --- a/internal/extract/resultjson.go +++ b/internal/extract/resultjson.go @@ -73,6 +73,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 @@ -211,6 +214,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/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 4c7ccc9..a0691de 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) @@ -68,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 9ed6c23..916f9df 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 } @@ -24,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) + } + } +} diff --git a/internal/model/code.go b/internal/model/code.go index 8baa4f0..a3ef566 100644 --- a/internal/model/code.go +++ b/internal/model/code.go @@ -31,6 +31,7 @@ const ( RelCites RelImplements RelReExports + RelDynamicImport ) const ( @@ -41,12 +42,12 @@ 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") ) // 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 @@ -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_test.go b/internal/query/affected_test.go index 873458a..455d575 100644 --- a/internal/query/affected_test.go +++ b/internal/query/affected_test.go @@ -147,6 +147,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) {