Skip to content

fix: ensure i18n objects are shallow copied#901

Open
mateo-leal wants to merge 1 commit intoWFCD:masterfrom
mateo-leal:i18n-array-reference-issue
Open

fix: ensure i18n objects are shallow copied#901
mateo-leal wants to merge 1 commit intoWFCD:masterfrom
mateo-leal:i18n-array-reference-issue

Conversation

@mateo-leal
Copy link
Copy Markdown

@mateo-leal mateo-leal commented Apr 10, 2026

What did you fix?

When changing locales within the same runtime environment, the returned array did not include the i18n entries for the new locale.
This pull request replaces the reference to the global i18n object with a shallow copy to resolve this issue.


Reproduction steps


Evidence/screenshot/link to line

Considerations

  • Does this contain a new dependency? No
  • Does this introduce opinionated data formatting or manual data entry? No
  • Does this pr include updated data files in a separate commit that can be reverted for a clean code-only pr? Yes
  • Have I run the linter? Yes
  • Is a bug fix, feature request, or enhancement? Bug Fix

Summary by CodeRabbit

  • Bug Fixes

    • Improved internationalization data handling to properly isolate locale entries and prevent shared reference mutations.
  • Tests

    • Added test coverage for multi-locale internationalization scenarios, validating correct behavior across sequential locale operations.

@mateo-leal mateo-leal requested a review from a team as a code owner April 10, 2026 19:55
@mateo-leal mateo-leal requested a review from AyAyEm April 10, 2026 19:55
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 10, 2026

📝 Walkthrough

Walkthrough

The changes modify how i18n entries are processed when options.i18n is an array. Instead of directly assigning the i18n lookup result, the code now creates a shallow clone using the spread operator and conditionally assigns it only when the entry exists. This affects both the main implementation files and includes a new test case validating locale-specific i18n data.

Changes

Cohort / File(s) Summary
i18n Entry Cloning
index.js, index.mjs
Modified i18n handling to clone entries ({ ...itemI18n }) instead of direct assignment, and set raw to undefined when no i18n entry exists for the item's uniqueName.
i18n Test Coverage
test/index.spec.mjs
Added new test case verifying locale-specific i18n entries (es, ja) are correctly present on constructed items across sequential test runs with i18nOnObject: true.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰✨ A shallow clone, so objects don't share,
Locales dance freely through the air,
Each item gets its own fresh start,
i18n's cloned with careful art!
🌍💚

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately and concisely describes the main change: ensuring i18n objects are shallow copied, which directly addresses the bug fix of locale switching within the same runtime.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
test/index.spec.mjs (1)

127-138: Nice regression test; consider adding anti-leak assertions.

This already covers the core bug well. You can make it stricter by also asserting each run only has the requested locale.

Suggested test hardening
       const esI18n = await getItem('es');
       assert.ok(esI18n.es, 'should have i18n on object for es');
+      assert.ok(!esI18n.ja, 'should not leak ja into es run');
       const jaI18n = await getItem('ja');
       assert.ok(jaI18n.ja, 'should have i18n on object for ja');
+      assert.ok(!jaI18n.es, 'should not leak es into ja run');
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/index.spec.mjs` around lines 127 - 138, The test should assert that each
run returns only the requested locale to prevent cross-run leakage: update the
test that uses getItem/wrapConstr (with i18nOnObject: true) to also assert that
the returned i18n object contains the requested locale key (e.g., es or ja) and
does NOT contain the other locale key, i.e., for esI18n assert es exists and ja
does not, and for jaI18n assert ja exists and es does not; use the existing
getItem, wrapConstr, firstItem, esI18n and jaI18n symbols to locate and modify
the assertions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@test/index.spec.mjs`:
- Around line 127-138: The test should assert that each run returns only the
requested locale to prevent cross-run leakage: update the test that uses
getItem/wrapConstr (with i18nOnObject: true) to also assert that the returned
i18n object contains the requested locale key (e.g., es or ja) and does NOT
contain the other locale key, i.e., for esI18n assert es exists and ja does not,
and for jaI18n assert ja exists and es does not; use the existing getItem,
wrapConstr, firstItem, esI18n and jaI18n symbols to locate and modify the
assertions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e5a3369c-b14d-4900-a820-f6542888f353

📥 Commits

Reviewing files that changed from the base of the PR and between d785728 and 31bbd66.

📒 Files selected for processing (3)
  • index.js
  • index.mjs
  • test/index.spec.mjs

Comment thread index.js
Comment on lines +108 to +109
const itemI18n = i18n[item.uniqueName];
const raw = itemI18n ? { ...itemI18n } : undefined;
Copy link
Copy Markdown
Member

@AyAyEm AyAyEm Apr 13, 2026

Choose a reason for hiding this comment

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

What other value besides an object or undefined itemI18n might be so it needs this check?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

None, but without that verification the branch coverage percentage requires a test that cannot be done on the if statement of line 111

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