Skip to content

Commit

Permalink
fix: Prevent parsing invalid tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jul 16, 2020
1 parent 0503882 commit 08227a7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
19 changes: 9 additions & 10 deletions internal/hcl/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,22 @@ func (f *file) parse() (*parsedFile, error) {
return f.pf, nil
}

var parseDiags hcl.Diagnostics

tokens, diags := hclsyntax.LexConfig(f.content, f.filename, hcl.InitialPos)
if diags.HasErrors() {
parseDiags = append(parseDiags, diags...)
// The hclsyntax parser assumes all tokens are valid
// so we return early here
// TODO: Avoid ignoring TokenQuotedNewline to provide completion in unclosed string
return nil, diags
}

body, diags := hclsyntax.ParseBodyFromTokens(tokens, hclsyntax.TokenEOF)
if diags.HasErrors() {
parseDiags = append(parseDiags, diags...)
}
body, _ := hclsyntax.ParseBodyFromTokens(tokens, hclsyntax.TokenEOF)

f.pf = &parsedFile{
Tokens: tokens,
Body: body,
}

if parseDiags.HasErrors() {
return f.pf, parseDiags
}
return f.pf, nil
}

Expand All @@ -94,7 +90,10 @@ func (f *file) PosInBlock(pos hcl.Pos) bool {
}

func (f *file) BlockAtPosition(pos hcl.Pos) (TokenizedBlock, error) {
pf, _ := f.parse()
pf, err := f.parse()
if err != nil {
return nil, err
}

body, ok := pf.Body.(*hclsyntax.Body)
if !ok {
Expand Down
22 changes: 22 additions & 0 deletions internal/hcl/hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ func TestFile_BlockAtPosition(t *testing.T) {
},
},
},
{
"invalid tokens",
`provider "aws" {
arg = "
}`,
hcl.Pos{
Line: 2,
Column: 10,
Byte: 26,
},
hcl.Diagnostics{
&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid multi-line string",
Detail: `Quoted strings may not be split over multiple lines. ` +
`To produce a multi-line string, either use the \n escape to ` +
`represent a newline character or use the "heredoc" ` +
`multi-line template syntax.`,
},
},
[]hclsyntax.Token{},
},
{
"valid config and position",
`provider "aws" {
Expand Down
1 change: 1 addition & 0 deletions internal/terraform/lang/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func TestParser_ParseBlockFromTokens(t *testing.T) {
}

func newTestBlock(t *testing.T, src string) ihcl.TokenizedBlock {
t.Helper()
b, err := ihcl.NewTestBlock([]byte(src))
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 08227a7

Please sign in to comment.