Skip to content

feat(core): support glob patterns and arrays for barrelFileName#253

Open
alilaa wants to merge 9 commits into
softarc-consulting:mainfrom
alilaa:feat/barrelfilename-glob-support
Open

feat(core): support glob patterns and arrays for barrelFileName#253
alilaa wants to merge 9 commits into
softarc-consulting:mainfrom
alilaa:feat/barrelfilename-glob-support

Conversation

@alilaa

@alilaa alilaa commented May 19, 2026

Copy link
Copy Markdown

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.

alilaa added 9 commits May 8, 2026 15:27
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
@alilaa alilaa force-pushed the feat/barrelfilename-glob-support branch from a9a3d66 to bba2e19 Compare May 19, 2026 08:52
@sonarqubecloud

Copy link
Copy Markdown

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