Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not try to apply patterns to the path with the trailing slash for non-directory entry #360

Merged
merged 1 commit into from May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/providers/filters/entry.spec.ts
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
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
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'
}
])
]);