Skip to content

Commit 1bddad2

Browse files
committed
Fix out of bounds columns on lines
1 parent 8ca407a commit 1bddad2

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

lib/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ export function location(file) {
6565
) {
6666
const offset =
6767
(point.line > 1 ? indices[point.line - 2] : 0) + point.column - 1
68-
// The given `column` could not exist.
69-
if (offset > value.length) return
70-
return offset
68+
// The given `column` could not exist on this line.
69+
if (offset < indices[point.line - 1]) return offset
7170
}
7271
}
7372
}

test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,19 @@ test('toOffset(point)', async function (t) {
6161
await t.test(
6262
'should return `undefined` for out of bounds input (#1)',
6363
async function () {
64-
assert.equal(place.toOffset({line: 3, column: 5}), undefined)
64+
assert.equal(place.toOffset({line: 2, column: 5}), undefined)
6565
}
6666
)
6767

6868
await t.test(
6969
'should return `undefined` for out of bounds input (#2)',
70+
async function () {
71+
assert.equal(place.toOffset({line: 3, column: 5}), undefined)
72+
}
73+
)
74+
75+
await t.test(
76+
'should return `undefined` for out of bounds input (#3)',
7077
async function () {
7178
assert.equal(place.toOffset({line: 4, column: 1}), undefined)
7279
}

0 commit comments

Comments
 (0)