Skip to content

Commit 702731c

Browse files
Fix Update ParseExpression to parse any json passed to it. (#418)
* Parse JSON * Update parse.go Co-authored-by: Kazuma Watanabe <[email protected]> --------- Co-authored-by: Kazuma Watanabe <[email protected]>
1 parent babec0d commit 702731c

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

hclext/parse.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,27 @@ import (
1313
// This function specializes in parsing intermediate expressions in the file,
1414
// so it takes into account the hack on trailing newlines in heredoc.
1515
func ParseExpression(src []byte, filename string, start hcl.Pos) (hcl.Expression, hcl.Diagnostics) {
16+
// Handle HCL files: .tf (Terraform HCL) and .hcl (HCL config like .tflint.hcl)
1617
if strings.HasSuffix(filename, ".tf") || strings.HasSuffix(filename, ".hcl") {
1718
// HACK: Always add a newline to avoid heredoc parse errors.
1819
// @see https://github.com/hashicorp/hcl/issues/441
1920
src = []byte(string(src) + "\n")
2021
return hclsyntax.ParseExpression(src, filename, start)
2122
}
2223

23-
if strings.HasSuffix(filename, ".tf.json") {
24+
// Handle JSON files:
25+
// We accept any .json file (including .tf.json), not just specific ones like .tflint.json.
26+
// The calling functions are responsible for validating that the file should be processed.
27+
// If the content is not valid HCL-compatible JSON, the JSON parser will return appropriate diagnostics.
28+
if strings.HasSuffix(filename, ".json") {
2429
return json.ParseExpressionWithStartPos(src, filename, start)
2530
}
2631

2732
return nil, hcl.Diagnostics{
2833
{
2934
Severity: hcl.DiagError,
3035
Summary: "Unexpected file extension",
31-
Detail: fmt.Sprintf("The file name `%s` is a file with an unexpected extension. Valid extensions are `.tf`, `.tf.json`, and `.hcl`.", filename),
36+
Detail: fmt.Sprintf("The file name `%s` is a file with an unexpected extension. Valid extensions are `.tf`, `.tf.json`, `.hcl`, and `.json`.", filename),
3237
},
3338
}
3439
}

hclext/parse_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,18 @@ func TestParseExpression(t *testing.T) {
3030
DiagCount: 0,
3131
},
3232
{
33-
Name: "HCL but file extension is invalid (*.json)",
33+
Name: "JSON (*.json)",
3434
Source: `"baz"`,
3535
Filename: "test.json",
36-
DiagCount: 1,
36+
Want: `cty.StringVal("baz")`,
37+
DiagCount: 0,
38+
},
39+
{
40+
Name: "JSON (.tflint.json)",
41+
Source: `{"config": {"force": true}}`,
42+
Filename: ".tflint.json",
43+
Want: `cty.ObjectVal(map[string]cty.Value{"config":cty.ObjectVal(map[string]cty.Value{"force":cty.True})})`,
44+
DiagCount: 0,
3745
},
3846
{
3947
Name: "HCL heredoc with trailing newline",
@@ -61,6 +69,18 @@ EOF`,
6169
Want: `cty.ObjectVal(map[string]cty.Value{"baz":cty.NumberIntVal(1), "foo":cty.StringVal("bar")})`,
6270
DiagCount: 0,
6371
},
72+
{
73+
Name: "Invalid JSON content",
74+
Source: `{invalid json content}`,
75+
Filename: "test.json",
76+
DiagCount: 2, // JSON parser returns 2 diagnostics for this invalid JSON
77+
},
78+
{
79+
Name: "Invalid file extension",
80+
Source: `"test"`,
81+
Filename: "test.yaml",
82+
DiagCount: 1,
83+
},
6484
}
6585

6686
for _, test := range tests {

0 commit comments

Comments
 (0)