Skip to content

Conversation

@mahendra-vml
Copy link

@mahendra-vml mahendra-vml commented Jan 13, 2026

Description / Motivation

This PR improves correctness and maintainability of client component detection used by codegen. It addresses real-world edge cases (UTF-8 BOM, formatting variations) and makes the scanning behavior configurable to balance detection accuracy with performance. Unit tests were expanded to prevent regressions and validate the new detection logic across representative file prefixes and directive formats.

  • Refactored client component detection into a dedicated isClientComponent() function (code) and added focused unit tests covering its behavior (tests).
  • Replaced the fixed 12-byte read with a configurable prefix scan (default 512B) via CODEGEN_CLIENT_DIRECTIVE_PREFIX_BYTES (code) and introduced unit tests validating behavior across different prefix sizes (tests).
  • Added UTF-8 BOM handling by stripping BOM before regex matching (code) and added a regression test to ensure BOM-prefixed files are detected correctly (tests).
  • Improved 'use client' detection with a more robust regex allowing leading whitespace, comments, and an optional trailing semicolon (code) and added unit tests for common formatting variants (whitespace, comments, semicolon/no semicolon) (tests).

Testing Details

  • Unit Test Added
  • Manual Test/Other (Please elaborate) - Perform the manual testing on the current SitecoreAI implementation, where it fixed the previous error

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

- Refactor client component detection into dedicated `isClientComponent()` function
- Read configurable prefix bytes (default 512B, env var `CODEGEN_CLIENT_DIRECTIVE_PREFIX_BYTES`) instead of fixed 12 bytes for better performance
- Handle UTF-8 BOM correctly by stripping it before regex matching
- Use more robust regex pattern to detect 'use client' directive that allows leading whitespace, comments, and optional trailing semicolon
…ead of createReadStream

- Replace fs.createReadStream mocks with fs.promises.open stubs for client component detection tests
- Add test cleanup to delete CODEGEN_CLIENT_DIRECTIVE_PREFIX_BYTES env var in afterEach
- Add test coverage for 'use client' detection with leading whitespace, comments, BOM, and optional semicolon
- Add test coverage for CODEGEN_CLIENT_DIRECTIVE_PREFIX_BYTES environment variable configuration
- Simplify mock
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.

1 participant