Skip to content

Commit

Permalink
Fix attribute get --with-comments for inline comments
Browse files Browse the repository at this point in the history
Follow-up to #103.
Inline comments belong to attributes, not expressions. When the
`--with-comments` flag is specified, we need to return tokens on the
right side of the equals sign.
  • Loading branch information
minamijoyo committed Aug 30, 2024
1 parent 151373e commit d04d1f2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
14 changes: 7 additions & 7 deletions cmd/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ locals {
}
`,
},
// does not pass at current
// {
// name: "single with comments",
// args: []string{"--with-comments", "locals.attribute"},
// ok: true,
// want: `"foo" #comment`,
// },
{
name: "single with comments",
args: []string{"--with-comments", "locals.attribute"},
ok: true,
want: `"foo" # comment
`,
},
{
name: "single without comments",
args: []string{"locals.attribute"},
Expand Down
21 changes: 17 additions & 4 deletions editor/sink_attribute_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,26 @@ func longestMatchingLabels(labels []string, prefix []string) []string {
// There is no way to get value as string directly,
// so we parses tokens of Attribute and build string representation.
func GetAttributeValueAsString(attr *hclwrite.Attribute, withComments bool) (string, error) {
// find TokenEqual
expr := attr.Expr()
exprTokens := expr.BuildTokens(nil)
var rhsTokens hclwrite.Tokens
if withComments {
// Inline comments belong to an attribute, not an expression.
attrTokens := attr.BuildTokens(nil)
for i, t := range attrTokens {
// find TokenEqual
if t.Type != hclsyntax.TokenEqual {
continue
}
rhsTokens = attrTokens[i+1:]
break
}
} else {
expr := attr.Expr()
rhsTokens = expr.BuildTokens(nil)
}

// append tokens until find TokenComment
var valueTokens hclwrite.Tokens
for _, t := range exprTokens {
for _, t := range rhsTokens {
if t.Type == hclsyntax.TokenComment && !withComments {
t.Bytes = []byte("\n")
t.SpacesBefore = 0
Expand Down
44 changes: 42 additions & 2 deletions editor/sink_attribute_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ a1 = v1
want: "",
},
{
name: "attribute with comments",
name: "attribute without comments",
src: `
// attr comment
a0 = v0 // inline comment
Expand All @@ -60,7 +60,19 @@ a1 = v1
want: "v0\n",
},
{
name: "multiline attribute with comments",
name: "attribute with comments",
src: `
// attr comment
a0 = v0 // inline comment
a1 = v1
`,
address: "a0",
withComments: true,
ok: true,
want: "v0 // inline comment\n",
},
{
name: "multiline attribute without comments",
src: `
// attr comment
a0 = v0
Expand All @@ -85,6 +97,34 @@ a2 = v2
"val5",
]
`,
},
{
name: "multiline attribute with comments",
src: `
// attr comment
a0 = v0
a1 = [
"val1",
"val2", // inline comment
"val3", # another comment
"val4",
# a ocmment line
"val5",
]
a2 = v2
`,
address: "a1",
withComments: true,
ok: true,
want: `[
"val1",
"val2", // inline comment
"val3", # another comment
"val4",
# a ocmment line
"val5",
]
`,
},
{
Expand Down

0 comments on commit d04d1f2

Please sign in to comment.