Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hclsyntax: Report correct Range.End for FunctionCall w/ incomplete arg #588

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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