Skip to content

Commit

Permalink
Add newline to body only on well-known error
Browse files Browse the repository at this point in the history
  • Loading branch information
syndicut committed Jan 24, 2021
1 parent 49b8284 commit 9843cc8
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions tflint/client/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,35 @@ func parseExpression(src []byte, filename string, start hcl.Pos) (hcl.Expression
panic(fmt.Sprintf("Unexpected file: %s", filename))
}

func hasUnterminatedTemplateString(diags []*hcl.Diagnostic) bool {
for _, diag := range diags {
if diag.Summary == "Unterminated template string" &&
diag.Detail == "No closing marker was found for the string." {
return true
}
}

return false
}

func parseConfig(src []byte, filename string, start hcl.Pos) (*hcl.File, hcl.Diagnostics) {
if strings.HasSuffix(filename, ".tf") {
// 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.ParseConfig(src, filename, start)
file, diags := hclsyntax.ParseConfig(src, filename, start)
if hasUnterminatedTemplateString(diags) {
// HACK: Add a newline to avoid heredoc parse errors.
// @see https://github.com/hashicorp/hcl/issues/441
src = []byte(string(src) + "\n")
fixedFile, fixedDiags := hclsyntax.ParseConfig(src, filename, start)

// Still has error? Return first result
if hasUnterminatedTemplateString(fixedDiags) {
return file, diags
}

return fixedFile, fixedDiags
}

return file, diags
}

if strings.HasSuffix(filename, ".tf.json") {
Expand Down

0 comments on commit 9843cc8

Please sign in to comment.