hclwrite: Unquoted label should be parsed as *identifier

This commit is contained in:
Masayuki Morita 2019-09-10 05:16:13 +09:00 committed by Martin Atkins
parent 0c888d1241
commit 388af45637
2 changed files with 84 additions and 1 deletions

View File

@ -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)
}

View File

@ -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{