Skip to content

Commit 83b0f4a

Browse files
committed
extract require cycle detection helpers
1 parent cef2b32 commit 83b0f4a

2 files changed

Lines changed: 59 additions & 56 deletions

File tree

vibes/modules_cycles.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package vibes
2+
3+
import "strings"
4+
5+
func moduleCycleFromLoadStack(stack []string, next string) ([]string, bool) {
6+
for idx, key := range stack {
7+
if key == next {
8+
cycle := append(append([]string(nil), stack[idx:]...), next)
9+
return cycle, true
10+
}
11+
}
12+
return nil, false
13+
}
14+
15+
func moduleExecutionChain(stack []moduleContext) []string {
16+
chain := make([]string, 0, len(stack))
17+
for _, ctx := range stack {
18+
if ctx.key == "" {
19+
continue
20+
}
21+
if len(chain) > 0 && chain[len(chain)-1] == ctx.key {
22+
continue
23+
}
24+
chain = append(chain, ctx.key)
25+
}
26+
return chain
27+
}
28+
29+
func moduleCycleFromExecution(stack []moduleContext, next string) ([]string, bool) {
30+
chain := moduleExecutionChain(stack)
31+
if len(chain) < 2 {
32+
return nil, false
33+
}
34+
for idx, key := range chain[:len(chain)-1] {
35+
if key == next {
36+
cycle := append(append([]string(nil), chain[idx:]...), next)
37+
return cycle, true
38+
}
39+
}
40+
return nil, false
41+
}
42+
43+
func formatModuleCycle(cycle []string) string {
44+
if len(cycle) == 0 {
45+
return ""
46+
}
47+
normalized := make([]string, 0, len(cycle))
48+
for _, key := range cycle {
49+
if len(normalized) > 0 && normalized[len(normalized)-1] == key {
50+
continue
51+
}
52+
normalized = append(normalized, key)
53+
}
54+
parts := make([]string, len(normalized))
55+
for idx, key := range normalized {
56+
parts[idx] = moduleDisplayName(key)
57+
}
58+
return strings.Join(parts, " -> ")
59+
}

vibes/modules_require.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,62 +6,6 @@ import (
66
"strings"
77
)
88

9-
func moduleCycleFromLoadStack(stack []string, next string) ([]string, bool) {
10-
for idx, key := range stack {
11-
if key == next {
12-
cycle := append(append([]string(nil), stack[idx:]...), next)
13-
return cycle, true
14-
}
15-
}
16-
return nil, false
17-
}
18-
19-
func moduleExecutionChain(stack []moduleContext) []string {
20-
chain := make([]string, 0, len(stack))
21-
for _, ctx := range stack {
22-
if ctx.key == "" {
23-
continue
24-
}
25-
if len(chain) > 0 && chain[len(chain)-1] == ctx.key {
26-
continue
27-
}
28-
chain = append(chain, ctx.key)
29-
}
30-
return chain
31-
}
32-
33-
func moduleCycleFromExecution(stack []moduleContext, next string) ([]string, bool) {
34-
chain := moduleExecutionChain(stack)
35-
if len(chain) < 2 {
36-
return nil, false
37-
}
38-
for idx, key := range chain[:len(chain)-1] {
39-
if key == next {
40-
cycle := append(append([]string(nil), chain[idx:]...), next)
41-
return cycle, true
42-
}
43-
}
44-
return nil, false
45-
}
46-
47-
func formatModuleCycle(cycle []string) string {
48-
if len(cycle) == 0 {
49-
return ""
50-
}
51-
normalized := make([]string, 0, len(cycle))
52-
for _, key := range cycle {
53-
if len(normalized) > 0 && normalized[len(normalized)-1] == key {
54-
continue
55-
}
56-
normalized = append(normalized, key)
57-
}
58-
parts := make([]string, len(normalized))
59-
for idx, key := range normalized {
60-
parts[idx] = moduleDisplayName(key)
61-
}
62-
return strings.Join(parts, " -> ")
63-
}
64-
659
func shouldExportModuleFunction(fn *ScriptFunction) bool {
6610
return fn != nil && !fn.Private
6711
}

0 commit comments

Comments
 (0)