Repository: a-h/templ
Pull request #1186 (2025-06-10 10:07:33)

fix: comments don't eat newlines
Fixes https://github.com/a-h/templ/issues/1182


Patch:
diff --git a/parser/v2/gocommentparser.go b/parser/v2/gocommentparser.go
index 051b46e..b12295a 100644
--- a/parser/v2/gocommentparser.go
+++ b/parser/v2/gocommentparser.go
@@ -4,11 +4,12 @@ import (
 	"github.com/a-h/parse"
 )
 
-var goSingleLineCommentStart = parse.String("//")
-var goSingleLineCommentEnd = parse.Any(parse.NewLine, parse.EOF[string]())
+var (
+	goSingleLineCommentStart = parse.String("//")
+	goSingleLineCommentEnd   = parse.Any(parse.NewLine, parse.EOF[string]())
+)
 
-type goSingleLineCommentParser struct {
-}
+type goSingleLineCommentParser struct{}
 
 var goSingleLineComment = goSingleLineCommentParser{}
 
@@ -24,18 +25,17 @@ func (p goSingleLineCommentParser) Parse(pi *parse.Input) (n Node, ok bool, err
 		err = parse.Error("expected end comment literal '\n' not found", pi.Position())
 		return
 	}
-	// Move past the end element.
-	_, _, _ = goSingleLineCommentEnd.Parse(pi)
 	// Return the comment.
 	c.Multiline = false
 	return c, true, nil
 }
 
-var goMultiLineCommentStart = parse.String("/*")
-var goMultiLineCommentEnd = parse.String("*/")
+var (
+	goMultiLineCommentStart = parse.String("/*")
+	goMultiLineCommentEnd   = parse.String("*/")
+)
 
-type goMultiLineCommentParser struct {
-}
+type goMultiLineCommentParser struct{}
 
 var goMultiLineComment = goMultiLineCommentParser{}
 


Test patch:
diff --git a/parser/v2/ifexpressionparser_test.go b/parser/v2/ifexpressionparser_test.go
index 1d7b85d..028f326 100644
--- a/parser/v2/ifexpressionparser_test.go
+++ b/parser/v2/ifexpressionparser_test.go
@@ -599,6 +599,56 @@ func TestIfExpression(t *testing.T) {
 				},
 			},
 		},
+		{
+			name: "if: else with comment and indentation",
+			input: `if p.A {
+		// this is a comment
+	} else {
+		{ "B" }
+	}`,
+			expected: &IfExpression{
+				Expression: Expression{
+					Value: `p.A`,
+					Range: Range{
+						From: Position{
+							Index: 3,
+							Line:  0,
+							Col:   3,
+						},
+						To: Position{
+							Index: 6,
+							Line:  0,
+							Col:   6,
+						},
+					},
+				},
+				Then: []Node{
+					&Whitespace{Value: "\t\t"},
+					&GoComment{Contents: " this is a comment", Multiline: false},
+					&Whitespace{Value: "\n\t"},
+				},
+				Else: []Node{
+					&StringExpression{
+						Expression: Expression{
+							Value: `"B"`,
+							Range: Range{
+								From: Position{
+									Index: 46,
+									Line:  3,
+									Col:   4,
+								},
+								To: Position{
+									Index: 49,
+									Line:  3,
+									Col:   7,
+								},
+							},
+						},
+						TrailingSpace: SpaceVertical,
+					},
+				},
+			},
+		},
 	}
 	for _, tt := range tests {
 		tt := tt
diff --git a/parser/v2/templateparser_test.go b/parser/v2/templateparser_test.go
index 6aa60ae..348bd16 100644
--- a/parser/v2/templateparser_test.go
+++ b/parser/v2/templateparser_test.go
@@ -671,6 +671,7 @@ func TestTemplateParser(t *testing.T) {
 				Children: []Node{
 					&Whitespace{Value: "\t"},
 					&GoComment{Contents: " Comment", Multiline: false},
+					&Whitespace{Value: "\n"},
 				},
 			},
 		},