In-line comments aren't parsed correctly for top level ObjectItems
- Dominant language
- Go
- Stars
- 5.8k
- Forks
- 657
- Avg merge
- 20h 36m
- Merged PRs (30d)
- 6
Description
### HCL Template
```hcl
resource foo bar { # abcdef
name = "some-name-${var.service_name}-here" # ghijk
}
```
### Expected behavior
After parsing the HCL, I should ideally see the comments on the parsed `ObjectList` first Object Item's LineComment (or elsewhere but it would be nice for the line comment to be on the first ObjectItem). This is the ideal behavior after parsing the above HCL. Take note of the expected behavior marked in the snipped below
```
(*ast.ObjectList)(0xc4200b06a0)({
Items: ([]*ast.ObjectItem) (len=1 cap=1) {
(*ast.ObjectItem)(0xc4200c62a0)({
Keys: ([]*ast.ObjectKey) (len=3 cap=4) {
(*ast.ObjectKey)(0xc4200b4320)({
Token: (token.Token) 1:1 IDENT resource
}),
(*ast.ObjectKey)(0xc4200b4370)({
Token: (token.Token) 1:10 IDENT foo
}),
(*ast.ObjectKey)(0xc4200b43c0)({
Token: (token.Token) 1:14 IDENT bar
})
},
Assign: (token.Pos) -,
Val: (*ast.ObjectType)(0xc4200c6300)({
Lbrace: (token.Pos) 1:18,
Rbrace: (token.Pos) 3:1,
List: (*ast.ObjectList)(0xc4200b06e0)({
Items: ([]*ast.ObjectItem) (len=1 cap=1) {
(*ast.ObjectItem)(0xc4200c6360)({
Keys: ([]*ast.ObjectKey) (len=1 cap=1) {
(*ast.ObjectKey)(0xc4200b4410)({
Token: (token.Token) 2:3 IDENT name
})
},
Assign: (token.Pos) 2:8,
Val: (*ast.LiteralType)(0xc4200c63c0)({
Token: (token.Token) 2:10 STRING "some-name-${var.service_name}-here",
LeadComment: (*ast.CommentGroup)(),
LineComment: (*ast.CommentGroup)()
}),
LeadComment: (*ast.CommentGroup)(),
LineComment: (*ast.CommentGroup)(0xc4200b0720)({
List: ([]*ast.Comment) (len=1 cap=1) {
(*ast.Comment)(0xc4200a4480)({
Start: (token.Pos) 2:47,
Text: (string) (len=7) "# ghijk"
})
}
})
})
}
})
}),
LeadComment: (*ast.CommentGroup)(),
LineComment: (*ast.CommentGroup)(0xc42000c760)({
List: ([]*ast.Comment) (len=1 cap=1) {
(*ast.Comment)(0xc420024500)({ <<<<<<<<<<<<<<< Should not be nil
Start: (token.Pos) 1:20, <<<<<<<<<<<<<<< Should be present on line 1 column 12
Text: (string) (len=8) "# abcdef" <<<<<<<<<<<<<<< Should be present here with length 8 and a value of '# abcdef'
})
}
})
})
}
})
```
### Actual behavior
What actually happened?
The comment does not appear in the AST at all. This is what the ObjectList actually looks like:
```
(*ast.ObjectList)(0xc42000c6e0)({
Items: ([]*ast.ObjectItem) (len=1 cap=1) {
(*ast.ObjectItem)(0xc420076300)({
Keys: ([]*ast.ObjectKey) (len=3 cap=4) {
(*ast.ObjectKey)(0xc4200a2320)({
Token: (token.Token) 1:1 IDENT resource
}),
(*ast.ObjectKey)(0xc4200a2370)({
Token: (token.Token) 1:10 IDENT foo
}),
(*ast.ObjectKey)(0xc4200a23c0)({
Token: (token.Token) 1:14 IDENT bar
})
},
Assign: (token.Pos) -,
Val: (*ast.ObjectType)(0xc420076360)({
Lbrace: (token.Pos) 1:18,
Rbrace: (token.Pos) 3:1,
List: (*ast.ObjectList)(0xc42000c720)({
Items: ([]*ast.ObjectItem) (len=1 cap=1) {
(*ast.ObjectItem)(0xc4200763c0)({
Keys: ([]*ast.ObjectKey) (len=1 cap=1) {
(*ast.ObjectKey)(0xc4200a2410)({
Token: (token.Token) 2:3 IDENT name
})
},
Assign: (token.Pos) 2:8,
Val: (*ast.LiteralType)(0xc420076420)({
Token: (token.Token) 2:10 STRING "some-name-${var.service_name}-here",
LeadComment: (*ast.CommentGroup)(),
LineComment: (*ast.CommentGroup)()
}),
LeadComment: (*ast.CommentGroup)(),
LineComment: (*ast.CommentGroup)(0xc42000c760)({
List: ([]*ast.Comment) (len=1 cap=1) {
(*ast.Comment)(0xc420024500)({
Start: (token.Pos) 2:47,
Text: (string) (len=7) "# ghijk"
})
}
})
})
}
})
}),
LeadComment: (*ast.CommentGroup)(),
LineComment: (*ast.CommentGroup)() <<<<<<<<<<<<<<< Missing comment here.
})
}
})
```
### Steps to reproduce
```go
manfiestContent := `resource foo bar { # abcdef
name = "some-name-${var.service_name}-here" # ghijk
}
`
if astFile, err = hcl.Parse(manifestContent); err != nil {
return nil, fmt.Errorf("error: Decoding file manifest content. Cannot be parsed into valid HCL\n%s\n", err.Error())
}
root, _ := astFile.Node.(*ast.ObjectList)
spew.Dump(root)
```
### References
N/A
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.