Skip to content

Commit

Permalink
fix: matching with relative parent glob pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Apr 4, 2024
1 parent 9f6331c commit 59e80d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/hot_hook/src/matcher.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolve } from 'node:path'
import picomatch from 'picomatch'

export class Matcher {
Expand All @@ -6,7 +7,14 @@ export class Matcher {

constructor(rootDirectory: string, patterns: picomatch.Glob = []) {
this.#rootDirectory = rootDirectory
this.#matcher = picomatch(patterns || [], { dot: true })

patterns = Array.isArray(patterns) ? patterns : [patterns]
const absolutePatterns = patterns.map((pattern) => {
if (pattern.startsWith('../')) return resolve(rootDirectory, pattern)
return pattern
})

this.#matcher = picomatch(absolutePatterns || [], { dot: true })
}

/**
Expand Down
15 changes: 15 additions & 0 deletions packages/hot_hook/tests/matcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,19 @@ test.group('Matcher', () => {
assert.isTrue(matches.every((match) => matcher.match(match)))
assert.isFalse(notMatches.some((match) => matcher.match(match)))
})

test('works with parent directory', ({ assert }) => {
const matcher = new Matcher('/home/foo/bar', ['../config/**'])

const matches = ['/home/foo/config/index.js', '/home/foo/config/bar/index.js']

const notMatches = [
'/home/config/index.js',
'/home/foo/bar/config/index.js',
'/home/foo/bar/config/test/index.js',
]

assert.isTrue(matches.every((match) => matcher.match(match)))
assert.isFalse(notMatches.some((match) => matcher.match(match)))
})
})

0 comments on commit 59e80d2

Please sign in to comment.