Skip to content

fix: throw on dynamic macro message ids#2553

Open
yslpn wants to merge 3 commits into
lingui:mainfrom
yslpn:fix/jsx-dynamic-id
Open

fix: throw on dynamic macro message ids#2553
yslpn wants to merge 3 commits into
lingui:mainfrom
yslpn:fix/jsx-dynamic-id

Conversation

@yslpn

@yslpn yslpn commented May 24, 2026

Copy link
Copy Markdown
Contributor

Description

Throw a compile-time error when Lingui macros receive a dynamic message id in JS or JSX.

Note: the SWC plugin has the same gap and should be fixed separately.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Examples update

Fixes #2523

Checklist

  • I have read the CONTRIBUTING and CODE_OF_CONDUCT docs
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)

@vercel

vercel Bot commented May 24, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
js-lingui Ready Ready Preview May 26, 2026 4:11pm

Request Review

@codecov

codecov Bot commented May 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 89.55%. Comparing base (6bb8983) to head (a3416d7).
⚠️ Report is 354 commits behind head on main.

Files with missing lines Patch % Lines
...-plugin-lingui-macro/src/messageDescriptorUtils.ts 83.33% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #2553       +/-   ##
===========================================
+ Coverage   77.05%   89.55%   +12.50%     
===========================================
  Files          84      119       +35     
  Lines        2157     3475     +1318     
  Branches      555     1036      +481     
===========================================
+ Hits         1662     3112     +1450     
+ Misses        382      327       -55     
+ Partials      113       36       -77     

☔ 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.

Comment on lines +44 to +49
name: "Preserve custom ID (dynamic expression)",
code: `
import { Trans } from '@lingui/react/macro';
const dynId = "dynamic";
<Trans id={dynId}>Hello World</Trans>;
`,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a misuse and it should throw. Or maybe i'm missing some valid use case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was careless and misunderstood the intent. The best solution would be to throw an error.

@timofei-iatsenko

timofei-iatsenko commented May 26, 2026

Copy link
Copy Markdown
Collaborator

As wrote above, passing a dynamic id is acceptable only on the runtime Trans. This is a valid pattern to resolve "lazy" translations. However passing a dynamic id to the Trans macro - break the logic. Extractor would not be able to extract this correctly, and it most likely a misuse. if you have a dynamic id - means you defined a message somewhere else. the message and parameters of Trans macro would be discarded.

@yslpn yslpn marked this pull request as draft May 26, 2026 07:24
@yslpn yslpn changed the title fix: preserve dynamic ids in JSX macros fix: throw on dynamic macro message ids May 26, 2026
@yslpn yslpn marked this pull request as ready for review May 27, 2026 16:50
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.

[v6] Inconsistent behaviour of dynamic IDs in JS/JSX macros

2 participants