Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

printer: Fix handling of line comments in multi-line statements. #244

Merged
merged 2 commits into from
Apr 4, 2018
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 hcl/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ func (p *Parser) objectItem() (*ast.ObjectItem, error) {
}
}

// key=#comment
// val
if p.lineComment != nil {
o.LineComment, p.lineComment = p.lineComment, nil
}

// do a look-ahead for line comment
p.scan()
if len(keys) > 0 && o.Val.Pos().Line == keys[0].Pos().Line && p.lineComment != nil {
Expand Down
10 changes: 9 additions & 1 deletion hcl/printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ func (p *printer) objectItem(o *ast.ObjectItem) []byte {
}
}

// If key and val are on different lines, treat line comments like lead comments.
if o.LineComment != nil && o.Val.Pos().Line != o.Keys[0].Pos().Line {
for _, comment := range o.LineComment.List {
buf.WriteString(comment.Text)
buf.WriteByte(newline)
}
}

for i, k := range o.Keys {
buf.WriteString(k.Token.Text)
buf.WriteByte(blank)
Expand All @@ -265,7 +273,7 @@ func (p *printer) objectItem(o *ast.ObjectItem) []byte {

buf.Write(p.output(o.Val))

if o.Val.Pos().Line == o.Keys[0].Pos().Line && o.LineComment != nil {
if o.LineComment != nil && o.Val.Pos().Line == o.Keys[0].Pos().Line {
buf.WriteByte(blank)
for _, comment := range o.LineComment.List {
buf.WriteString(comment.Text)
Expand Down
1 change: 1 addition & 0 deletions hcl/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func TestFormatValidOutput(t *testing.T) {
cases := []string{
"#\x00",
"#\ue123t",
"x=//\n0y=<<_\n_\n",
"Y=<<4\n4/\n\n\n/4/@=4/\n\n\n/4000000004\r\r\n00004\n",
"x=<<_\n_\r\r\n_\n",
}
Expand Down
3 changes: 3 additions & 0 deletions hcl/printer/testdata/comment.golden
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ variable = {
foo {
bar = "fatih" // line comment 2
} // line comment 3

// comment
multiline = "assignment"
2 changes: 2 additions & 0 deletions hcl/printer/testdata/comment.input
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ foo {
bar = "fatih" // line comment 2
} // line comment 3

multiline = // comment
"assignment"
2 changes: 2 additions & 0 deletions hcl/printer/testdata/comment_crlf.input
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ foo {
bar = "fatih" // line comment 2
} // line comment 3

multiline = // comment
"assignment"