Repository navigation
fix: compute locations correctly for CRLF line endings - #187
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
Locationsrecords only the index of each line terminator and derives columns asoffset - before - 1, which assumes a one-character terminator. A\r\nis twocharacters, so every position on a line following a CRLF terminator is reported
one column too far right:
getOffsetFromLocmakes the same assumption, so the two directions disagreewith each other and with postcss's own
Input#fromOffset, which maps the sameoffset to line 2, column 1.
Downstream this surfaces as line endings changing reported positions: with
customSyntax: 'postcss-html', ano-empty-sourcereport for"<style>\r\n</style>"lands on2:2-2:3where the LF document gives2: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
\rand the\nof a CRLF pair is reported at the end ofthe line that the terminator ends, which keeps positions monotonic.
This also fixes documents using a lone
\r, where the old column arithmetic ranoff the end of the line.
Tests
Two regression tests added to
test/html.js:reports the same positions for every line terminator— parses the samedocument joined with
\nand\r\nand asserts the node positions relative tothe style block's start are identical.
translates between an offset and a location consistently— asserts a CRLFdocument's reported
source.start.offsetreally 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:
With the fix:
73 passing, andnpm run lintpasses.