Skip to content

fix(transloco): strip a leading BOM in keys-manager and optimize - #1002

Merged
shaharkazaz merged 2 commits into
masterfrom
fix/bom-strip-cli-tools
Sep 2, 2026
Merged

fix(transloco): strip a leading BOM in keys-manager and optimize#1002
shaharkazaz merged 2 commits into
masterfrom
fix/bom-strip-cli-tools

Conversation

@shaharkazaz

@shaharkazaz shaharkazaz commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

Follow-up to #1000, which taught transloco-validator to strip a leading UTF-8 BOM before parsing a translation file. The sibling Node tools that read the same files were left with the original bug, so the toolchain was inconsistent: a translation file saved with a BOM would now pass validation and then still blow up further down the pipeline.

Concretely, before this PR:

$ transloco-validator src/assets/i18n/en.json   # passes since #1000
$ transloco-keys-manager find
SyntaxError: Unexpected token ... is not valid JSON
# (the offending token is the invisible BOM at offset 0)

What changed

Two sites parse translation JSON out of a raw readFileSync result and so hit the same SyntaxError:

  • libs/transloco-keys-manager/src/lib/utils/file.utils.ts — the parse: true branch of readFile, reached from keys-detective/compare-keys-to-files.ts and webpack-plugin/generate-keys.ts.
  • libs/transloco-optimize/src/lib/transloco-optimize.tsoptimizeFiles.

Both now apply the same anchored .replace(/^\uFEFF/, '') that #1000 introduced, so the three CLI entry points agree on how they treat a BOM.

Scope notes

  • The strip is deliberately confined to the parse branch of readFile. Raw (unparsed) reads feed run-prettier, which writes its result back over the user's own file — stripping there would silently delete BOMs from source files people asked us to format. There is a spec pinning that behavior.
  • keys-builder/utils/get-current-translation.ts is untouched: its parseJson goes through fs-extra's readJsonSync, and jsonfile already strips the BOM. Verified rather than assumed.
  • The browser runtime was never affected — XHR/fetch text decoding strips the BOM before Angular's HttpXhrBackend parses the response. This is a CLI-only gap.
  • The schematics JSON.parse call sites are left alone; those read workspace/package config, not translation files.

Verification

  • New spec libs/transloco-keys-manager/src/lib/tests/file.utils.spec.ts (4 tests) and libs/transloco-optimize/src/lib/transloco-optimize.spec.ts (3 tests). transloco-optimize had no specs at all before this, and the suite runs with passWithNoTests: true, so the test count was confirmed to actually rise from 0 to 3 rather than the suite merely staying green.
  • Mutation-tested each one-liner independently: reverting the keys-manager strip fails exactly its BOM spec (1 failed, 3 passed); reverting the optimize strip fails exactly its BOM spec (1 failed, 2 passed). Each spec guards its own site.
  • Full suites green: transloco-keys-manager 222 passed / 1 skipped across 21 files, transloco-optimize 3 passed.
  • nx lint clean for both projects (0 errors; the remaining warnings are pre-existing no-explicit-any).
  • Both tsconfig.lib.json files already exclude **/*.spec.ts, so neither new spec is emitted into dist or the published packages.

Related to #609. Does not close it — #1000 is the PR that addresses the reported case; this only brings the sibling tools in line.

Summary by CodeRabbit

  • Bug Fixes

    • Translation and JSON files with a leading UTF-8 BOM now parse and optimize correctly.
    • Existing handling of malformed JSON and raw file reads remains unchanged.
  • Tests

    • Added coverage for BOM-prefixed files, standard JSON files, malformed JSON, comment removal, and raw content preservation.

@shaharkazaz shaharkazaz changed the title fix(transloco): strip a leading BOM in the CLI tools fix(transloco): strip a leading BOM in keys-manager and optimize Aug 29, 2026
@coderabbitai

coderabbitai Bot commented Aug 29, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f1a8ed67-80aa-4893-91cc-cf95be047672

📥 Commits

Reviewing files that changed from the base of the PR and between d6d169c and ba2aa29.

📒 Files selected for processing (4)
  • libs/transloco-keys-manager/src/lib/tests/file.utils.spec.ts
  • libs/transloco-keys-manager/src/lib/utils/file.utils.ts
  • libs/transloco-optimize/src/lib/transloco-optimize.spec.ts
  • libs/transloco-optimize/src/lib/transloco-optimize.ts

Included review availability: Your plan provides up to 8 included reviews per hour; 6 remain after this review.


📝 Walkthrough

Walkthrough

readFile and optimizeFiles now remove a leading UTF-8 BOM before parsing JSON. Tests cover BOM-prefixed files, standard files, malformed JSON, raw BOM preservation, flattening, and comment removal.

Changes

BOM-aware JSON parsing

Layer / File(s) Summary
Keys manager file reader
libs/transloco-keys-manager/src/lib/utils/file.utils.ts, libs/transloco-keys-manager/src/lib/tests/file.utils.spec.ts
readFile strips a leading BOM before JSON parsing. Tests cover parsed JSON, malformed JSON errors, and raw reads that preserve the BOM.
Optimizer file reader
libs/transloco-optimize/src/lib/transloco-optimize.ts, libs/transloco-optimize/src/lib/transloco-optimize.spec.ts
optimizeFiles strips a leading BOM before parsing. Tests cover flattening, comment removal, and malformed input rejection.

Merge Risk: ⚪ Minimal · up to ba2aa

The change allows two CLI tools to parse translation files with a leading BOM while preserving source-file formatting behavior; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely identifies the main change: stripping a leading BOM in the keys-manager and optimize tools.
Description check ✅ Passed The description is detailed and directly explains the bug, affected code paths, scope decisions, tests, mutation testing, lint results, and suite results. It does not reproduce every template heading …
Full details: Description check

Explanation

The description is detailed and directly explains the bug, affected code paths, scope decisions, tests, mutation testing, lint results, and suite results. It does not reproduce every template heading or checklist item, but it provides the required change context and verification information.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/bom-strip-cli-tools

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.

@pkg-pr-new

pkg-pr-new Bot commented Aug 29, 2026

Copy link
Copy Markdown

Open in StackBlitz

@jsverse/transloco

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco@1002

@jsverse/transloco-keys-manager

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-keys-manager@1002

@jsverse/transloco-locale

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-locale@1002

@jsverse/transloco-messageformat

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-messageformat@1002

@jsverse/transloco-optimize

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-optimize@1002

@jsverse/transloco-persist-lang

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-persist-lang@1002

@jsverse/transloco-persist-translations

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-persist-translations@1002

@jsverse/transloco-preload-langs

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-preload-langs@1002

@jsverse/transloco-schematics

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-schematics@1002

@jsverse/transloco-scoped-libs

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-scoped-libs@1002

@jsverse/transloco-utils

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-utils@1002

@jsverse/transloco-validator

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-validator@1002

commit: ba2aa29

@shaharkazaz
shaharkazaz merged commit e853885 into master Sep 2, 2026
13 checks passed
@shaharkazaz
shaharkazaz deleted the fix/bom-strip-cli-tools branch September 2, 2026 16:04
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