diff options
| author | Alisdair McDiarmid <alisdair@users.noreply.github.com> | 2020-11-18 09:21:23 -0500 |
|---|---|---|
| committer | Alisdair McDiarmid <alisdair@users.noreply.github.com> | 2020-11-18 09:21:23 -0500 |
| commit | 1818f360948ea1eda51cee0d5117873a4aba4e92 (patch) | |
| tree | e1f5515a0430f0b35604ca9214b4edcb6faa3cf9 /hclwrite | |
| parent | 3de61ecba2981c715584071070bd18723cf7299d (diff) | |
hclwrite: Allow blank quoted string block labels
The hclsyntax package permits block labels to be blank quoted strings,
and defers to the application to determine if this is valid or not. This
commit updates hclwrite to allow the same behaviour.
This is in response to an upstream bug report on Terraform, which uses
hclwrite to implement its fmt subcommand. Given a block with a blank
quoted string label, it currently deletes the label when formatting.
This is inconsistent with Terraform's behaviour when parsing HCL, which
treats empty labels differently from missing labels.
Diffstat (limited to 'hclwrite')
| -rw-r--r-- | hclwrite/ast_block.go | 6 | ||||
| -rw-r--r-- | hclwrite/ast_block_test.go | 9 |
2 files changed, 14 insertions, 1 deletions
diff --git a/hclwrite/ast_block.go b/hclwrite/ast_block.go index 2bcda5b..edabb26 100644 --- a/hclwrite/ast_block.go +++ b/hclwrite/ast_block.go @@ -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: diff --git a/hclwrite/ast_block_test.go b/hclwrite/ast_block_test.go index 4ca5cb2..8129040 100644 --- a/hclwrite/ast_block_test.go +++ b/hclwrite/ast_block_test.go @@ -107,6 +107,13 @@ escape "\u0041" { `, []string{"\u0041"}, }, + { + ` +blank "" { +} +`, + []string{""}, + }, } for _, test := range tests { @@ -414,7 +421,7 @@ func TestBlockSetLabels(t *testing.T) { { `foo "hoge" /* foo */ "" {}`, "foo", - []string{"hoge"}, + []string{"hoge", ""}, []string{"fuga"}, // force quoted form even if the old one is unquoted. Tokens{ { |
