Skip to content

Commit 211912f

Browse files
knudttya-h
andauthoredAug 18, 2024··
fix: support comments at end of raw go expressions (#871)
Co-authored-by: Adrian Hesketh <adrianhesketh@hushmail.com>
1 parent 65c2618 commit 211912f

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed
 

‎.version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.759
1+
0.2.763

‎parser/v2/gocodeparser_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,32 @@ func TestGoCodeParser(t *testing.T) {
9191
Multiline: true,
9292
},
9393
},
94+
{
95+
name: "comments in expression",
96+
input: `{{
97+
one := "one"
98+
two := "two"
99+
// Comment in middle of expression.
100+
four := "four"
101+
// Comment at end of expression.
102+
}}`,
103+
expected: GoCode{
104+
Expression: Expression{
105+
Value: `
106+
one := "one"
107+
two := "two"
108+
// Comment in middle of expression.
109+
four := "four"
110+
// Comment at end of expression.`,
111+
Range: Range{
112+
From: Position{Index: 2, Line: 0, Col: 2},
113+
To: Position{Index: 117, Line: 5, Col: 33},
114+
},
115+
},
116+
TrailingSpace: SpaceNone,
117+
Multiline: true,
118+
},
119+
},
94120
}
95121
for _, tt := range tests {
96122
tt := tt

‎parser/v2/goexpression/parse.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func TemplExpression(src string) (start, end int, err error) {
112112
errorHandler := func(pos token.Position, msg string) {
113113
err = fmt.Errorf("error parsing expression: %v", msg)
114114
}
115-
s.Init(file, []byte(src), errorHandler, 0)
115+
s.Init(file, []byte(src), errorHandler, scanner.ScanComments)
116116

117117
// Read chains of identifiers, e.g.:
118118
// components.Variable
@@ -140,7 +140,7 @@ func Expression(src string) (start, end int, err error) {
140140
errorHandler := func(pos token.Position, msg string) {
141141
err = fmt.Errorf("error parsing expression: %v", msg)
142142
}
143-
s.Init(file, []byte(src), errorHandler, 0)
143+
s.Init(file, []byte(src), errorHandler, scanner.ScanComments)
144144

145145
// Read chains of identifiers and constants up until RBRACE, e.g.:
146146
// true
@@ -183,6 +183,8 @@ loop:
183183
end = int(pos) + len(lit) - 1
184184
case token.SEMICOLON:
185185
continue
186+
case token.COMMENT:
187+
end = int(pos) + len(lit) - 1
186188
case token.ILLEGAL:
187189
return 0, 0, fmt.Errorf("illegal token: %v", lit)
188190
default:

0 commit comments

Comments
 (0)
Please sign in to comment.