Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func Test_Comment(t *testing.T) {
}
}

func Test_Unterminated_Comment(t *testing.T) {
output, err := plush.Render("<%# unterminated comment", plush.NewContext())
require.EqualError(t, err, "line 1: unterminated comment")
require.Empty(t, output)
}

func Test_Block_Comment(t *testing.T) {
r := require.New(t)
input := map[string]string{
Expand Down
32 changes: 32 additions & 0 deletions parser/comments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package parser_test

import (
"testing"

"github.com/gobuffalo/plush/v5/parser"
"github.com/stretchr/testify/require"
)

func Test_Unterminated_Comment(t *testing.T) {
tests := []struct {
name string
input string
err string
}{
{"empty", "<%#", "line 1: unterminated comment"},
{"text", "<%# comment", "line 1: unterminated comment"},
{"partial closing tag", "<%# comment %", "line 1: unterminated comment"},
{"multiline", "<%# first line\nsecond line", "line 2: unterminated comment"},
{"trailing newline", "<%#\n", "line 2: unterminated comment"},
{"after HTML", "Hello\n<%# comment", "line 2: unterminated comment"},
{"inside code", "<% <%# comment", "line 1: unterminated comment"},
{"after closed comment", "<%# closed %><%#", "line 1: unterminated comment"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := parser.Parse(tt.input)
require.EqualError(t, err, tt.err)
})
}
}
5 changes: 5 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ func (p *parser) parseStringLiteral() ast.Expression {

func (p *parser) parseCommentLiteral() ast.Expression {
for p.curToken.Type != token.E_END {
if p.curTokenIs(token.EOF) {
msg := fmt.Sprintf("line %d: unterminated comment", p.curToken.LineNumber)
p.errors = append(p.errors, msg)
return nil
}
p.nextToken()
}

Expand Down
2 changes: 2 additions & 0 deletions vm/plush/parity_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func Test_Parity_Phase_13_Line_Numbers(t *testing.T) {
}

func Test_Parity_Phase_13_Parser_And_EOF_Syntax_Errors(t *testing.T) {
compareExactRenderError(t, `<%#`, emptyContext)
compareExactRenderError(t, "<%# first line\nsecond line", emptyContext)
compareExactRenderError(t, `<% let = %>`, emptyContext)
compareBothRenderError(t, `<%= if true %>bad<% } %>`, emptyContext)
compareBothRenderError(t, `<%= foo("unterminated) %>`, contextWith(map[string]interface{}{
Expand Down
Loading