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): Escape glob characters in folder #84

Merged
merged 1 commit into from Dec 20, 2019
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
10 changes: 8 additions & 2 deletions packages/pluginutils/src/createFilter.ts
@@ -1,16 +1,22 @@
import { resolve, sep } from 'path';
import { resolve, join, sep } from 'path';

import mm from 'micromatch';

import { CreateFilter } from '../types';

import ensureArray from './utils/ensureArray';

const ESCAPE_IN_PATH = '()+@!'

function getMatcherString(id: string, resolutionBase: string | false | null | undefined) {
if (resolutionBase === false) {
return id;
}
return resolve(...(typeof resolutionBase === 'string' ? [resolutionBase, id] : [id]));
let basePath = typeof resolutionBase === 'string' ? resolve(resolutionBase) : process.cwd();
for (const char of ESCAPE_IN_PATH) {
basePath = basePath.replace(new RegExp(`\\${char}`, 'g'), `\\${char}`);
}
return join(basePath, id);
}

const createFilter: CreateFilter = function createFilter(include?, exclude?, options?) {
Expand Down
10 changes: 10 additions & 0 deletions packages/pluginutils/test/createFilter.ts
Expand Up @@ -4,6 +4,8 @@ import test from 'ava';

import { createFilter } from '../';

test.beforeEach(() => process.chdir(__dirname));

test('includes by default', (t) => {
const filter = createFilter();
t.truthy(filter(resolve('x')));
Expand Down Expand Up @@ -107,3 +109,11 @@ test('includes names starting with a "."', (t) => {
t.truthy(filter(resolve('.a')));
t.truthy(filter(resolve('.x/a')));
});

test.serial('includes names containing parenthesis', (t) => {
process.chdir(resolve(__dirname, 'fixtures/folder-with (parens)'));
const filter = createFilter(['*.ts+(|x)', '**/*.ts+(|x)'], ['*.d.ts', '**/*.d.ts']);
t.truthy(filter(resolve('folder (test)/src/main.tsx')));
t.truthy(filter(resolve('.x/(test)a.ts')));
t.falsy(filter(resolve('.x/(test)a.d.ts')));
});
Empty file.