Skip to content

Commit 653ccfb

Browse files
authored
Merge pull request #246 from octo/zero-length-heredoc-anchor
scanner: Fix detection of zero-length heredoc anchor.
2 parents 061bf37 + a68b5db commit 653ccfb

File tree

3 files changed

+5
-1
lines changed

3 files changed

+5
-1
lines changed

hcl/printer/printer_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ func TestFormatValidOutput(t *testing.T) {
155155
"#\ue123t",
156156
"Y=<<4\n4/\n\n\n/4/@=4/\n\n\n/4000000004\r\r\n00004\n",
157157
"x=<<_\n_\r\r\n_\n",
158+
"X=<<-\n\r\r\n",
158159
}
159160

160161
for _, c := range cases {

hcl/scanner/scanner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ func (s *Scanner) scanHeredoc() {
433433

434434
// Read the identifier
435435
identBytes := s.src[offs : s.srcPos.Offset-s.lastCharLen]
436-
if len(identBytes) == 0 {
436+
if len(identBytes) == 0 || (len(identBytes) == 1 && identBytes[0] == '-') {
437437
s.err("zero-length heredoc anchor")
438438
return
439439
}

hcl/scanner/scanner_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ func TestError(t *testing.T) {
531531
testError(t, `"${abc`+"\n", "2:1", "literal not terminated", token.STRING)
532532
testError(t, `/*/`, "1:4", "comment not terminated", token.COMMENT)
533533
testError(t, `/foo`, "1:1", "expected '/' for comment", token.COMMENT)
534+
535+
testError(t, "<<\nfoo\n\n", "1:3", "zero-length heredoc anchor", token.HEREDOC)
536+
testError(t, "<<-\nfoo\n\n", "1:4", "zero-length heredoc anchor", token.HEREDOC)
534537
}
535538

536539
func testError(t *testing.T, src, pos, msg string, tok token.Type) {

0 commit comments

Comments
 (0)