feat(core): support glob patterns and arrays for barrelFileName#253
Open
alilaa wants to merge 9 commits into
Open
feat(core): support glob patterns and arrays for barrelFileName#253alilaa wants to merge 9 commits into
alilaa wants to merge 9 commits into
Conversation
Add a standalone, dependency-free glob matching function that supports * (zero or more characters) and ? (exactly one character) wildcards with case-insensitive matching. This utility is designed to be reusable across the codebase, including for barrelFileName and potentially encapsulationPattern.
Update UserSheriffConfig to accept string | string[] for barrelFileName. Internally normalize to string[] in Configuration type. Update defaultConfig, parseConfig, and project-creator accordingly.
…terns Update findFiles in VirtualFs and DefaultFs to use matchGlob for filename matching. Update findModulePathsWithBarrel and findModulePathsWithoutBarrel to accept string[] of barrel patterns. Replace Module.barrelPath getter with isBarrelFile(path) method that checks filenames against configured barrel patterns using matchGlob. Update encapsulation violation check to use the new isBarrelFile method.
Add tests for: - findModulePathsWithBarrel with glob patterns and arrays - encapsulation violations with multiple barrel files - config parsing for barrelFileName as string, array, and glob - matchGlob edge cases
Update configuration reference to document that barrelFileName now accepts string | string[] with glob pattern support (* and ?). Add usage examples for Angular monorepo use case.
isBarrelFile() previously matched barrel patterns against the basename only, so nested files like orders/internal/index.ts were incorrectly treated as public entry barrels. This weakened encapsulation by allowing deep imports to pass the barrel check. The fix adds a directory check: the file's parent directory must match the module's root path before the filename pattern is evaluated. This restores the previous behavior where only the module's own barrel path was considered valid. Adds direct unit tests for Module.isBarrelFile() and regression tests for nested barrel-like files in the encapsulation test suite.
Add 31 additional tests covering: - Consecutive and adjacent wildcards (**, *?, ?*) - Greedy star matching with multiple split points - Single-character pattern edge cases (?, ??) - Boundary and length edge cases - encapsulationPattern reuse scenarios Total: 76 tests for the glob matching function.
…atcher closure, eliminating repeated regex compilation in hot paths
a9a3d66 to
bba2e19
Compare
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
This PR extends the barrelFileName configuration option to accept string | string[] with glob pattern support (* and ? wildcards). This allows users to define multiple barrel file patterns (e.g., ["index.ts", "public-api.ts", "*.barrel.ts"]), which is useful for Angular monorepos and other projects with non-standard barrel file conventions.
Motivation
Currently barrelFileName only accepts a single literal string ("index.ts"). Projects that use multiple barrel naming conventions (e.g., Angular's public-api.ts alongside index.ts) had no way to configure Sheriff to recognize all of them. This change makes barrel detection flexible without adding external dependencies.
Changes
Core feature:
Extended UserSheriffConfig.barrelFileName to accept string | string[]
Internally normalized to string[] in the Configuration type
Added a dependency-free matchGlob utility supporting * and ? wildcards (case-insensitive)
Added a pre-compiled createGlobMatcher factory that avoids repeated regex compilation in hot paths
Module detection:
Updated findModulePathsWithBarrel and findModulePathsWithoutBarrel to match against multiple patterns
Replaced Module.barrelPath getter with isBarrelFile(path) method using glob matching
Updated encapsulation violation checks to use the new method
Performance:
Glob patterns are pre-compiled into a GlobMatcher closure (literal patterns use Set lookup, wildcard patterns use pre-built regexes) — created once and passed through the call chain
Tests:
76 unit tests for matchGlob covering wildcards, edge cases, and boundary conditions
Unit tests for Module.isBarrelFile() including nested barrel regression
Tests for findModulePathsWithBarrel with glob patterns and arrays
Tests for encapsulation violations with multiple barrel files
Config parsing tests for barrelFileName as string, array, and glob
Documentation:
Updated docs/configuration.md with the new barrelFileName syntax and usage examples
Breaking Changes
None. The previous barrelFileName: "index.ts" syntax continues to work unchanged.