Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] sui recursive build detected #659

Merged
merged 1 commit into from
Jul 6, 2024
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
44 changes: 41 additions & 3 deletions sui/core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
ctx = NewBuildContext(nil)
}

// Push the current page onto the stack and increment the visit counter
ctx.stack = append(ctx.stack, page.Route)
ctx.visited[page.Route]++
defer func() {
ctx.stack = ctx.stack[:len(ctx.stack)-1] // Pop the stack
ctx.visited[page.Route]--
}()

// Check for recursive calls
if ctx.visited[page.Route] > 1 {
return nil, ctx.warnings, fmt.Errorf("recursive build detected for page %s", page.Route)
}

ctx.sequence++
html, err := page.BuildHTML(option)
if err != nil {
Expand Down Expand Up @@ -68,6 +81,19 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op
return "", fmt.Errorf("The parent page is not set")
}

// Push the current page onto the stack and increment the visit counter
ctx.stack = append(ctx.stack, page.Route)
ctx.visited[page.Route]++
defer func() {
ctx.stack = ctx.stack[:len(ctx.stack)-1] // Pop the stack
ctx.visited[page.Route]--
}()

// Check for recursive calls
if ctx.visited[page.Route] > 1 {
return "", fmt.Errorf("recursive build detected for page %s", page.Route)
}

name, exists := sel.Attr("is")
if !exists {
return "", fmt.Errorf("The component tag must have an is attribute")
Expand Down Expand Up @@ -148,6 +174,7 @@ func (page *Page) copySlots(ctx *BuildContext, from *goquery.Selection, to *goqu
html, err := slot.Html()
if err != nil {
ctx.warnings = append(ctx.warnings, err.Error())
setError(slotSel, err)
continue
}
slotSel.SetHtml(html)
Expand All @@ -172,6 +199,7 @@ func (page *Page) copyProps(ctx *BuildContext, from *goquery.Selection, to *goqu
val, err := data.Exec(fmt.Sprintf("{{ %s }}", attr.Key[3:]))
if err != nil {
ctx.warnings = append(ctx.warnings, err.Error())
setError(to, err)
continue
}
switch value := val.(type) {
Expand Down Expand Up @@ -245,21 +273,26 @@ func (page *Page) buildComponents(doc *goquery.Document, ctx *BuildContext, opti

ipage, err := tmpl.Page(name)
if err != nil {
sel.ReplaceWith(fmt.Sprintf("<!-- %s -->", err.Error()))
setError(sel, err)
log.Warn("Page %s/%s/%s: %s", page.SuiID, page.TemplateID, page.Route, err.Error())
return
}

err = ipage.Load()
if err != nil {
sel.ReplaceWith(fmt.Sprintf("<!-- %s -->", err.Error()))
setError(sel, err)
log.Warn("Page %s/%s/%s: %s", page.SuiID, page.TemplateID, page.Route, err.Error())
return
}

component := ipage.Get()
component.parent = page
component.BuildAsComponent(sel, ctx, option)
_, err = component.BuildAsComponent(sel, ctx, option)
if err != nil {
setError(sel, err)
log.Warn("Page %s/%s/%s: %s", page.SuiID, page.TemplateID, page.Route, err.Error())
return
}
return
})

Expand Down Expand Up @@ -424,6 +457,11 @@ func (page *Page) BuildHTML(option *BuildOption) (string, error) {
return string(res), nil
}

func setError(sel *goquery.Selection, err error) {
html := `<div style="color:red; margin:10px 0px; font-size: 12px; font-family: monospace; padding: 10px; border: 1px solid red; background-color: #f8d7da;">%s</div>`
sel.SetHtml(fmt.Sprintf(html, err.Error()))
}

func addTabToEachLine(input string, prefix ...string) string {
var lines []string

Expand Down
2 changes: 2 additions & 0 deletions sui/core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ func NewBuildContext(global *GlobalBuildContext) *BuildContext {
jitComponents: map[string]bool{},
global: global,
warnings: []string{},
visited: map[string]int{},
stack: []string{},
}
}

Expand Down
2 changes: 2 additions & 0 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type BuildContext struct {
global *GlobalBuildContext
translations []Translation
warnings []string
visited map[string]int // Keep a counter for each page
stack []string // Stack to manage build states
}

// ScriptNode is the struct for the script node
Expand Down