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