From a1550b1e73a6d90085fb154c6dd49e2c24a0685f Mon Sep 17 00:00:00 2001 From: mrmlnc Date: Mon, 30 May 2022 20:13:52 +0300 Subject: [PATCH] fix: do not try to apply patterns to the path with the trailing slash for non-directory entry --- src/providers/filters/entry.spec.ts | 14 ++++++++++++++ src/providers/filters/entry.ts | 22 ++++++++++++++-------- src/tests/smoke/regular.smoke.ts | 6 ++---- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/providers/filters/entry.spec.ts b/src/providers/filters/entry.spec.ts index 4d3be89f..ee72f42d 100644 --- a/src/providers/filters/entry.spec.ts +++ b/src/providers/filters/entry.spec.ts @@ -226,6 +226,20 @@ describe('Providers → Filters → Entry', () => { positive: ['**/*'] }); }); + + it('should try to apply patterns to the path with the trailing slash for directory entry', () => { + accept(DIRECTORY_ENTRY, { + positive: ['**/'], + options: { onlyFiles: false } + }); + }); + + it('should not try to apply patterns to the path with the trailing slash for non-directory entry', () => { + reject(FILE_ENTRY, { + positive: ['**/'], + options: { onlyFiles: false } + }); + }); }); }); diff --git a/src/providers/filters/entry.ts b/src/providers/filters/entry.ts index 899eeb18..70082b05 100644 --- a/src/providers/filters/entry.ts +++ b/src/providers/filters/entry.ts @@ -5,7 +5,7 @@ import * as utils from '../../utils'; export default class EntryFilter { public readonly index: Map = new Map(); - constructor(private readonly _settings: Settings, private readonly _micromatchOptions: MicromatchOptions) { } + constructor(private readonly _settings: Settings, private readonly _micromatchOptions: MicromatchOptions) {} public getFilter(positive: Pattern[], negative: Pattern[]): EntryFilterFunction { const positiveRe = utils.pattern.convertPatternsToRe(positive, this._micromatchOptions); @@ -28,8 +28,9 @@ export default class EntryFilter { } const filepath = this._settings.baseNameMatch ? entry.name : entry.path; + const isDirectory = entry.dirent.isDirectory(); - const isMatched = this._isMatchToPatterns(filepath, positiveRe) && !this._isMatchToPatterns(entry.path, negativeRe); + const isMatched = this._isMatchToPatterns(filepath, positiveRe, isDirectory) && !this._isMatchToPatterns(entry.path, negativeRe, isDirectory); if (this._settings.unique && isMatched) { this._createIndexRecord(entry); @@ -64,13 +65,18 @@ export default class EntryFilter { return utils.pattern.matchAny(fullpath, patternsRe); } - /** - * First, just trying to apply patterns to the path. - * Second, trying to apply patterns to the path with final slash. - */ - private _isMatchToPatterns(entryPath: string, patternsRe: PatternRe[]): boolean { + private _isMatchToPatterns(entryPath: string, patternsRe: PatternRe[], isDirectory: boolean): boolean { const filepath = utils.path.removeLeadingDotSegment(entryPath); - return utils.pattern.matchAny(filepath, patternsRe) || utils.pattern.matchAny(filepath + '/', patternsRe); + // Trying to match files and directories by patterns. + const isMatched = utils.pattern.matchAny(filepath, patternsRe); + + // A pattern with a trailling slash can be used for directory matching. + // To apply such pattern, we need to add a tralling slash to the path. + if (!isMatched && isDirectory) { + return utils.pattern.matchAny(filepath + '/', patternsRe); + } + + return isMatched; } } diff --git a/src/tests/smoke/regular.smoke.ts b/src/tests/smoke/regular.smoke.ts index 53f161ac..9e7613d8 100644 --- a/src/tests/smoke/regular.smoke.ts +++ b/src/tests/smoke/regular.smoke.ts @@ -508,8 +508,6 @@ smoke.suite('Smoke → Regular (relative & ignore)', [ smoke.suite('Smoke -> Regular (negative group)', [ { pattern: '**/!(*.md)', - cwd: 'fixtures/first', - broken: true, - issue: 357 + cwd: 'fixtures/first' } -]) +]);