Skip to content

Commit c9bd939

Browse files
authored
fix: comments don't eat newlines (#1186)
1 parent 15199ff commit c9bd939

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

parser/v2/gocommentparser.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"github.com/a-h/parse"
55
)
66

7-
var goSingleLineCommentStart = parse.String("//")
8-
var goSingleLineCommentEnd = parse.Any(parse.NewLine, parse.EOF[string]())
7+
var (
8+
goSingleLineCommentStart = parse.String("//")
9+
goSingleLineCommentEnd = parse.Any(parse.NewLine, parse.EOF[string]())
10+
)
911

10-
type goSingleLineCommentParser struct {
11-
}
12+
type goSingleLineCommentParser struct{}
1213

1314
var goSingleLineComment = goSingleLineCommentParser{}
1415

@@ -24,18 +25,17 @@ func (p goSingleLineCommentParser) Parse(pi *parse.Input) (n Node, ok bool, err
2425
err = parse.Error("expected end comment literal '\n' not found", pi.Position())
2526
return
2627
}
27-
// Move past the end element.
28-
_, _, _ = goSingleLineCommentEnd.Parse(pi)
2928
// Return the comment.
3029
c.Multiline = false
3130
return c, true, nil
3231
}
3332

34-
var goMultiLineCommentStart = parse.String("/*")
35-
var goMultiLineCommentEnd = parse.String("*/")
33+
var (
34+
goMultiLineCommentStart = parse.String("/*")
35+
goMultiLineCommentEnd = parse.String("*/")
36+
)
3637

37-
type goMultiLineCommentParser struct {
38-
}
38+
type goMultiLineCommentParser struct{}
3939

4040
var goMultiLineComment = goMultiLineCommentParser{}
4141

parser/v2/ifexpressionparser_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,56 @@ func TestIfExpression(t *testing.T) {
599599
},
600600
},
601601
},
602+
{
603+
name: "if: else with comment and indentation",
604+
input: `if p.A {
605+
// this is a comment
606+
} else {
607+
{ "B" }
608+
}`,
609+
expected: &IfExpression{
610+
Expression: Expression{
611+
Value: `p.A`,
612+
Range: Range{
613+
From: Position{
614+
Index: 3,
615+
Line: 0,
616+
Col: 3,
617+
},
618+
To: Position{
619+
Index: 6,
620+
Line: 0,
621+
Col: 6,
622+
},
623+
},
624+
},
625+
Then: []Node{
626+
&Whitespace{Value: "\t\t"},
627+
&GoComment{Contents: " this is a comment", Multiline: false},
628+
&Whitespace{Value: "\n\t"},
629+
},
630+
Else: []Node{
631+
&StringExpression{
632+
Expression: Expression{
633+
Value: `"B"`,
634+
Range: Range{
635+
From: Position{
636+
Index: 46,
637+
Line: 3,
638+
Col: 4,
639+
},
640+
To: Position{
641+
Index: 49,
642+
Line: 3,
643+
Col: 7,
644+
},
645+
},
646+
},
647+
TrailingSpace: SpaceVertical,
648+
},
649+
},
650+
},
651+
},
602652
}
603653
for _, tt := range tests {
604654
tt := tt

parser/v2/templateparser_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ func TestTemplateParser(t *testing.T) {
671671
Children: []Node{
672672
&Whitespace{Value: "\t"},
673673
&GoComment{Contents: " Comment", Multiline: false},
674+
&Whitespace{Value: "\n"},
674675
},
675676
},
676677
},

0 commit comments

Comments
 (0)