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(pluginutils): fix path normalisation for createFilter #1161

Merged
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
4 changes: 2 additions & 2 deletions packages/pluginutils/src/createFilter.ts
Expand Up @@ -9,7 +9,7 @@ import normalizePath from './normalizePath';

function getMatcherString(id: string, resolutionBase: string | false | null | undefined) {
if (resolutionBase === false || isAbsolute(id) || id.startsWith('*')) {
return id;
return normalizePath(id);
}

// resolve('') is valid and will default to process.cwd()
Expand All @@ -20,7 +20,7 @@ function getMatcherString(id: string, resolutionBase: string | false | null | un
// 1. the basePath has been normalized to use /
// 2. the incoming glob (id) matcher, also uses /
// otherwise Node will force backslash (\) on windows
return posix.join(basePath, id);
return posix.join(basePath, normalizePath(id));
}

const createFilter: CreateFilter = function createFilter(include?, exclude?, options?) {
Expand Down
28 changes: 28 additions & 0 deletions packages/pluginutils/test/createFilter.ts
Expand Up @@ -134,6 +134,34 @@ test('does not add current working directory when pattern is an absolute path',
t.falsy(filter(resolve('..', 'c')));
});

test('normalizes path when pattern is an absolute path', (t) => {
const filterPosix = createFilter([`${resolve('.')}/*`]);
const filterWin = createFilter([`${resolve('.')}\\*`]);

t.truthy(filterPosix(resolve('a')));
t.truthy(filterWin(resolve('a')));
});

test('normalizes path when pattern starts with *', (t) => {
const filterPosix = createFilter([`**/a`]);
const filterWin = createFilter([`**\\a`]);

t.truthy(filterPosix(resolve('a')));
t.truthy(filterWin(resolve('a')));
});

test('normalizes path when pattern has resolution base', (t) => {
const filterPosix = createFilter([`test/*`], [], {
resolve: __dirname
});
const filterWin = createFilter([`test\\*`], [], {
resolve: __dirname
});

t.truthy(filterPosix(resolve('test/a')));
t.truthy(filterWin(resolve('test/a')));
});

test('does not add current working directory when pattern starts with a glob', (t) => {
const filter = createFilter(['**/*']);
t.truthy(filter(resolve('a')));
Expand Down