Skip to content

Commit

Permalink
fix: Prevent parsing invalid tokens (hashicorp#236)
Browse files Browse the repository at this point in the history
* fix: Prevent parsing invalid tokens

* go mod tidy
  • Loading branch information
radeksimko authored Jul 16, 2020
1 parent 0503882 commit dd89629
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/zclconf/go-cty v1.2.1
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
golang.org/x/text v0.3.2
)

replace github.com/sourcegraph/go-lsp => github.com/radeksimko/go-lsp v0.1.0
20 changes: 9 additions & 11 deletions internal/hcl/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,21 @@ 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 +89,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 dd89629

Please sign in to comment.