Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 6, 2025

✅ Refactor Text static class to module namespace object pattern

This PR successfully refactors the Text static class to use the module namespace object pattern (similar to existing Disposables, Objects, and User) for better tree-shaking support by modern bundlers.

Completed Work

Text Refactoring (COMPLETE):

  • Created text/ directory with 13 individual function files
  • Updated Text.ts to re-export from text/index using module namespace pattern
  • Ensured all types/enums (Encoding, NewlineKind, IReadLinesFromIterableOptions) remain accessible
  • Updated test file to use function.name for synchronized test titles
  • Factored out common newline regex helper to reduce duplication
  • Used Symbol.matchAll for proper regex iteration
  • Added module documentation to text/index.ts with @module JSDoc tag
  • All 235 tests passing (0 failures)
  • Build successful with no errors
  • CodeQL security check passed (0 alerts)
  • Removed unrelated FileSystem changes to keep PR focused on Text API only

API Compatibility

Backward compatibility maintained - All consumers continue to use the same API:

// Usage remains identical
import { Text } from '@rushstack/node-core-library';
Text.padEnd('hello', 10);      // Works exactly as before
Text.convertToLf(content);     // Works exactly as before  
Text.escapeRegExp('[test]');   // Works exactly as before

Tree-Shaking Benefits

With this refactoring, bundlers can now tree-shake unused Text functions:

// Before: Importing Text pulls in ALL methods (~293 lines)
import { Text } from '@rushstack/node-core-library';

// After: Bundlers can eliminate unused functions
// Only padEnd() will be included in the final bundle
Text.padEnd('hello', 10);

Recent Updates

  • Created _newlineHelpers.ts to factor out common NEWLINE_REGEX and replace logic
  • Updated convertTo, convertToCrLf, convertToLf to use shared helper
  • Changed readLinesFromIterable.ts to use NEWLINE_REGEX[Symbol.matchAll] instead of .matchAll()
  • Restored use of Text.functionName.name in tests for synchronized test titles
  • Removed fileSystem/errorChecks.ts to keep PR focused on Text API only
  • Preserved original class documentation in text/index.ts with @module tag for module-level documentation
Original prompt

This section details on the original issue you should resolve

<issue_title>[node-core-library] Replace static classes with exported module namespace objects</issue_title>
<issue_description>## Summary
While preserving the implementations and outward-facing API shape (All consumers of the refactored APIs should not need any code changes to compile and test successfully), replace static classes like:

./FileSystem.ts

export class FileSystem {
  public static someApi(...): TResult { ... }
}

with a reexported module namespace object that forwards individual exports for single files:

./FileSystem.ts

import * as FileSystem from './fileSystem/index';

./fileSystem/index.ts

export { someApi } from './someApi';

./fileSystem/someApi.ts

export function someApi(...): TResult { ... }

The Disposables and Objects APIs are existing examples of the new pattern. FileSystem and Text are examples of the old pattern to be replaced.

The reason for this conversion is that module namespace objects can be tree-shaken by modern bundlers, but static members on classes cannot.

Question Answer
Package name:
Package version?
Operating system?
Would you consider contributing a PR?
Node.js version (node -v)?
</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Replace static classes with exported module namespace objects Refactor Text static class to module namespace object for tree-shaking Nov 6, 2025
Copilot AI requested a review from dmichon-msft November 6, 2025 20:37
Copilot finished work on behalf of dmichon-msft November 6, 2025 20:37
… restore function.name in tests

Co-authored-by: dmichon-msft <[email protected]>
Copilot AI changed the title Refactor Text static class to module namespace object for tree-shaking [node-core-library] Refactor Text static class to module namespace object for tree-shaking Nov 7, 2025
Copilot AI requested a review from dmichon-msft November 7, 2025 22:34
Copilot finished work on behalf of dmichon-msft November 7, 2025 22:34
Copilot AI requested a review from dmichon-msft November 7, 2025 23:20
Copilot finished work on behalf of dmichon-msft November 7, 2025 23:20
Comment on lines -86 to -94
/**
* Operations for working with strings that contain text.
*
* @remarks
* The utilities provided by this class are intended to be simple, small, and very
* broadly applicable.
*
* @public
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot preserve this comment in Text/index.ts and give it the @module JSDoc tag to indicate that it should be treated as documentation for the module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the documentation comment to text/index.ts with the @module JSDoc tag. The comment preserves the original Text class documentation describing operations for working with strings. (commit 236df5b)

Note: There's a TSDoc linting warning about @module not being defined in the configuration, but this is expected as @module is a standard JSDoc tag for documenting modules. The build and all tests pass successfully.

Copilot AI requested a review from dmichon-msft November 12, 2025 01:30
Copilot finished work on behalf of dmichon-msft November 12, 2025 01:30
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.

[node-core-library] Replace static classes with exported module namespace objects

2 participants