Repository navigation
fix: emit $__() instead of $__i18n._() in useLingui transform - #280
Merged
Merged
Conversation
The `useLingui` macro rewrote `const { t } = useLingui()` to
`const { i18n: $__i18n, _: $__ } = $_useLingui()` and compiled every `t`
call to `$__i18n._(...)`. That crashes under `@lingui/solid`, where `i18n`
is an accessor and `$__i18n._` is `undefined`. It also defeats the React
Compiler manual-memoization check, because a `[t]` dependency compiles to
`[$__]` while the calls go through `$__i18n`.
Call the destructured `_` binding directly, as
`@lingui/babel-plugin-lingui-macro` does:
`const { _: $__ } = $_useLingui(); $__(...)`. `t(customI18n)` still emits
`customI18n._(...)`, and macro calls outside a `useLingui` scope still emit
`i18n._(...)` with the `i18n` import.
Fixes lingui#275
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #280 +/- ##
==========================================
+ Coverage 94.41% 94.44% +0.02%
==========================================
Files 11 11
Lines 2507 2518 +11
==========================================
+ Hits 2367 2378 +11
Misses 140 140
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Member
Let's leave them on |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #275
Inside a
useLinguiscope,tcalls now compile to$__(...)instead of$__i18n._(...), the same shape@lingui/babel-plugin-lingui-macroemits:The old output bound
$__but never called it. With@lingui/solid,i18nis an accessor, so$__i18n._isundefinedand the app crashes withTypeError: $__i18n._ is not a function. With React Compiler, a[t]dependency compiled to[$__]while the call went through$__i18n, so the compiler could not preserve the manual memoization.Changes
handle_use_lingui(lib.rs): drop thetprop from the rewritten pattern instead of replacing it withi18n: $__i18n. A user-writteni18nin the pattern is kept.create_i18n_fn_call(js_macro_folder.rs): inside auseLinguiscope, emit$__(...), tracked by a new optionalRuntimeIdents::use_lingui_tset only on the per-function context clone.t(customI18n)still emitscustomI18n._(...), and calls outside auseLinguiscope still emit$_i18n._(...), so those snapshots do not change.Tests
use_linguiandlingui_directiveinsta snapshots and thenative-toolstransform.spec.tsinline snapshot. Addedwork_with_custom_i18n_instancefort(i18n)inside auseLinguiscope.cargo test --workspace,cargo clippy --all-targets --all-features -- -D warnings,cargo fmt --check, andvitestinpackages/native-toolspass. With the source reverted, 12 Rust snapshots and the vitest inline snapshot fail.@lingui/solidapp.Open question
Core-macro calls such as
pluralfrom@lingui/core/macroinside auseLinguiscope also go through$__now, because they used the hook's$__i18nbefore. Routing them to the globali18nimport instead is a one-branch change increate_i18n_fn_callif you prefer that.