Skip to content

columnOffset returns -1 at offset 0 when the source starts with LF #674

Open

Description

@poplolioi11

columnOffset(source, 0) returns -1 when the first character of source is LF, although offset 0 is the start of the first line and should have column offset 0.

Reproduced in the published @fluent/syntax@0.19.0 package and current main (9a925d2a38b893be735ff4429be8ad62132a204d), using Node.js v24.19.0 on macOS.

Minimal reproduction

const { columnOffset, lineOffset } = require("@fluent/syntax");

console.log(lineOffset("\nhello = Hello", 0));   // 0
console.log(columnOffset("\nhello = Hello", 0)); // -1 (expected 0)
console.log(columnOffset("\n", 0));              // -1 (expected 0)

Controls such as columnOffset("hello", 0), columnOffset("\nhello", 1), and columnOffset("\r\nhello", 0) return 0 as expected.

Cause and scope

The helper calls source.lastIndexOf("\n", pos - 1). At pos === 0, JavaScript clamps the search position -1 to 0, so a leading LF is found at index 0 and the final subtraction produces -1.

This gives a negative column to consumers converting a valid file-start offset into a line/column coordinate. I have not established a downstream production failure.

Related history: #64 fixed the search boundary when pos itself points to a newline. This report concerns the remaining file-start boundary. I checked the public issues and PRs, including #4, #7, and #64, and did not find this exact case reported.

Proposed fix and validation

Return 0 immediately when pos === 0. The attached patch includes eight regression/control tests covering the empty source, leading LF, LF-only source, CRLF, a position on a newline, the next line's first character, consecutive newlines, and EOF.

  • Before the fix: 2 regression tests failed and 6 controls passed.
  • After the fix: the full suite passed with 742 passed, 3 skipped across 59 test files.
  • All workspace builds, lint, and documentation generation passed.
  • The built distribution also returns 0 for both failing examples.

The patch applies cleanly to the main commit above. Discovery and test preparation were AI-assisted; the reported results were verified by local execution with synthetic text inputs.

fluent-column-offset-fix.patch.txt

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions