Skip to content

Fix JS extractor line numbers being off on CRLF files#1288

Open
apoorva-01 wants to merge 1 commit into
python-babel:masterfrom
apoorva-01:fix/770-crlf-jslexer-line-numbers
Open

Fix JS extractor line numbers being off on CRLF files#1288
apoorva-01 wants to merge 1 commit into
python-babel:masterfrom
apoorva-01:fix/770-crlf-jslexer-line-numbers

Conversation

@apoorva-01

@apoorva-01 apoorva-01 commented Jul 1, 2026

Copy link
Copy Markdown

Fixes #770

The JS extractor reports different line numbers depending on line endings — a call after three comment lines shows up on line 4 with LF but line 7 with CRLF, which is the Linux/Windows difference the reporter hit.

It's the // and <!-- comment rules in jslexer: they match with .*, and . eats the \r of a CRLF, so the comment token keeps a trailing \r that gets counted as a newline, and then the \n is counted again — two lines per comment. Switching both to [^\r\n]* stops the comment before the newline so the \r\n is counted once. Added tests that the LF and CRLF line numbers match.

Left CHANGES.rst alone since it looks like that's filled in at release time, but happy to add an entry if you'd prefer.

The `//` and `<!--` line-comment rules matched with `.*`, which also
consumes the `\r` of a CRLF newline (only `\n` stops `.`). The comment
token therefore carried a trailing `\r` that `line_re` counted as a
line break, and the following lone `\n` was counted again by the
whitespace rule, so every comment line advanced the counter twice.
Messages extracted after a comment ended up shifted down by one line
per preceding comment line on CRLF-formatted files.

Restrict both rules to `[^\r\n]` so the comment stops before the
newline and the CRLF pair is counted once by the whitespace rule.
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.

Line number discrepancy with JavaScript extractor

1 participant