From 44e85c8bd379215f7fb7331e2be12610e50db3c3 Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Mon, 4 Apr 2022 18:32:08 +0900 Subject: [PATCH] Fix suppress an empty token in empty list --- tfwrite/hclwritex.go | 5 ++++- tfwrite/hclwritex_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tfwrite/hclwritex.go b/tfwrite/hclwritex.go index c592031..78157ad 100644 --- a/tfwrite/hclwritex.go +++ b/tfwrite/hclwritex.go @@ -32,7 +32,10 @@ func SplitTokensAsList(tokens hclwrite.Tokens) []hclwrite.Tokens { for ; end < len(tokens); end++ { // Find a `]` token if tokens[end].Type == hclsyntax.TokenCBrack { - ret = append(ret, tokens[begin:end]) + elm := tokens[begin:end] + if len(elm) > 0 { + ret = append(ret, elm) + } foundCBrack = true break } diff --git a/tfwrite/hclwritex_test.go b/tfwrite/hclwritex_test.go index 6019efa..51979c4 100644 --- a/tfwrite/hclwritex_test.go +++ b/tfwrite/hclwritex_test.go @@ -31,6 +31,26 @@ foo { }, ok: true, }, + { + desc: "empty string (invalid list)", + src: ` +foo { + attr = "" +} +`, + want: nil, + ok: true, + }, + { + desc: "empty list", + src: ` +foo { + attr = [] +} +`, + want: []hclwrite.Tokens{}, + ok: true, + }, { desc: "variable", src: `