Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-mai authored and a-h committed Sep 27, 2023
1 parent 9d7c432 commit 0244fef
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
26 changes: 26 additions & 0 deletions parser/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,14 @@ func writeNodes(w io.Writer, indent int, nodes []Node, block bool) error {
for i := 0; i < len(nodes); i++ {
// Terminating and leading whitespace is stripped.
_, isWhitespace := nodes[i].(Whitespace)

// allow a single space between StringExpressions
// prevents <div>{firstName} {lastName}</div> from becoming <div>{firstName}{lastName}</div>
if isWhitespace && !block && previousIs[StringExpression](nodes, i) && futureHasAnythingOtherThan[Whitespace](nodes, i) {
w.Write([]byte(" "))

Check failure on line 457 in parser/v2/types.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `w.Write` is not checked (errcheck)
continue
}

if isWhitespace && (i == 0 || i == len(nodes)-1) {
continue
}
Expand All @@ -469,6 +477,24 @@ func writeNodes(w io.Writer, indent int, nodes []Node, block bool) error {
return nil
}

func futureHasAnythingOtherThan[T any](nodes []Node, cur int) bool {
for i := cur + 1; i < len(nodes); i++ {
if _, ok := nodes[i].(T); !ok {
return true
}
}
return false
}

func previousIs[T any](nodes []Node, cur int) bool {
for i := cur; i >= 0; i-- {
if _, ok := nodes[i].(T); ok {
return true
}
}
return false
}

type RawElement struct {
Name string
Attributes []Attribute
Expand Down
36 changes: 36 additions & 0 deletions parser/v2/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,42 @@ templ x() {
</li>
}
`,
},
{
name: "spacing between string expressions is kept",
input: ` // first line removed to make indentation clear
package main
templ x() {
<div>{firstName} {lastName}</div>
}
`,
expected: ` // first line removed to make indentation clear
package main
templ x() {
<div>{ firstName } { lastName }</div>
}
`,
},
{
name: "spacing between string expressions is not magically added",
input: ` // first line removed to make indentation clear
package main
templ x() {
<div>{pt1}{pt2}</div>
}
`,
expected: ` // first line removed to make indentation clear
package main
templ x() {
<div>{ pt1 }{ pt2 }</div>
}
`,
},
}
Expand Down

0 comments on commit 0244fef

Please sign in to comment.