From 388af45637a30350df8cc3df86f410b74a2a6acf Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Tue, 10 Sep 2019 05:16:13 +0900 Subject: [PATCH] hclwrite: Unquoted label should be parsed as *identifier --- hclwrite/parser.go | 7 +++- hclwrite/parser_test.go | 78 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) 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 @@ -224,6 +224,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{