Skip to content

fix: Avoid conflict with existing useTranslations import for useExtracted#2150

Merged
amannn merged 6 commits intocanaryfrom
cursor/optimize-usetranslations-import-in-swc-plugin-claude-4.5-opus-high-thinking-0387
Dec 2, 2025
Merged

fix: Avoid conflict with existing useTranslations import for useExtracted#2150
amannn merged 6 commits intocanaryfrom
cursor/optimize-usetranslations-import-in-swc-plugin-claude-4.5-opus-high-thinking-0387

Conversation

@amannn
Copy link
Owner

@amannn amannn commented Dec 2, 2025

Fix duplicate useTranslations imports generated by the SWC plugin.

The SWC plugin previously generated duplicate imports (e.g., import { useTranslations, useTranslations } from 'next-intl';) when both useExtracted and useTranslations were present. This fix ensures that if the target translation hook (useTranslations or getTranslations) is already imported, the useExtracted/getExtracted import is removed, and all transformed calls reuse the existing hook's local name.


Open in Cursor Open in Web


Note

Rewrites useExtracted/getExtracted imports to aliased real hooks and updates call sites to use unique local aliases, preventing duplicate useTranslations/getTranslations imports; adds/updates tests.

  • SWC Plugin (packages/swc-plugin-extractor/src/lib.rs)
    • Introduce HookType::{extracted_name,target_name,local_name} for consistent mapping.
    • Rewrite imports of useExtracted/getExtracted to target hooks (useTranslations/getTranslations) with unique local aliases (e.g., useTranslations$1, getTranslations$1).
    • Update call-site rewriting to use the aliased local identifiers and DUMMY_SP.
  • Tests
    • Update fixtures to expect aliased imports/usages across next-intl and next-intl/server.
    • Add existing-aliased-hook fixture to verify coexistence with existing useTranslations aliases.
    • Refresh source maps to match new output.

Written by Cursor Bugbot for commit 0fa519e. This will update automatically on new commits. Configure here.

This commit refactors the next-intl hook extraction logic to correctly handle existing hooks and aliases. It also updates tests to reflect these changes.

Co-authored-by: jan <jan@amann.work>
@cursor
Copy link

cursor bot commented Dec 2, 2025

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@vercel
Copy link

vercel bot commented Dec 2, 2025

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

Project Deployment Preview Comments Updated (UTC)
next-intl-docs Ready Ready Preview Comment Dec 2, 2025 4:33pm
next-intl-example-app-router Ready Ready Preview Comment Dec 2, 2025 4:33pm
next-intl-example-app-router-without-i18n-routing Ready Ready Preview Comment Dec 2, 2025 4:33pm

@amannn amannn changed the title Optimize useTranslations import in swc plugin fix: Conflate existing useTranslations import for useExtracted Dec 2, 2025
cursoragent and others added 5 commits December 2, 2025 16:07
Co-authored-by: jan <jan@amann.work>
This change renames the extracted hooks `useTranslations` and `getTranslations` to `useTranslations$1` and `getTranslations$1` respectively. This prevents naming conflicts when the original hooks are also imported and used in the same scope.

Co-authored-by: jan <jan@amann.work>
Co-authored-by: jan <jan@amann.work>
@amannn amannn marked this pull request as ready for review December 2, 2025 16:31
@amannn amannn merged commit 067b6e7 into canary Dec 2, 2025
9 checks passed
fn local_name(self) -> swc_atoms::Atom {
match self {
HookType::UseTranslation => "useTranslations$1".into(),
HookType::GetTranslation => "getTranslations$1".into(),
Copy link
Owner Author

@amannn amannn Dec 3, 2025

Choose a reason for hiding this comment

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

I had an idea about using useExtracted here instead, but Bugbot made a valid point here: #2153 (comment)

@amannn amannn changed the title fix: Conflate existing useTranslations import for useExtracted fix: Avoid conflict with existing useTranslations import for useExtracted Dec 3, 2025
@amannn amannn deleted the cursor/optimize-usetranslations-import-in-swc-plugin-claude-4.5-opus-high-thinking-0387 branch January 30, 2026 10:27
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.

2 participants