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

hclwrite: Allow blank quoted string block labels #422

Merged
merged 1 commit into from
Nov 18, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions hclwrite/ast_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ func (bl *blockLabels) Current() []string {
if !diags.HasErrors() {
labelNames = append(labelNames, labelString)
}
} else if len(tokens) == 2 &&
tokens[0].Type == hclsyntax.TokenOQuote &&
tokens[1].Type == hclsyntax.TokenCQuote {
// An open quote followed immediately by a closing quote is a
// valid but unusual blank string label.
labelNames = append(labelNames, "")
}

default:
Expand Down
9 changes: 8 additions & 1 deletion hclwrite/ast_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ escape "\u0041" {
`,
[]string{"\u0041"},
},
{
`
blank "" {
}
`,
[]string{""},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -414,7 +421,7 @@ func TestBlockSetLabels(t *testing.T) {
{
`foo "hoge" /* foo */ "" {}`,
"foo",
[]string{"hoge"},
[]string{"hoge", ""},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this test is covering the parsing of foo "hoge" "" {} as foo "hoge" {}, but it is an example of the bug we are trying to fix with this change, so I've updated the test. @minamijoyo, do you have any concerns about this test update?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alisdair I'm not sure why 🤔
I checked the commit history and found that the test case was added by @apparentlymart
7e5b148

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 Sorry, my mistake! I misread the commit history. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm sorry about that! I imagine I must've misread the input while I was updating this (empty quotes in that position are unusual!) and managed to accidentally write a test that incorrectly passed, rather than a test that caught the bug. 😖

My main goal here was to test the handling of that comment in the middle, so I expect I was focused on it having removed that and not paying attention to how it handled the labels.

[]string{"fuga"}, // force quoted form even if the old one is unquoted.
Tokens{
{
Expand Down