fix: negated patterns with slashes incorrectly use basename-only matching when matchBase is true - #190
Open
deepakganesh78 wants to merge 1 commit into
Conversation
…me only when matchBase is true When matchBase is true, picomatch tests the compiled regex against the basename of the input path. For negated patterns containing path separators (e.g. '!**/examples/**'), this causes incorrect results because the negation regex's positive lookahead cannot detect directory structure from the basename alone. The fix tests both the full path and the basename for negated patterns with slashes, returning a non-match if either method detects that the positive pattern matches. This ensures directory-based negation patterns work correctly while preserving basename-based negation for patterns like '!**/*.js'. Fixes micromatch#136 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Fixes #136
Problem
When
matchBase: trueis set,picomatch.test()unconditionally tests the compiled regex against the basename of the input path. For negated patterns containing path separators (e.g.!**/examples/**), this produces incorrect results because the negative lookahead regex cannot detect directory structure from the basename alone.Reproduction:
Root cause
In
picomatch.test(), whenmatchBaseis true, the code callspicomatch.matchBase()which tests the regex againstutils.basename(input)only. For the negated regex^(?!^(?:...examples...)$).*$tested against basename.eslintrc.yaml, the positive pattern doesn't match the basename, so the negative lookahead passes and the result is incorrectlytrue.Fix
For negated patterns whose glob (after stripping the
!prefix) contains a path separator (/or\), the fix now tests both the full path (regex.exec(output)) and the basename (matchBase). If either method detects that the positive pattern matches, the overall result is non-matching (null). This ensures:!**/examples/**correctly reject paths insideexamples/!**/*.jscontinue to work (the basename test catches cases where the full-path regex misses due to./prefix handling)Test results
Compatibility
This change only affects negated patterns that contain path separators when
matchBaseis enabled. Non-negated patterns and slash-free patterns are completely unaffected.