Skip to content

Commit

Permalink
fix: format single line raw go code (#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
joerdav authored Jun 1, 2024
1 parent 599b54f commit aaa0049
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
6 changes: 6 additions & 0 deletions parser/v2/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import (
"path/filepath"
"testing"

"github.com/a-h/templ/cfg"
"github.com/google/go-cmp/cmp"
"golang.org/x/tools/txtar"
)

func TestFormatting(t *testing.T) {
oldFlags := cfg.Experiment
t.Cleanup(func() {
cfg.Experiment = oldFlags
})
cfg.Experiment.RawGo = true
files, _ := filepath.Glob("formattestdata/*.txt")
if len(files) == 0 {
t.Errorf("no test files found")
Expand Down
12 changes: 12 additions & 0 deletions parser/v2/formattestdata/raw_go_is_formatted.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- in --
package test

templ nameList(items []Item) {
{{ first := items[0] }}
}
-- out --
package test

templ nameList(items []Item) {
{{ first := items[0] }}
}
12 changes: 6 additions & 6 deletions parser/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,14 +1136,14 @@ func (gc GoCode) Write(w io.Writer, indent int) error {
if isWhitespace(gc.Expression.Value) {
gc.Expression.Value = ""
}
if !gc.Multiline {
return writeIndent(w, indent, `{{ `, gc.Expression.Value, ` }}`)
}
formatted, err := format.Source([]byte(gc.Expression.Value))
source, err := format.Source([]byte(gc.Expression.Value))
if err != nil {
return err
source = []byte(gc.Expression.Value)
}
if !gc.Multiline {
return writeIndent(w, indent, `{{ `, string(source), ` }}`)
}
if err := writeIndent(w, indent, "{{"+string(formatted)+"\n"); err != nil {
if err := writeIndent(w, indent, "{{"+string(source)+"\n"); err != nil {
return err
}
return writeIndent(w, indent, "}}")
Expand Down

0 comments on commit aaa0049

Please sign in to comment.