Skip to content

Commit f781adc

Browse files
committed
refactor pattern matcher to only match files with supported extensions when expanding directories
- Modified matchPatterns() to generate glob pattern with supported file extensions (.txt, .adblock) when expanding directories - Added SUPPORTED_FILE_EXTENSIONS import from cli/constants - Changed directory glob from `/**/*` to `/**/*.{txt,adblock}` format - Updated test to verify only supported extensions are matched, excluding .ts and .js files - Added test files (filter.txt, rules.adblock) to verify
1 parent ebf86e2 commit f781adc

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/cli/utils/pattern-matcher/matcher.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import isGlob from 'is-glob';
22

3+
import { SUPPORTED_FILE_EXTENSIONS } from '../../constants';
34
import { type FileSystemAdapter } from '../fs-adapter';
45
import { type PathAdapter } from '../path-adapter';
56

@@ -90,8 +91,9 @@ export async function matchPatterns(
9091
expandedPatterns.push(absPath);
9192
patternMap.set(pattern, []);
9293
} else if (stats.isDirectory) {
93-
// Directory - expand to glob pattern
94-
const dirGlob = `${absPath.replace(/\/+$/, '')}/**/*`;
94+
// Directory - expand to glob pattern with supported file extensions
95+
const extensions = Array.from(SUPPORTED_FILE_EXTENSIONS).map((e) => e.slice(1)).join(',');
96+
const dirGlob = `${absPath.replace(/\/+$/, '')}/**/*.{${extensions}}`;
9597
if (debug) {
9698
debug.log(`Pattern "${pattern}" is a directory, expanding to: ${dirGlob}`);
9799
}

test/cli/utils/pattern-matcher.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ describe('pattern-matcher', () => {
3636
await writeFile(join(testDir, 'src', 'index.ts'), 'typescript');
3737
await writeFile(join(testDir, 'src', 'utils.ts'), 'utils');
3838
await writeFile(join(testDir, 'src', 'test.js'), 'test');
39+
await writeFile(join(testDir, 'src', 'filter.txt'), 'filter');
40+
await writeFile(join(testDir, 'src', 'rules.adblock'), 'rules');
3941

4042
await mkdir(join(testDir, 'docs'), { recursive: true });
4143
await writeFile(join(testDir, 'docs', 'readme.md'), 'readme');
@@ -143,17 +145,22 @@ describe('pattern-matcher', () => {
143145
});
144146

145147
describe('matchPatterns - directory patterns', () => {
146-
test('should expand directory to all files', async () => {
148+
test('should expand directory to files with supported extensions', async () => {
147149
const result = await matchPatterns(
148150
['src'],
149151
fs,
150152
pathAdapter,
151153
{ cwd: testDir },
152154
);
153155

154-
expect(result.files.length).toBeGreaterThan(0);
156+
// Should only match .txt and .adblock files, not .ts or .js files
157+
expect(result.files.length).toBe(2);
155158
expect(result.files.every((f) => f.includes('/src/'))).toBe(true);
156-
expect(result.patternMap.get('src')?.length).toBeGreaterThan(0);
159+
expect(result.files.some((f) => f.endsWith('filter.txt'))).toBe(true);
160+
expect(result.files.some((f) => f.endsWith('rules.adblock'))).toBe(true);
161+
expect(result.files.some((f) => f.endsWith('.ts'))).toBe(false);
162+
expect(result.files.some((f) => f.endsWith('.js'))).toBe(false);
163+
expect(result.patternMap.get('src')?.length).toBe(2);
157164
});
158165

159166
test('should match files in nested directory', async () => {

0 commit comments

Comments
 (0)