Skip to content

fix: compute locations correctly for CRLF line endings - #187

Merged
ota-meshi merged 1 commit into
ota-meshi:masterfrom
simpleqt:fix/crlf-locations
Nov 16, 2026
Merged

ota-meshi merged 1 commit into
ota-meshi:masterfrom
simpleqt:fix/crlf-locations

Conversation

@simpleqt

@simpleqt simpleqt commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

The problem

Locations records only the index of each line terminator and derives columns as
offset - before - 1, which assumes a one-character terminator. A \r\n is two
characters, so every position on a line following a CRLF terminator is reported
one column too far right:

syntax.parse("<style>\n</style>").first.source.start
// { line: 2, column: 1, offset: 8 }  correct

syntax.parse("<style>\r\n</style>").first.source.start
// { line: 2, column: 2, offset: 9 }  column should be 1

getOffsetFromLoc makes the same assumption, so the two directions disagree
with each other and with postcss's own Input#fromOffset, which maps the same
offset to line 2, column 1.

Downstream this surfaces as line endings changing reported positions: with
customSyntax: 'postcss-html', a no-empty-source report for
"<style>\r\n</style>" lands on 2:2-2:3 where the LF document gives 2:1-2:2.

The fix

Keep each terminator's length and derive line starts from it, so the first column
after a terminator is column 1 whatever terminator the document uses. An offset
landing between the \r and the \n of a CRLF pair is reported at the end of
the line that the terminator ends, which keeps positions monotonic.

This also fixes documents using a lone \r, where the old column arithmetic ran
off the end of the line.

Tests

Two regression tests added to test/html.js:

  • reports the same positions for every line terminator — parses the same
    document joined with \n and \r\n and asserts the node positions relative to
    the style block's start are identical.
  • translates between an offset and a location consistently — asserts a CRLF
    document's reported source.start.offset really is the start of </style>,
    and that its location is line 2, column 1.

With the library change reverted and only the tests kept, both fail:

71 passing
2 failing
  1) html tests reports the same positions for every line terminator
  2) html tests translates between an offset and a location consistently

With the fix: 73 passing, and npm run lint passes.

A line terminator is one character for `\n` and `\r`, but two for `\r\n`.
`Locations` recorded only the index of each terminator and then derived
columns as `offset - before - 1`, which assumes a one-character terminator.
Every position on a line after a CRLF terminator was therefore reported one
column too far right:

    syntax.parse("<style>\n</style>").first.source.start    // line 2, column 1
    syntax.parse("<style>\r\n</style>").first.source.start  // line 2, column 2

`getOffsetFromLoc` had the same assumption, and both directions disagreed with
`postcss`'s own `Input#fromOffset` for the same offset.

Keep each terminator's length and derive line starts from it, so the first
column after a terminator is always column 1 regardless of which terminator the
document uses. An offset that falls between the `\r` and the `\n` of a CRLF pair
is reported at the end of the line that the terminator ends, keeping positions
monotonic.

The same change also fixes locations for documents that use a lone `\r`, where
column arithmetic previously ran off the end of the line.
Copilot AI balanced review requested due to automatic review settings November 3, 2026 20:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@ota-meshi ota-meshi left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@ota-meshi
ota-meshi merged commit e705270 into ota-meshi:master Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants