refactor: move ambient const enums into real modules#6089
Conversation
📝 WalkthroughWalkthroughThis change extracts shared enums into runtime-exported modules, updates imports and declaration formatting across CLI services, adds optional iOS target parenting, adjusts cleanup request handling, and aligns analytics and system-warning tests with the new enum locations. ChangesEnum and contract migration
Estimated code review effort: 3 (Moderate) | ~20 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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.
Inline comments:
In `@lib/detached-processes/cleanup-process.ts`:
- Around line 229-249: Update the existence check in removeRequest to compare
each queued currentRequestInfo with the removal requestInfo, matching the
comparison already used by _.remove. Preserve the existing success and
absent-request logging behavior based on whether that matching request is
present.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: c7665e5c-a52a-47d9-aa0e-dfd95c8fc810
📒 Files selected for processing (38)
lib/commands/build.tslib/commands/debug.tslib/commands/deploy.tslib/commands/prepare.tslib/commands/test.tslib/common/declarations.d.tslib/common/definitions/key-commands.tslib/common/enums.tslib/common/errors.tslib/common/services/analytics/google-analytics-custom-dimensions.tslib/common/services/cancellation.tslib/common/services/commands-service.tslib/common/verify-node-version.tslib/constants.tslib/definitions/project.d.tslib/definitions/system-warnings.d.tslib/definitions/system-warnings.tslib/detached-processes/cleanup-js-subprocess.tslib/detached-processes/cleanup-process.tslib/detached-processes/detached-process-enums.tslib/detached-processes/file-log-service.tslib/helpers/livesync-command-helper.tslib/helpers/options-track-helper.tslib/nativescript-cli.tslib/options.tslib/services/analytics/analytics-broker-process.tslib/services/analytics/analytics-broker.tslib/services/analytics/analytics-service.tslib/services/analytics/google-analytics-cross-client-custom-dimensions.d.tslib/services/analytics/google-analytics-provider.tslib/services/cleanup-service.tslib/services/initialize-service.tslib/services/ios-native-target-service.tslib/services/test-execution-service.tslib/sys-info.tstest/options.tstest/services/analytics/analytics-service.tstest/sys-info.ts
💤 Files with no reviewable changes (2)
- lib/services/analytics/google-analytics-cross-client-custom-dimensions.d.ts
- lib/definitions/system-warnings.d.ts
TypeScript inlines `const enum` values at compile time and erases the
import, so declaring them in .d.ts files worked only because tsc has
whole-program type information. Any file-local transpiler (esbuild, swc,
tsx, vite, bun) cannot inline them and fails to resolve the import, which
blocks running the sources without a full tsc pass.
The 12 ambient const enums now live in real modules:
- 5 from lib/common/declarations.d.ts -> lib/common/enums.ts
- BuildNames -> lib/constants.ts, next to the
other build-related enums
- detached-process-enums, system-warnings and
google-analytics-custom-dimensions -> .d.ts renamed to .ts; they held
nothing but enums
- SpecialKeys -> key-commands.d.ts renamed to .ts,
which already exported everything,
so importers are unchanged
The four global files had no imports, making their enums globally visible;
consumers now import them explicitly.
GoogleAnalyticsCrossClientCustomDimensions is referenced nowhere and was
dropped rather than converted into a dead runtime module.
Renaming key-commands to .ts surfaced two interface methods whose missing
return types an ambient context had allowed; both implementations return
nothing, so they are annotated void.
10ebf46 to
98a0dd8
Compare
Moving the ambient const enums into real modules removed the last thing standing in the way: tsc --noEmit --isolatedModules reports 244 errors on main (all TS2748, 'Cannot access ambient const enums') and none once they are real modules. This is not emit-neutral. With isolatedModules on, tsc stops inlining const enums and emits them as runtime objects, so 33 files change: consumers go from a baked-in literal to a property lookup, and lib/constants.js now emits ShouldMigrate, NativePlatformStatus, DebugTools, TrackActionNames, BuildStates and PlatformTypes. The values are unchanged - verified at runtime - and the suite is unchanged at 1513 passing, with the CLI still rendering help and reporting its version. That change is arguably the point. oxc and esbuild cannot inline a const enum across files, so today tsc's output and every other transpiler's output disagree for exactly those six. Now they agree. It also means the const modifier on the remaining ten declarations no longer buys inlining, so it is decorative; converting them to plain enum is a sensible follow-up.
PR Checklist
What is the current behavior?
Twelve
const enums are declared inside ambient.d.tsfiles and used in value positions, for exampleprocess.exit(ErrorCodes.UNHANDLED_REJECTION_FAILURE).That only works because TypeScript has whole-program type information: it inlines the constant at compile time and erases the import entirely.
declarations.d.tsproduces no runtime module, so there is nothing to import at runtime.Any file-local transpiler — esbuild, swc, tsx, vite, bun — cannot inline them. It leaves the import in place and resolution fails:
This makes the sources impossible to run through anything other than a full
tscpass, and it is also whatisolatedModulesdisallows.What is the new behavior?
The twelve ambient const enums now live in real modules:
lib/common/declarations.d.ts→ newlib/common/enums.tsBuildNames→lib/constants.ts, alongside the other build-related enumsdetached-process-enums,system-warningsandgoogle-analytics-custom-dimensions→.d.tsrenamed to.ts; they contained nothing but enumsSpecialKeys→key-commands.d.tsrenamed to.ts, which already exported everything, so importers are unchangedFour of those files were global ambient declarations with no imports, which is why their enums were visible everywhere without being imported. Consumers now import them explicitly.
Two incidental changes fall out of this:
GoogleAnalyticsCrossClientCustomDimensionsis referenced nowhere. It is dropped rather than converted into a dead runtime module.key-commands.d.tsto.tssurfaced two interface methods with no return type annotation, which an ambient context had allowed to be implicitlyany. Both implementations return nothing, so they are annotatedvoid.tsc --noEmitis clean and the suite is unchanged at 1513 passing. Note that the.d.tsfiles here are hand-written rather than generated (declaration: false), but the package declares notypes/typingsentry and ships nolib/nativescript-cli-lib.d.ts, so they are not a consumable public type surface.Summary by CodeRabbit
Bug Fixes
Enhancements
Refactor
Follow-up commit: enable
isolatedModulesMoving these enums out of the ambient files removed the last blocker, so the flag is turned on in the same PR:
tsc --noEmit --isolatedModulesmainTS2748: Cannot access ambient const enums)This part is not emit-neutral, so review it as a runtime change rather than a type-only one. With
isolatedModuleson, tsc stops inlining const enums: 33 emitted files change, consumers move from a baked-in literal to a property lookup, andlib/constants.jsnow emitsShouldMigrate,NativePlatformStatus,DebugTools,TrackActionNames,BuildStatesandPlatformTypesas runtime objects.Values are unchanged (verified at runtime), the suite is unchanged at 1513 passing, and the CLI still reports its version and renders the full help.
Arguably that convergence is the point: oxc and esbuild cannot inline a cross-file const enum, so today tsc's output disagrees with every other transpiler's for exactly those six enums. Now they agree.
It also makes the
constmodifier on the ten remaining declarations decorative, since it no longer buys inlining — converting them to plainenumis a sensible follow-up.Happy to split this commit into its own PR stacked on this one if you would rather keep this change purely a refactor.