Skip to content

Commit

Permalink
Merge pull request #588 from hashicorp/b-fix-func-closetok-parsing
Browse files Browse the repository at this point in the history
hclsyntax: Report correct Range.End for FunctionCall w/ incomplete arg
  • Loading branch information
radeksimko authored Feb 10, 2023
2 parents d180583 + a3ce77c commit 67041cc
Show file tree
Hide file tree
Showing 3 changed files with 1,134 additions and 1 deletion.
76 changes: 76 additions & 0 deletions hclsyntax/expression_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package hclsyntax

import (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
Expand Down Expand Up @@ -2269,3 +2271,77 @@ func TestStaticExpressionList(t *testing.T) {
t.Fatalf("wrong first value %#v; want cty.Zero", first.Val)
}
}

// Check that function call w/ incomplete argument still reports correct range
func TestParseExpression_incompleteFunctionCall(t *testing.T) {
tests := []struct {
cfg string
expectedRange hcl.Range
}{
{
`object({ foo = })`,
hcl.Range{
Filename: "test.hcl",
Start: hcl.InitialPos,
End: hcl.Pos{Line: 1, Column: 18, Byte: 17},
},
},
{
`object({
foo =
})`,
hcl.Range{
Filename: "test.hcl",
Start: hcl.InitialPos,
End: hcl.Pos{Line: 3, Column: 3, Byte: 19},
},
},
{
`object({ foo = }`,
hcl.Range{
Filename: "test.hcl",
Start: hcl.InitialPos,
End: hcl.Pos{Line: 0, Column: 0, Byte: 0},
},
},
{
`object({
foo =
}`,
hcl.Range{
Filename: "test.hcl",
Start: hcl.InitialPos,
End: hcl.Pos{Line: 0, Column: 0, Byte: 0},
},
},
{
`object({
foo =
`,
hcl.Range{
Filename: "test.hcl",
Start: hcl.InitialPos,
End: hcl.Pos{Line: 0, Column: 0, Byte: 0},
},
},
{
`object({
foo =
)`,
hcl.Range{
Filename: "test.hcl",
Start: hcl.InitialPos,
End: hcl.Pos{Line: 0, Column: 0, Byte: 0},
},
},
}

for i, tc := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
expr, _ := ParseExpression([]byte(tc.cfg), "test.hcl", hcl.InitialPos)
if diff := cmp.Diff(tc.expectedRange, expr.Range()); diff != "" {
t.Fatalf("range mismatch: %s", diff)
}
})
}
}
7 changes: 6 additions & 1 deletion hclsyntax/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,12 @@ Token:
// if there was a parse error in the argument then we've
// probably been left in a weird place in the token stream,
// so we'll bail out with a partial argument list.
p.recover(TokenCParen)
recoveredTok := p.recover(TokenCParen)

// record the recovered token, if one was found
if recoveredTok.Type == TokenCParen {
closeTok = recoveredTok
}
break Token
}

Expand Down
Loading

0 comments on commit 67041cc

Please sign in to comment.