Skip to content

Forbid private static members and refactor existing usages to module scope#5884

Open
iclanton wants to merge 3 commits into
microsoft:mainfrom
iclanton:forbid-private-static
Open

Forbid private static members and refactor existing usages to module scope#5884
iclanton wants to merge 3 commits into
microsoft:mainfrom
iclanton:forbid-private-static

Conversation

@iclanton

Copy link
Copy Markdown
Member

Summary

Adds an ESLint rule that forbids private static methods and properties (in decoupled-local-node-rig's flat profile), steering code toward module-scoped functions and variables, and refactors every existing violation across the repo to satisfy it.

Motivation

A private static member is really just module-scoped state or a helper expressed as a class member. Plain module-scoped functions/consts are simpler, avoid unnecessary ClassName. qualification, and are friendlier to bundlers.

What changed

  • The rig's no-restricted-syntax rule now also forbids private static methods and properties. It stays at error severity, alongside the existing export * from '...' restriction (a single rule instance can't have per-selector severities).
  • Every private static violation across all rig-consuming projects was converted:
    • private static method → module-scoped function
    • private static property → module-scoped const (or let when reassigned)

Notable edge cases (behavior preserved)

  • Helpers that touch private instance members or call a private constructor were kept as private instance methods, or changed to return data so an in-class caller performs the construction (e.g. JsonSchema, LockFile, RigConfig, MinimalRushConfiguration).
  • Stateful singletons (e.g. EnvironmentConfiguration) moved to module-scoped state.
  • Async lazy-init caches use a require-atomic-updates suppression where appropriate (matching existing repo convention).
  • Moved declarations were placed to preserve API-Extractor doc association (no .api.md churn) and avoid no-use-before-define.
  • Added typed @internal test seams where tests previously reached into private statics via Reflect.set / bracket access (LockFile, OperationBuildCache).

Testing

  • Full rush build passes with zero errors and zero warnings.
  • rush-lib test suite passes (706 tests).

iclanton added 3 commits July 18, 2026 18:59
…d-syntax rule

Enforce the new ESLint rule in decoupled-local-node-rig that forbids
`private static` methods and properties (kept at "error" severity alongside
the existing `export *` restriction).

Convert every `private static` violation across all rig-consuming projects to
module-scoped functions and variables:
- private static method    -> module-scoped function
- private static property   -> module-scoped const (or let if reassigned)

Edge cases handled without changing public behavior:
- Helpers needing private instance members / private constructors kept as
  private instance methods, or return data for an in-class caller to construct
  (e.g. JsonSchema, LockFile, RigConfig, MinimalRushConfiguration).
- Stateful singletons (e.g. EnvironmentConfiguration) moved to module state.
- Async lazy-init caches suppressed with require-atomic-updates where needed.
- Module declarations placed to preserve TSDoc/API-Extractor doc association
  and avoid no-use-before-define.
- Test seams exported where tests referenced moved members.

Full `rush build` passes with zero errors and zero warnings.
Moving `_tarUtilityPromise` from a `private static` field to a module-scoped
variable silently invalidated the test's `Reflect.set(OperationBuildCache,
'_tarUtilityPromise', ...)` mock injection, causing the restore test to run
the real tar/untar path (EACCES on mkdir '/repo').

Add an exported `@internal` test seam `_setTarUtilityPromiseForTesting` and
use it from the test instead of poking the class internals.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Needs triage

Development

Successfully merging this pull request may close these issues.

1 participant