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

fix: format single line raw go code #767

Merged
merged 1 commit into from
Jun 1, 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
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
Loading