fix(extractor): route each t alias to its own useTranslate namespace#204
Closed
bdshadow wants to merge 1 commit into
Closed
fix(extractor): route each t alias to its own useTranslate namespace#204bdshadow wants to merge 1 commit into
bdshadow wants to merge 1 commit into
Conversation
A file with multiple useTranslate calls in the same scope used to
attribute every t() call to whichever useTranslate appeared last, and
silently dropped any aliased call (`const { t: tCommon } = ...`).
The pre-pass now records each destructured alias and the namespace it
came from. Aliased call sites are retagged as trigger.t.function so
the existing rule pipeline handles them; the resulting keyInfo and
nsInfo carry the alias, and the reporter matches them up instead of
relying on "latest nsInfo wins."
Resolves the customer report on bdshadow/resolve-const-namespace
about multi-namespace components.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Problem
Reported by a customer: a file with two
useTranslatecalls in thesame scope misroutes keys.
tolgee extract print:t(...)is attributed tocommon(the namespace of the lastuseTranslate), andtCommon(...)is silently dropped — nowarning. The "component-specific NS plus a shared
commonNS"pattern is hard to avoid in a real codebase, so this hits people
quickly.
Root cause
Two interlocking bugs:
tFunctionMergeronly matches the literal namet(
token === 't'). Any aliased name liketCommonis invisible tothe parser, so the call never produces a
keyInfo.generateReportthreads "most recentnsInfo" ascontextNsforall sibling nodes. Two
useTranslate(...)declarations collapseonto whichever appeared last.
Solution
A small source-level pre-pass (
extractTranslateAliases.ts) detectsall
const { t [, ...] } = useTranslate(...)andconst { t: alias } = useTranslate(...)shapes, plus theconst t = await getTranslate(...)variant. For each, it records:aliasMap: alias name → resolved namespace literallineAliasMap: line of theuseTranslate(call → aliasThe namespace argument is resolved against the const map from
#202 (so
useTranslate(NS.COMMON)works once that PR lands).After the main merger pipeline, the parser walks the merged tokens
once and retags any
function.call(<alias>) + expression.beginpairas a
trigger.t.functiontoken whose.tokenis<alias>(. Theexisting
tFunctionrule strips the trailing(to recover the aliasand attaches it to the produced
keyInfo. TheuseTranslaterulelooks up
lineAliasMap[currentLine]and attaches the alias to itsnsInfo.generateReport.reportGeneralnow carries aMap<alias, nsInfo>alongside the existing "most-recent nsInfo" walker. When a
keyInfoarrives with an alias, the reporter prefers the matching nsInfo over
the latest-sibling heuristic.
Scope and limitations
useTranslateinside one function anda same-named alias inside a different function will still share the
binding. In practice, alias names like
tCommonare unique per file.useTranslate(...)calls. Multi-line argument lists arehandled, but the
lineAliasMapkeys on the line of the opening(— collisions on the same line aren't disambiguated.
bdshadow/resolve-const-namespace.Verification
npm run test:unit— 613 passed (was 597; +16 new alias testsacross four file extensions).
namespaces and zero warnings.
Test plan
const t = await getTranslate('ns')style (no destructure)continues to work.
Depends on
reuses the same constants map.