From 4597040f86a3dff0c10d6461efee4e910c767929 Mon Sep 17 00:00:00 2001 From: Divine Ikhuoria <64232397+Divuzki@users.noreply.github.com> Date: Sat, 28 Nov 2026 11:35:17 -0500 Subject: [PATCH] Preserve JavaScript division context across comments --- babel/messages/jslexer.py | 5 +++-- tests/messages/test_js_extract.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/babel/messages/jslexer.py b/babel/messages/jslexer.py index fa9b74294..891354c84 100644 --- a/babel/messages/jslexer.py +++ b/babel/messages/jslexer.py @@ -204,7 +204,7 @@ def _tokenize( # if we don't have a match we don't give up yet, but check for # division operators or regular expression literals, based on # the status of `may_divide` which is determined by the last - # processed non-whitespace token using `_indicates_division`. + # processed non-comment, non-whitespace token using `_indicates_division`. else: if may_divide: match = division_re.match(source, pos) @@ -220,7 +220,8 @@ def _tokenize( token_value = match.group() if token_type is not None: token = Token(token_type, token_value, lineno) - may_divide = _indicates_division(token) + if token_type not in ('linecomment', 'multilinecomment'): + may_divide = _indicates_division(token) yield token lineno += len(line_re.findall(token_value)) pos = match.end() diff --git a/tests/messages/test_js_extract.py b/tests/messages/test_js_extract.py index 7bf953c85..05055cdb5 100644 --- a/tests/messages/test_js_extract.py +++ b/tests/messages/test_js_extract.py @@ -67,6 +67,18 @@ def test_message_with_multiline_comment(): assert messages[0][3] == ['NOTE: hello', 'and bonjour', ' and servus'] +@pytest.mark.parametrize('comment', [b'/* total for the day */', b'// total for the day']) +def test_division_after_comment(comment): + buf = BytesIO( + b'const average = total ' + comment + b'\n' + b' / count;\n' + b'const label = gettext("Average");\n' + b'const pattern = /avg/;' + ) + messages = list(extract.extract('javascript', buf, extract.DEFAULT_KEYWORDS, [], {})) + assert messages == [(3, 'Average', [], None)] + + def test_ignore_function_definitions(): buf = BytesIO(b"""\ function gettext(value) {