Compare commits

..

1 Commits

Author SHA1 Message Date
Cursor Agent d15f1d131f fix parser error highlight offsets for non-suffix slices
Co-authored-by: Thomas Pelletier <thomas@pelletier.dev>
2026-04-12 12:26:40 +00:00
2 changed files with 32 additions and 70 deletions
+21 -67
View File
@@ -286,6 +286,27 @@ func TestDecodeError_Position(t *testing.T) {
} }
} }
func TestDecodeError_InvalidKeyStartAfterComment(t *testing.T) {
doc := "# comment\n= \"value\""
var out map[string]string
err := Unmarshal([]byte(doc), &out)
assert.Error(t, err)
var derr *DecodeError
if !errors.As(err, &derr) {
t.Fatal("error not in expected format")
}
row, col := derr.Position()
assert.Equal(t, 2, row)
assert.Equal(t, 1, col)
assert.Equal(t, "toml: invalid character at start of key: =", derr.Error())
assert.Equal(t, `1| # comment
2| = "value"
| ~ invalid character at start of key: =`, derr.String())
}
func TestStrictErrorUnwrap(t *testing.T) { func TestStrictErrorUnwrap(t *testing.T) {
fo := bytes.NewBufferString(` fo := bytes.NewBufferString(`
Missing = 1 Missing = 1
@@ -301,73 +322,6 @@ OtherMissing = 1
assert.Equal(t, 2, len(strictErr.Unwrap())) assert.Equal(t, 2, len(strictErr.Unwrap()))
} }
func TestDecodeError_PositionAfterComment(t *testing.T) {
// Regression test for https://github.com/pelletier/go-toml/issues/1047
// Error positions must be correct when the error occurs after comments or
// other content that was already scanned past.
examples := []struct {
desc string
doc string
expectedRow int
expectedCol int
expectedStr string
}{
{
desc: "invalid key after comment",
doc: "# comment\n= \"value\"",
expectedRow: 2,
expectedCol: 1,
expectedStr: "1| # comment\n2| = \"value\"\n | ~ invalid character at start of key: =",
},
{
desc: "invalid key after two comments",
doc: "# one\n# two\n= \"value\"",
expectedRow: 3,
expectedCol: 1,
expectedStr: "1| # one\n2| # two\n3| = \"value\"\n | ~ invalid character at start of key: =",
},
{
desc: "invalid key after key-value pair",
doc: "a = 1\n= 2",
expectedRow: 2,
expectedCol: 1,
expectedStr: "1| a = 1\n2| = 2\n | ~ invalid character at start of key: =",
},
{
desc: "invalid key after blank line",
doc: "a = 1\n\n= 2",
expectedRow: 3,
expectedCol: 1,
expectedStr: "1| a = 1\n2|\n3| = 2\n | ~ invalid character at start of key: =",
},
}
for _, e := range examples {
t.Run(e.desc, func(t *testing.T) {
var v interface{}
err := Unmarshal([]byte(e.doc), &v)
if err == nil {
t.Fatal("expected an error")
}
var derr *DecodeError
if !errors.As(err, &derr) {
t.Fatalf("error not a *DecodeError: %T: %v", err, err)
}
row, col := derr.Position()
if row != e.expectedRow {
t.Errorf("row: got %d, want %d (error: %s)", row, e.expectedRow, derr.String())
}
if col != e.expectedCol {
t.Errorf("col: got %d, want %d (error: %s)", col, e.expectedCol, derr.String())
}
assert.Equal(t, e.expectedStr, derr.String())
})
}
}
func ExampleDecodeError() { func ExampleDecodeError() {
doc := `name = 123__456` doc := `name = 123__456`
+11 -3
View File
@@ -84,14 +84,22 @@ func (p *Parser) rangeOfToken(token, rest []byte) Range {
} }
// subsliceOffset returns the byte offset of subslice b within p.data. // subsliceOffset returns the byte offset of subslice b within p.data.
// b must be a subslice of p.data (sharing the same backing array). // b must share the same backing array as p.data.
func (p *Parser) subsliceOffset(b []byte) int { func (p *Parser) subsliceOffset(b []byte) int {
if len(b) == 0 { if len(b) == 0 {
return 0 // Most callers pass suffix slices, so preserve EOF behavior.
return len(p.data)
} }
dataPtr := reflect.ValueOf(p.data).Pointer() dataPtr := reflect.ValueOf(p.data).Pointer()
subPtr := reflect.ValueOf(b).Pointer() subPtr := reflect.ValueOf(b).Pointer()
return int(subPtr - dataPtr)
offset := int(subPtr - dataPtr)
if offset < 0 || offset+len(b) > len(p.data) {
panic("subslice is not within parser input")
}
return offset
} }
// Raw returns the slice corresponding to the bytes in the given range. // Raw returns the slice corresponding to the bytes in the given range.