Skip to content

fix: don't warn about missing id when a spread is present#2581

Merged
andrii-bodnar merged 1 commit into
lingui:mainfrom
mogelbrod:dont-warn-on-id-spread
Jul 6, 2026
Merged

fix: don't warn about missing id when a spread is present#2581
andrii-bodnar merged 1 commit into
lingui:mainfrom
mogelbrod:dont-warn-on-id-spread

Conversation

@mogelbrod

Copy link
Copy Markdown
Contributor

Description

Code like below currently triggers a "Missing message ID, skipping" warning on lingui extract, even though it works and is a decent workaround to use placeholders in lazy lookups:

const lookup = {
  x: msg`x {val}`,
  generic: msg`generic {val}`,
}

function X() {
  const { i18n } = useI18n()
  return i18n._({
    ...(lookup.x ?? lookup.generic),
    values: { val: 'test' },
  })
}

I'd like to propose that we stop reporting missing IDs when a spread is also present in the object passed to the i18n._ call.

If this is accepted I'll happily provide patches for the experimental extractor along with the SWC extractor.

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

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 Jun 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 Jun 24, 2026 4:35pm

Request Review

@codecov

codecov Bot commented Jun 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.79%. Comparing base (6bb8983) to head (d2f1b42).
⚠️ Report is 354 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #2581       +/-   ##
===========================================
+ Coverage   77.05%   89.79%   +12.74%     
===========================================
  Files          84      124       +40     
  Lines        2157     3695     +1538     
  Branches      555     1110      +555     
===========================================
+ Hits         1662     3318     +1656     
+ Misses        382      339       -43     
+ Partials      113       38       -75     

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

@andrii-bodnar andrii-bodnar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@mogelbrod thank you!

@andrii-bodnar andrii-bodnar merged commit b1b9480 into lingui:main Jul 6, 2026
11 checks passed
@andrii-bodnar

Copy link
Copy Markdown
Contributor

@mogelbrod would you create subsequent fixes for the SWC and experimental extractor?

@mogelbrod

Copy link
Copy Markdown
Contributor Author

@mogelbrod would you create subsequent fixes for the SWC and experimental extractor?

Thanks, Will do @andrii-bodnar!

@timofei-iatsenko

timofei-iatsenko commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

@mogelbrod

experimental extractor

Rather into my branch with new rust extractor, lingui/swc-plugin#236

@mogelbrod

mogelbrod commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Rather into my branch with new rust extractor, lingui/swc-plugin#236

@timofei-iatsenko I do not seem to be able to open a PR stacked against your swc-plugin branch as it's a fork 🙁

The following patch should address this and apply cleanly on top of the latest pushed commit in that PR:

diff --git a/crates/lingui_extractor/src/message_extractor_visitor.rs b/crates/lingui_extractor/src/message_extractor_visitor.rs
index a5949fa..97e04e3 100644
--- a/crates/lingui_extractor/src/message_extractor_visitor.rs
+++ b/crates/lingui_extractor/src/message_extractor_visitor.rs
@@ -330,6 +330,16 @@ impl<'a> MessageExtractorVisitor<'a> {
         let raw = extract_from_object_expression(obj, &mut self.warnings);
 
         if raw.id.is_none() {
+            // The id may be provided by a spread element (e.g. `i18n._({ ...msg })`),
+            // which can't be resolved statically. Skip silently instead of warning.
+            let has_spread = obj
+                .props
+                .iter()
+                .any(|prop| matches!(prop, PropOrSpread::Spread(_)));
+            if has_spread {
+                return;
+            }
+
             let loc = self.source_map.span_to_string(span);
             self.warnings
                 .push(format!("{loc}: Missing message ID, skipping."));
diff --git a/crates/lingui_extractor/tests/message_extractor.rs b/crates/lingui_extractor/tests/message_extractor.rs
index 247e1a9..81e3df3 100644
--- a/crates/lingui_extractor/tests/message_extractor.rs
+++ b/crates/lingui_extractor/tests/message_extractor.rs
@@ -84,6 +84,21 @@ import { Trans } from "@lingui/react";
     assert!(warnings.iter().any(|w| w.contains("Missing message ID")));
 }
 
+#[test]
+fn test_no_warning_when_id_could_be_provided_via_spread() {
+    let code = r#"
+const lookup = { x: {}, generic: {} };
+i18n._({
+  ...(lookup.x ?? lookup.generic),
+  values: { val: "test" },
+});
+    "#;
+
+    let (messages, warnings) = extract_and_sort(code, "test.js");
+    assert_no_warnings(&warnings);
+    assert_eq!(messages.len(), 0);
+}
+
 #[test]
 fn test_call_expression_i18n_underscore() {
     let code = r#"

@mogelbrod

Copy link
Copy Markdown
Contributor Author

@mogelbrod would you create subsequent fixes for the SWC and experimental extractor?

It looks like the experimental babel extractor uses the already patched extractFromMessageDescriptor, so it doesn't appear to need any change.

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.

3 participants