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 b7d6662
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 @@ -62,12 +62,35 @@ func decodeBlock(block *Block) (*hcl.Block, hcl.Diagnostics) {
}, nil
}

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 parseExpression(src []byte, filename string, start hcl.Pos) (hcl.Expression, 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.ParseExpression(src, filename, start)
expr, diags := hclsyntax.ParseExpression(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")
fixedExpr, fixedDiags := hclsyntax.ParseExpression(src, filename, start)

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

return fixedExpr, fixedDiags
}

return expr, diags
}

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

0 comments on commit b7d6662

Please sign in to comment.