Skip to content

Commit a93de9d

Browse files
NotWoodsshellscape
authored andcommitted
fix(pluginutils): Escape glob characters in folder (#84)
1 parent ad861da commit a93de9d

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

packages/pluginutils/src/createFilter.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
import { resolve, sep } from 'path';
1+
import { resolve, join, sep } from 'path';
22

33
import mm from 'micromatch';
44

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

77
import ensureArray from './utils/ensureArray';
88

9+
const ESCAPE_IN_PATH = '()+@!'
10+
911
function getMatcherString(id: string, resolutionBase: string | false | null | undefined) {
1012
if (resolutionBase === false) {
1113
return id;
1214
}
13-
return resolve(...(typeof resolutionBase === 'string' ? [resolutionBase, id] : [id]));
15+
let basePath = typeof resolutionBase === 'string' ? resolve(resolutionBase) : process.cwd();
16+
for (const char of ESCAPE_IN_PATH) {
17+
basePath = basePath.replace(new RegExp(`\\${char}`, 'g'), `\\${char}`);
18+
}
19+
return join(basePath, id);
1420
}
1521

1622
const createFilter: CreateFilter = function createFilter(include?, exclude?, options?) {

packages/pluginutils/test/createFilter.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import test from 'ava';
44

55
import { createFilter } from '../';
66

7+
test.beforeEach(() => process.chdir(__dirname));
8+
79
test('includes by default', (t) => {
810
const filter = createFilter();
911
t.truthy(filter(resolve('x')));
@@ -107,3 +109,11 @@ test('includes names starting with a "."', (t) => {
107109
t.truthy(filter(resolve('.a')));
108110
t.truthy(filter(resolve('.x/a')));
109111
});
112+
113+
test.serial('includes names containing parenthesis', (t) => {
114+
process.chdir(resolve(__dirname, 'fixtures/folder-with (parens)'));
115+
const filter = createFilter(['*.ts+(|x)', '**/*.ts+(|x)'], ['*.d.ts', '**/*.d.ts']);
116+
t.truthy(filter(resolve('folder (test)/src/main.tsx')));
117+
t.truthy(filter(resolve('.x/(test)a.ts')));
118+
t.falsy(filter(resolve('.x/(test)a.d.ts')));
119+
});

packages/pluginutils/test/fixtures/folder-with (parens)/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)