diff --git a/hclext/parse.go b/hclext/parse.go index 3196394..5c869ae 100644 --- a/hclext/parse.go +++ b/hclext/parse.go @@ -13,6 +13,7 @@ import ( // This function specializes in parsing intermediate expressions in the file, // so it takes into account the hack on trailing newlines in heredoc. func ParseExpression(src []byte, filename string, start hcl.Pos) (hcl.Expression, hcl.Diagnostics) { + // Handle HCL files: .tf (Terraform HCL) and .hcl (HCL config like .tflint.hcl) if strings.HasSuffix(filename, ".tf") || strings.HasSuffix(filename, ".hcl") { // HACK: Always add a newline to avoid heredoc parse errors. // @see https://github.com/hashicorp/hcl/issues/441 @@ -20,7 +21,11 @@ func ParseExpression(src []byte, filename string, start hcl.Pos) (hcl.Expression return hclsyntax.ParseExpression(src, filename, start) } - if strings.HasSuffix(filename, ".tf.json") { + // Handle JSON files: + // We accept any .json file (including .tf.json), not just specific ones like .tflint.json. + // The calling functions are responsible for validating that the file should be processed. + // If the content is not valid HCL-compatible JSON, the JSON parser will return appropriate diagnostics. + if strings.HasSuffix(filename, ".json") { return json.ParseExpressionWithStartPos(src, filename, start) } @@ -28,7 +33,7 @@ func ParseExpression(src []byte, filename string, start hcl.Pos) (hcl.Expression { Severity: hcl.DiagError, Summary: "Unexpected file extension", - Detail: fmt.Sprintf("The file name `%s` is a file with an unexpected extension. Valid extensions are `.tf`, `.tf.json`, and `.hcl`.", filename), + Detail: fmt.Sprintf("The file name `%s` is a file with an unexpected extension. Valid extensions are `.tf`, `.tf.json`, `.hcl`, and `.json`.", filename), }, } } diff --git a/hclext/parse_test.go b/hclext/parse_test.go index d4da165..7a5c6bb 100644 --- a/hclext/parse_test.go +++ b/hclext/parse_test.go @@ -30,10 +30,18 @@ func TestParseExpression(t *testing.T) { DiagCount: 0, }, { - Name: "HCL but file extension is invalid (*.json)", + Name: "JSON (*.json)", Source: `"baz"`, Filename: "test.json", - DiagCount: 1, + Want: `cty.StringVal("baz")`, + DiagCount: 0, + }, + { + Name: "JSON (.tflint.json)", + Source: `{"config": {"force": true}}`, + Filename: ".tflint.json", + Want: `cty.ObjectVal(map[string]cty.Value{"config":cty.ObjectVal(map[string]cty.Value{"force":cty.True})})`, + DiagCount: 0, }, { Name: "HCL heredoc with trailing newline", @@ -61,6 +69,18 @@ EOF`, Want: `cty.ObjectVal(map[string]cty.Value{"baz":cty.NumberIntVal(1), "foo":cty.StringVal("bar")})`, DiagCount: 0, }, + { + Name: "Invalid JSON content", + Source: `{invalid json content}`, + Filename: "test.json", + DiagCount: 2, // JSON parser returns 2 diagnostics for this invalid JSON + }, + { + Name: "Invalid file extension", + Source: `"test"`, + Filename: "test.yaml", + DiagCount: 1, + }, } for _, test := range tests {