Skip to content

Commit ab5a103

Browse files
committed
Fix error position reporting in multiline script
1 parent bbcbc53 commit ab5a103

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

expr_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,28 @@ func TestCompile_exposed_error(t *testing.T) {
17371737
)
17381738
}
17391739

1740+
func TestCompile_multiline_error_in_the_middle(t *testing.T) {
1741+
_, err := expr.Compile("{\n\ta: 1,\n\tb: #,\n\tc: 3,\n}")
1742+
require.Error(t, err)
1743+
1744+
fileError, ok := err.(*file.Error)
1745+
require.True(t, ok, "error should be of type *file.Error")
1746+
require.Equal(t, "unexpected token Operator(\"#\") (3:5)\n | b: #,\n | ....^", fileError.Error())
1747+
require.Equal(t, 4, fileError.Column)
1748+
require.Equal(t, 3, fileError.Line)
1749+
}
1750+
1751+
func TestCompile_multiline_error_at_the_end(t *testing.T) {
1752+
_, err := expr.Compile("{\n\ta: 1,\n\tb: 2,\n\tc: 3,\n}#")
1753+
require.Error(t, err)
1754+
1755+
fileError, ok := err.(*file.Error)
1756+
require.True(t, ok, "error should be of type *file.Error")
1757+
require.Equal(t, "unexpected token Operator(\"#\") (5:2)\n | }#\n | .^", fileError.Error())
1758+
require.Equal(t, 1, fileError.Column)
1759+
require.Equal(t, 5, fileError.Line)
1760+
}
1761+
17401762
func TestAsBool_exposed_error(t *testing.T) {
17411763
_, err := expr.Compile(`42`, expr.AsBool())
17421764
require.Error(t, err)

file/error.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ func (e *Error) Bind(source Source) *Error {
3131
break
3232
}
3333
if r == '\n' {
34-
lineStart = i
34+
lineStart = i + 1
3535
e.Line++
3636
e.Column = 0
37+
} else {
38+
e.Column++
3739
}
3840
runeCount++
39-
e.Column++
4041
}
4142

4243
lineEnd := lineStart + strings.IndexByte(src[lineStart:], '\n')

0 commit comments

Comments
 (0)