Skip to content

Commit

Permalink
Fix out of bounds columns on lines
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 15, 2024
1 parent 8ca407a commit 1bddad2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 2 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ export function location(file) {
) {
const offset =
(point.line > 1 ? indices[point.line - 2] : 0) + point.column - 1
// The given `column` could not exist.
if (offset > value.length) return
return offset
// The given `column` could not exist on this line.
if (offset < indices[point.line - 1]) return offset
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,19 @@ test('toOffset(point)', async function (t) {
await t.test(
'should return `undefined` for out of bounds input (#1)',
async function () {
assert.equal(place.toOffset({line: 3, column: 5}), undefined)
assert.equal(place.toOffset({line: 2, column: 5}), undefined)
}
)

await t.test(
'should return `undefined` for out of bounds input (#2)',
async function () {
assert.equal(place.toOffset({line: 3, column: 5}), undefined)
}
)

await t.test(
'should return `undefined` for out of bounds input (#3)',
async function () {
assert.equal(place.toOffset({line: 4, column: 1}), undefined)
}
Expand Down

0 comments on commit 1bddad2

Please sign in to comment.