Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions fluent-syntax/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export function lineOffset(source: string, pos: number): number {
}

export function columnOffset(source: string, pos: number): number {
// lastIndexOf clamps a negative fromIndex to zero.
if (pos === 0) {
return 0;
}

// Find the last line break starting backwards from the index just before
// pos. This allows us to correctly handle ths case where the character at
// pos is a line break as well.
Expand Down
42 changes: 42 additions & 0 deletions fluent-syntax/test/offset_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import assert from "assert";
import { columnOffset, lineOffset } from "../src/index.ts";

suite("columnOffset", function () {
test("file start before a leading LF", function () {
const source = "\nhello = Hello";
assert.strictEqual(lineOffset(source, 0), 0);
assert.strictEqual(columnOffset(source, 0), 0);
});

test("file start in an LF-only source", function () {
assert.strictEqual(columnOffset("\n", 0), 0);
});

test("file start in an empty source", function () {
assert.strictEqual(columnOffset("", 0), 0);
});

test("file start in a nonempty first line", function () {
assert.strictEqual(columnOffset("hello", 0), 0);
});

test("file start before CRLF", function () {
assert.strictEqual(columnOffset("\r\nhello", 0), 0);
});

test("position on a line break", function () {
assert.strictEqual(columnOffset("hello\nworld", 5), 5);
});

test("position immediately after a line break", function () {
assert.strictEqual(columnOffset("hello\nworld", 6), 0);
});

test("position between consecutive line breaks", function () {
assert.strictEqual(columnOffset("\n\nhello", 1), 0);
});

test("end of the last line", function () {
assert.strictEqual(columnOffset("hello\nworld", 11), 5);
});
});