diff options
| author | Masayuki Morita <minamijoyo@gmail.com> | 2019-09-10 05:16:13 +0900 |
|---|---|---|
| committer | Martin Atkins <mart@degeneration.co.uk> | 2019-09-09 13:16:13 -0700 |
| commit | 388af45637a30350df8cc3df86f410b74a2a6acf (patch) | |
| tree | 66b3f75d8e7c19b13590233e95955ceae0e7d1aa /hclwrite | |
| parent | 0c888d1241f6523caa75fd64dfcf8d3b610c77f9 (diff) | |
hclwrite: Unquoted label should be parsed as *identifier
Diffstat (limited to 'hclwrite')
| -rw-r--r-- | hclwrite/parser.go | 7 | ||||
| -rw-r--r-- | hclwrite/parser_test.go | 78 |
2 files changed, 84 insertions, 1 deletions
diff --git a/hclwrite/parser.go b/hclwrite/parser.go index 1876818..0e8952d 100644 --- a/hclwrite/parser.go +++ b/hclwrite/parser.go @@ -307,7 +307,12 @@ func parseBlock(nativeBlock *hclsyntax.Block, from, leadComments, lineComments, before, labelTokens, from = from.Partition(rng) children.AppendUnstructuredTokens(before.Tokens()) tokens := labelTokens.Tokens() - ln := newNode(newQuoted(tokens)) + var ln *node + if len(tokens) == 1 && tokens[0].Type == hclsyntax.TokenIdent { + ln = newNode(newIdentifier(tokens[0])) + } else { + ln = newNode(newQuoted(tokens)) + } block.labels.Add(ln) children.AppendNode(ln) } diff --git a/hclwrite/parser_test.go b/hclwrite/parser_test.go index e8f8fc9..f40b1e4 100644 --- a/hclwrite/parser_test.go +++ b/hclwrite/parser_test.go @@ -225,6 +225,84 @@ func TestParse(t *testing.T) { }, }, { + "b label {}\n", + TestTreeNode{ + Type: "Body", + Children: []TestTreeNode{ + { + Type: "Block", + Children: []TestTreeNode{ + { + Type: "comments", + }, + { + Type: "identifier", + Val: "b", + }, + { + Type: "identifier", + Val: ` label`, + }, + { + Type: "Tokens", + Val: " {", + }, + { + Type: "Body", + }, + { + Type: "Tokens", + Val: "}", + }, + { + Type: "Tokens", + Val: "\n", + }, + }, + }, + }, + }, + }, + { + "b \"label\" {}\n", + TestTreeNode{ + Type: "Body", + Children: []TestTreeNode{ + { + Type: "Block", + Children: []TestTreeNode{ + { + Type: "comments", + }, + { + Type: "identifier", + Val: "b", + }, + { + Type: "quoted", + Val: ` "label"`, + }, + { + Type: "Tokens", + Val: " {", + }, + { + Type: "Body", + }, + { + Type: "Tokens", + Val: "}", + }, + { + Type: "Tokens", + Val: "\n", + }, + }, + }, + }, + }, + }, + { "b {\n a = 1\n}\n", TestTreeNode{ Type: "Body", |
