Skip to content

Commit

Permalink
feat: support build tags by copying template header over (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
joerdav authored Apr 19, 2024
1 parent f300746 commit 6af8b3b
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 151 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.663
0.2.664
15 changes: 15 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func (g *generator) generate() (err error) {
if err = g.writeGeneratedDateComment(); err != nil {
return
}
if err = g.writeHeader(); err != nil {
return
}
if err = g.writePackage(); err != nil {
return
}
Expand Down Expand Up @@ -132,6 +135,18 @@ func (g *generator) writeGeneratedDateComment() (err error) {
return err
}

func (g *generator) writeHeader() (err error) {
if len(g.tf.Header) == 0 {
return nil
}
for _, n := range g.tf.Header {
if err := g.writeGoExpression(n); err != nil {
return err
}
}
return err
}

func (g *generator) writePackage() error {
var r parser.Range
var err error
Expand Down
2 changes: 1 addition & 1 deletion parser/v2/templatefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (p TemplateFileParser) Parse(pi *parse.Input) (tf TemplateFile, ok bool, er
}
var newLine string
newLine, _, _ = parse.NewLine.Parse(pi)
tf.Header = append(tf.Header, TemplateFileGoExpression{Expression: NewExpression(line+newLine, from, pi.Position())})
tf.Header = append(tf.Header, TemplateFileGoExpression{Expression: NewExpression(line+newLine, from, pi.Position()), BeforePackage: true})
}

// Strip any whitespace between the template declaration and the first template.
Expand Down
147 changes: 0 additions & 147 deletions parser/v2/templatefile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package parser

import (
"reflect"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestTemplateFileParser(t *testing.T) {
Expand Down Expand Up @@ -160,150 +157,6 @@ templ template(
})
}

func TestTemplateFileRoundTrip(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "template files can be round tripped",
input: `package goof
templ Hello() {
Hello
}
`,
expected: `package goof
templ Hello() {
Hello
}
`,
},
{
name: "template files can start with comments",
input: `// Go comment
package goof
templ Hello() {
Hello
}
`,
expected: `// Go comment
package goof
templ Hello() {
Hello
}
`,
},
{
name: "template files can start with comments, mixed with whitespace",
input: `
// Go comment
package goof
templ Hello() {
Hello
}
`,
expected: `
// Go comment
package goof
templ Hello() {
Hello
}
`,
},
{
name: "template files can start with multiline comments",
input: `
/********************
* multiline message *
********************/
package goof
templ Hello() {
Hello
}
`,
expected: `
/********************
* multiline message *
********************/
package goof
templ Hello() {
Hello
}
`,
},
{
name: "template files can start with comments, mixed with whitespace",
input: `
// Go comment
/* Multiline comment on a single line */
/*
Multi-line comment on multiple lines
*/
package goof
templ Hello() {
Hello
}
`,
expected: `
// Go comment
/* Multiline comment on a single line */
/*
Multi-line comment on multiple lines
*/
package goof
templ Hello() {
Hello
}
`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tf, err := ParseString(tt.input)
if err != nil {
t.Fatalf("failed to parse template file: %v", err)
}

sb := new(strings.Builder)
err = tf.Write(sb)
if err != nil {
t.Fatalf("failed to write template file: %v", err)
}
output := sb.String()

if diff := cmp.Diff(tt.expected, output); diff != "" {
t.Errorf("unexpected output (-want +got):\n%s", diff)
}
})
}
}

func TestDefaultPackageName(t *testing.T) {
tests := []struct {
name string
Expand Down
13 changes: 13 additions & 0 deletions parser/v2/testdata/templatefile_can_be_round_tripped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- in --
package goof

templ Hello() {
Hello
}

-- out --
package goof

templ Hello() {
Hello
}
14 changes: 14 additions & 0 deletions parser/v2/testdata/templatefile_can_start_with_comments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- in --
// Go comment
package goof

templ Hello() {
Hello
}
-- out --
// Go comment
package goof

templ Hello() {
Hello
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- in --
// Go comment

package goof

templ Hello() {
Hello
}
-- out --
// Go comment

package goof

templ Hello() {
Hello
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- in --
/********************
* multiline message *
********************/

package goof

templ Hello() {
Hello
}
-- out --
/********************
* multiline message *
********************/

package goof

templ Hello() {
Hello
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- in --
// Go comment

/* Multiline comment on a single line */

/*

Multi-line comment on multiple lines

*/

package goof

templ Hello() {
Hello
}
-- out --
// Go comment

/* Multiline comment on a single line */

/*

Multi-line comment on multiple lines

*/

package goof

templ Hello() {
Hello
}
10 changes: 10 additions & 0 deletions parser/v2/testdata/templateheader_with_build_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- in --
//go:build dev

package p

-- out --
//go:build dev

package p

13 changes: 11 additions & 2 deletions parser/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,24 @@ type TemplateFileNode interface {

// TemplateFileGoExpression within a TemplateFile
type TemplateFileGoExpression struct {
Expression Expression
Expression Expression
BeforePackage bool
}

func (exp TemplateFileGoExpression) IsTemplateFileNode() bool { return true }
func (exp TemplateFileGoExpression) Write(w io.Writer, indent int) error {
data, err := format.Source([]byte(exp.Expression.Value))
in := exp.Expression.Value

if exp.BeforePackage {
in += "\\\\formatstring\npackage p\n\\\\formatstring"
}
data, err := format.Source([]byte(in))
if err != nil {
return writeIndent(w, indent, exp.Expression.Value)
}
if exp.BeforePackage {
data = bytes.TrimSuffix(data, []byte("\\\\formatstring\npackage p\n\\\\formatstring"))
}
_, err = w.Write(data)
return err
}
Expand Down

0 comments on commit 6af8b3b

Please sign in to comment.