fix: don't warn about missing id when a spread is present#2581
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
andrii-bodnar
left a comment
There was a problem hiding this comment.
@mogelbrod thank you!
|
@mogelbrod would you create subsequent fixes for the SWC and experimental extractor? |
Thanks, Will do @andrii-bodnar! |
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#"
|
It looks like the experimental babel extractor uses the already patched |
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: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
Checklist