Skip to content

Commit

Permalink
fix: do not try to apply patterns to the path with the trailing slash…
Browse files Browse the repository at this point in the history
… for non-directory entry
  • Loading branch information
mrmlnc committed May 31, 2022
1 parent f58b8d5 commit a1550b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/providers/filters/entry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
});
});
});
});

Expand Down
22 changes: 14 additions & 8 deletions src/providers/filters/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as utils from '../../utils';
export default class EntryFilter {
public readonly index: Map<string, undefined> = 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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
6 changes: 2 additions & 4 deletions src/tests/smoke/regular.smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
])
]);

0 comments on commit a1550b1

Please sign in to comment.