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
4 changes: 2 additions & 2 deletions babel/messages/jslexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class Token(NamedTuple):

_rules: list[tuple[str | None, re.Pattern[str]]] = [
(None, re.compile(r'\s+', re.UNICODE)),
(None, re.compile(r'<!--.*')),
('linecomment', re.compile(r'//.*')),
(None, re.compile(r'<!--[^\r\n]*')),
('linecomment', re.compile(r'//[^\r\n]*')),
('multilinecomment', re.compile(r'/\*.*?\*/', re.UNICODE | re.DOTALL)),
('dotted_name', dotted_name_re),
('name', name_re),
Expand Down
17 changes: 17 additions & 0 deletions tests/messages/test_js_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,20 @@ def test_inside_nested_template_string():
)

assert messages == [(1, 'Greetings!', [], None), (1, 'This is a lovely evening.', [], None), (1, 'The day is really nice!', [], None)]


@pytest.mark.parametrize('comment', ['// comment', '<!-- comment'])
def test_line_numbers_are_the_same_for_lf_and_crlf(comment):
source = f"{comment}\n{comment}\n{comment}\nmsg = _('Bonjour')\n"

def line_numbers(newline):
buf = BytesIO(source.replace('\n', newline).encode('utf-8'))
return [lineno for lineno, _, _, _ in extract.extract('javascript', buf)]

assert line_numbers('\n') == line_numbers('\r\n') == [4]


def test_translator_comment_has_no_trailing_carriage_return():
buf = BytesIO("// NOTE: hello\r\nmsg = _('Bonjour à tous')\r\n".encode())
messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {}))
assert messages[0][3] == ['NOTE: hello']
13 changes: 13 additions & 0 deletions tests/messages/test_jslexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,16 @@ def test_jsx():
('jsx_tag', '</comp2', 8),
('operator', '>', 8),
]


def test_line_comment_does_not_eat_carriage_return():
# A `//` comment must not swallow the trailing `\r` of a CRLF line ending,
# otherwise the line count for the following code is off by one per comment.
source = "// comment\r\ngettext('foo')\r\n"
assert list(jslexer.tokenize(source)) == [
('linecomment', '// comment', 1),
('name', 'gettext', 2),
('operator', '(', 2),
('string', "'foo'", 2),
('operator', ')', 2),
]