feat(keys-manager): support custom scope providers and wrapper services - #985
feat(keys-manager): support custom scope providers and wrapper services#985HermannBjorgvin wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThe keys manager now accepts configurable scope-provider functions and wrapper service names. TypeScript extraction detects custom services, and scope mapping detects custom providers. Tests cover custom services and scoped translations. Fun fact: “i18n” abbreviates “internationalization.” ChangesCustom provider and service support
Possibly related issues
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install timed out. The project may have too many dependencies for the sandbox. 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 |
@jsverse/transloco
@jsverse/transloco-keys-manager
@jsverse/transloco-locale
@jsverse/transloco-messageformat
@jsverse/transloco-optimize
@jsverse/transloco-persist-lang
@jsverse/transloco-persist-translations
@jsverse/transloco-preload-langs
@jsverse/transloco-schematics
@jsverse/transloco-scoped-libs
@jsverse/transloco-utils
@jsverse/transloco-validator
commit: |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
libs/transloco-keys-manager/src/lib/utils/update-scopes-map.ts (1)
30-43: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick winValidate custom provider names before interpolation.
scopeProviderFunctionsis inserted directly into atsqueryselector and aRegExp. An empty or malformed name can break selector parsing, broaden the pre-scan to unrelated files, or cause excessive backtracking while TypeScript files are scanned. Restrict each value to a valid TypeScript identifier and escape values used in the regular expression. Add tests for rejected names.Verify that the CLI and configuration normalization apply the same validation. This is a local and CI robustness issue, not a remote-user injection path.
Fun fact:
i18nis a numeronym with 18 letters betweeniandn.Static analysis flags the dynamic regular-expression construction.
Also applies to: 94-105
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@libs/transloco-keys-manager/src/lib/utils/update-scopes-map.ts` around lines 30 - 43, Validate scopeProviderFunctions entries as non-empty valid TypeScript identifiers during shared CLI/config normalization, rejecting invalid names consistently before they reach these helpers. In buildFunctionProviderQuery, only interpolate validated identifiers into selectors; in buildProviderRegex, escape each name before constructing the regex while preserving the built-in provider names. Add tests covering rejected names and the normalized validation path.Source: Linters/SAST tools
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@libs/transloco-keys-manager/src/lib/utils/update-scopes-map.ts`:
- Around line 30-43: Validate scopeProviderFunctions entries as non-empty valid
TypeScript identifiers during shared CLI/config normalization, rejecting invalid
names consistently before they reach these helpers. In
buildFunctionProviderQuery, only interpolate validated identifiers into
selectors; in buildProviderRegex, escape each name before constructing the regex
while preserving the built-in provider names. Add tests covering rejected names
and the normalized validation path.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: c5a7ed05-f7e8-47d4-b089-730b530e8d31
📒 Files selected for processing (16)
libs/transloco-keys-manager/src/lib/cli-options.tslibs/transloco-keys-manager/src/lib/keys-builder/typescript/index.tslibs/transloco-keys-manager/src/lib/keys-builder/typescript/service.extractor.tslibs/transloco-keys-manager/src/lib/keys-builder/utils/extract-keys.tslibs/transloco-keys-manager/src/lib/tests/buildTranslationFiles/build-translation-utils.tslibs/transloco-keys-manager/src/lib/tests/buildTranslationFiles/buildTranslationFiles.spec.tslibs/transloco-keys-manager/src/lib/tests/buildTranslationFiles/config-options/custom-providers/custom-providers-spec.tslibs/transloco-keys-manager/src/lib/tests/buildTranslationFiles/config-options/custom-providers/src/custom-scope-provider.tslibs/transloco-keys-manager/src/lib/tests/buildTranslationFiles/config-options/custom-providers/src/custom-service-constructor.tslibs/transloco-keys-manager/src/lib/tests/buildTranslationFiles/config-options/custom-providers/src/custom-service-inject.tslibs/transloco-keys-manager/src/lib/tests/warn-unsupported-options.spec.tslibs/transloco-keys-manager/src/lib/types.tslibs/transloco-keys-manager/src/lib/utils/resolve-config.tslibs/transloco-keys-manager/src/lib/utils/update-scopes-map.tslibs/transloco-keys-manager/src/lib/webpack-plugin/webpack-plugin.tslibs/transloco-utils/src/lib/transloco-utils.types.ts
|
@shaharkazaz holding off on responding to the code-rabbit reviews until everyone is happy with the design/approach to this feature. |
Adds two options to the keys manager so it can extract keys from projects that wrap the standard Transloco APIs in their own abstractions. Resubmission of jsverse/transloco-keys-manager#249, which was closed when that repository was archived. @shaharkazaz asked for it to be reopened here with a proper design pass.
In our Nx monorepo we wrap Transloco in a shared translations service to enforce conventions and keep future refactoring cheap. Without these options, TKM silently misses every key that flows through the wrapper, which makes
find's missing/extra-key enforcement unusable.What's included
scopeProviderFunctions(--scope-provider-functions) — additional function names treated likeprovideTranslocoScopewhen building the scopes map. Both the string form (provideScopedTranslations('todos')) and the object form (provideScopedTranslations({ scope: 'todos', alias: 'todosAlias' })) resolve, since the resolution reuses the same scope-def queries as the built-in provider. Lives inutils/update-scopes-map.ts, threaded fromresolve-config.tsand the webpack plugin's incremental path.serviceNames(--service-names) — additional service class names treated likeTranslocoServiceby the service extractor, covering both constructor injection andinject(...)in property/variable declarations (keys-builder/typescript/service.extractor.ts). The option flows to extractors throughExtractorConfigrather than a separate argument, so the extractor pipeline shape is unchanged.The TS extraction gate in
keys-builder/typescript/index.tsalso accounts for custom services: a file that injectsTranslationsServicebut never imports from@jsverse/transloco(the wrapper lives behind its own import path) is still parsed and run through the service extractor, while the fast-path skip for unrelated files is preserved.Config-file support — both options are also accepted under
keysManagerintransloco.config.ts(TranslocoGlobalConfigin@jsverse/transloco-utils), consistent with every other keys-manager option:Tests — a new
config-options/custom-providerssuite covers custom scope provider resolution (string + object/alias forms) and custom service extraction via both injection styles, including a fixture with notranslocostring anywhere in the file to lock in the import-gate behavior.Design notes — input welcome
Per the discussion on the original PR, flagging the open design questions rather than treating the old diff as settled:
scopeProviderFunctions/serviceNamesare carried over from the original PR. Happy to rename — e.g.customScopeProviders/customServiceNamesif you'd rather the names signal they extend rather than replace the built-ins.transloco.config.tsnext to the rest of the keys-manager config rather than on every CLI invocation.provideTranslocoScope/TranslocoServicedetection — TKM's extraction is syntactic throughout, so the custom names follow the same convention rather than introducing import-path resolution.PR Checklist
PR Type
What is the current behavior?
Only
provideTranslocoScope,TRANSLOCO_SCOPE, andTranslocoServiceare recognized for scope detection and service key extraction. Keys used through custom wrappers are not extracted, andfindreports them as extra/missing incorrectly.Issue Number: N/A (resubmission of jsverse/transloco-keys-manager#249)
What is the new behavior?
Custom scope provider function names and custom service class names can be registered via CLI flags or
transloco.config.ts, and are treated exactly like their built-in counterparts during extraction.Does this PR introduce a breaking change?
Other information
Docs for the two new options still need a home — I couldn't find the keys-manager options reference in this repo, so pointers welcome on where to add them (hence the unchecked docs box).
Summary by CodeRabbit
New Features
Tests