Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions hclext/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,27 @@ 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
src = []byte(string(src) + "\n")
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)
}

return nil, hcl.Diagnostics{
{
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),
},
}
}
24 changes: 22 additions & 2 deletions hclext/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down
Loading