Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions internal/analyze/typeonly_cycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion internal/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/export/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion internal/extract/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
21 changes: 18 additions & 3 deletions internal/extract/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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 != "" {
Expand Down
Loading
Loading