Skip to content

Commit a3ce77c

Browse files
committed
hclsyntax: Report correct Range.End for FunctionCall w/ incomplete arg
1 parent d180583 commit a3ce77c

File tree

3 files changed

+1134
-1
lines changed

3 files changed

+1134
-1
lines changed

hclsyntax/expression_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package hclsyntax
22

33
import (
4+
"fmt"
45
"testing"
56

7+
"github.com/google/go-cmp/cmp"
68
"github.com/hashicorp/hcl/v2"
79
"github.com/zclconf/go-cty/cty"
810
"github.com/zclconf/go-cty/cty/function"
@@ -2269,3 +2271,77 @@ func TestStaticExpressionList(t *testing.T) {
22692271
t.Fatalf("wrong first value %#v; want cty.Zero", first.Val)
22702272
}
22712273
}
2274+
2275+
// Check that function call w/ incomplete argument still reports correct range
2276+
func TestParseExpression_incompleteFunctionCall(t *testing.T) {
2277+
tests := []struct {
2278+
cfg string
2279+
expectedRange hcl.Range
2280+
}{
2281+
{
2282+
`object({ foo = })`,
2283+
hcl.Range{
2284+
Filename: "test.hcl",
2285+
Start: hcl.InitialPos,
2286+
End: hcl.Pos{Line: 1, Column: 18, Byte: 17},
2287+
},
2288+
},
2289+
{
2290+
`object({
2291+
foo =
2292+
})`,
2293+
hcl.Range{
2294+
Filename: "test.hcl",
2295+
Start: hcl.InitialPos,
2296+
End: hcl.Pos{Line: 3, Column: 3, Byte: 19},
2297+
},
2298+
},
2299+
{
2300+
`object({ foo = }`,
2301+
hcl.Range{
2302+
Filename: "test.hcl",
2303+
Start: hcl.InitialPos,
2304+
End: hcl.Pos{Line: 0, Column: 0, Byte: 0},
2305+
},
2306+
},
2307+
{
2308+
`object({
2309+
foo =
2310+
}`,
2311+
hcl.Range{
2312+
Filename: "test.hcl",
2313+
Start: hcl.InitialPos,
2314+
End: hcl.Pos{Line: 0, Column: 0, Byte: 0},
2315+
},
2316+
},
2317+
{
2318+
`object({
2319+
foo =
2320+
`,
2321+
hcl.Range{
2322+
Filename: "test.hcl",
2323+
Start: hcl.InitialPos,
2324+
End: hcl.Pos{Line: 0, Column: 0, Byte: 0},
2325+
},
2326+
},
2327+
{
2328+
`object({
2329+
foo =
2330+
)`,
2331+
hcl.Range{
2332+
Filename: "test.hcl",
2333+
Start: hcl.InitialPos,
2334+
End: hcl.Pos{Line: 0, Column: 0, Byte: 0},
2335+
},
2336+
},
2337+
}
2338+
2339+
for i, tc := range tests {
2340+
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
2341+
expr, _ := ParseExpression([]byte(tc.cfg), "test.hcl", hcl.InitialPos)
2342+
if diff := cmp.Diff(tc.expectedRange, expr.Range()); diff != "" {
2343+
t.Fatalf("range mismatch: %s", diff)
2344+
}
2345+
})
2346+
}
2347+
}

hclsyntax/parser.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,12 @@ Token:
11741174
// if there was a parse error in the argument then we've
11751175
// probably been left in a weird place in the token stream,
11761176
// so we'll bail out with a partial argument list.
1177-
p.recover(TokenCParen)
1177+
recoveredTok := p.recover(TokenCParen)
1178+
1179+
// record the recovered token, if one was found
1180+
if recoveredTok.Type == TokenCParen {
1181+
closeTok = recoveredTok
1182+
}
11781183
break Token
11791184
}
11801185

0 commit comments

Comments
 (0)