Skip to content

Decode JavaScript Unicode code point escapes during extraction - #1394

Open
theman6660 wants to merge 1 commit into
python-babel:masterfrom
theman6660:fix/js-unicode-codepoint-escapes
Open

theman6660 wants to merge 1 commit into
python-babel:masterfrom
theman6660:fix/js-unicode-codepoint-escapes

Conversation

@theman6660

Copy link
Copy Markdown

JavaScript gettext("Hello \u{1F600}") evaluates to "Hello 😀", but extraction currently produces the literal text "Hello u{1F600}". The resulting translation key does not match the runtime message.

Recognize lowercase-u braced hexadecimal escapes in unquote_string(), including supplementary code points and leading zeros. Preserve the existing fallback for malformed or out-of-range escapes and leave legacy hexadecimal/Unicode handling intact.

Add 29 cases across single/double quotes and template strings, including the public JavaScript extraction API, line numbers, NUL, BMP and maximum code points, adjacent/legacy escapes and malformed inputs. Before the fix, 23 failed and 6 passed. All 29 now pass; the focused lexer/extractor set has 49 passing tests. A separate Node.js runtime check agrees on 30 valid literal values. Full Babel suite with imported CLDR 48.2: 7860 passed, 7 skipped, 2 xfailed (one existing pytest deprecation warning). Project pre-commit hooks and git diff --check pass.

Prepared with OpenAI Codex assistance. No production usage, human review or upstream CI is claimed.

Comment thread babel/messages/jslexer.py
codepoint = codepoint_escape_re.match(string, escape_pos + 2)
if codepoint is not None:
value = int(codepoint.group(1), 16)
if value <= 0x10ffff:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this check needed?

EDIT: ah, right, that's basically the maximum for JavaScript. Sure, doesn't hurt here...

@akx
akx requested a balanced review from Copilot October 26, 2026 03:58
@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.44%. Comparing base (e2a1791) to head (299db77).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1394      +/-   ##
==========================================
+ Coverage   93.37%   93.44%   +0.07%     
==========================================
  Files          27       27              
  Lines        4922     4931       +9     
==========================================
+ Hits         4596     4608      +12     
+ Misses        326      323       -3     
Flag Coverage Δ
macos-14-3.10 92.53% <100.00%> (+0.07%) ⬆️
macos-14-3.11 92.47% <100.00%> (+0.07%) ⬆️
macos-14-3.12 92.67% <100.00%> (+0.07%) ⬆️
macos-14-3.13 92.67% <100.00%> (+0.07%) ⬆️
macos-14-3.14 92.65% <100.00%> (+0.07%) ⬆️
macos-14-3.15 92.65% <100.00%> (+0.07%) ⬆️
macos-14-3.8 92.41% <100.00%> (+0.07%) ⬆️
macos-14-3.9 92.47% <100.00%> (+0.07%) ⬆️
macos-14-pypy3.10 92.53% <100.00%> (+0.07%) ⬆️
ubuntu-24.04-3.10 92.55% <100.00%> (+0.07%) ⬆️
ubuntu-24.04-3.11 92.49% <100.00%> (+0.07%) ⬆️
ubuntu-24.04-3.12 92.69% <100.00%> (+0.07%) ⬆️
ubuntu-24.04-3.13 92.69% <100.00%> (+0.07%) ⬆️
ubuntu-24.04-3.14 92.67% <100.00%> (+0.07%) ⬆️
ubuntu-24.04-3.15 92.67% <100.00%> (+0.07%) ⬆️
ubuntu-24.04-3.8 92.43% <100.00%> (+0.07%) ⬆️
ubuntu-24.04-3.9 92.49% <100.00%> (+0.07%) ⬆️
ubuntu-24.04-pypy3.10 92.55% <100.00%> (+0.07%) ⬆️
windows-2022-3.10 92.54% <100.00%> (+0.07%) ⬆️
windows-2022-3.11 92.48% <100.00%> (+0.07%) ⬆️
windows-2022-3.12 92.69% <100.00%> (+0.07%) ⬆️
windows-2022-3.13 92.69% <100.00%> (+0.07%) ⬆️
windows-2022-3.14 92.67% <100.00%> (+0.07%) ⬆️
windows-2022-3.15 92.67% <100.00%> (+0.07%) ⬆️
windows-2022-3.8 92.52% <100.00%> (+0.07%) ⬆️
windows-2022-3.9 92.48% <100.00%> (+0.07%) ⬆️
windows-2022-pypy3.10 92.54% <100.00%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

🟢 Approval recommended

The focused implementation preserves existing fallback behavior and has targeted regression coverage, with no blocking issues identified in static review.

No open issues 🎉

What changed in this PR

This PR aligns JavaScript message extraction with runtime string values by decoding lowercase \u{...} Unicode code point escapes.

Changes:

  • Decode valid braced escapes, including supplementary code points and leading zeros, while preserving legacy handling and malformed-input fallback.
  • Add 29 regression cases covering quoted strings, templates, extraction results, line numbers, and escape edge cases.
File Description
tests/​messages/​test_js_unicode_codepoints.py Adds decoding and public extraction API regression coverage.
babel/​messages/​jslexer.py Recognizes and decodes valid braced Unicode code point escapes.

Review effort: Balanced

@codspeed

codspeed Bot commented Oct 26, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 88 untouched benchmarks


Comparing theman6660:fix/js-unicode-codepoint-escapes (299db77) with master (e2a1791)

Open in CodSpeed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants