Skip to content

Commit

Permalink
fix(pluginutils): optimize createFilter the matching rules when reg…
Browse files Browse the repository at this point in the history
…exp carry flags
  • Loading branch information
btea committed Oct 16, 2024
1 parent 100af13 commit feca9ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/pluginutils/src/createFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@ const createFilter: CreateFilter = function createFilter(include?, exclude?, opt

for (let i = 0; i < excludeMatchers.length; ++i) {
const matcher = excludeMatchers[i];
if (matcher instanceof RegExp) {
matcher.lastIndex = 0;
}
if (matcher.test(pathId)) return false;
}

for (let i = 0; i < includeMatchers.length; ++i) {
const matcher = includeMatchers[i];
if (matcher instanceof RegExp) {
matcher.lastIndex = 0;
}
if (matcher.test(pathId)) return true;
}

Expand Down
24 changes: 24 additions & 0 deletions packages/pluginutils/test/createFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,27 @@ test('normalizes path when pattern has resolution base', (t) => {
t.truthy(filterPosix(resolve('test/a')));
t.truthy(filterWin(resolve('test/a')));
});

test('pass a regular expression to the include parameter', (t) => {
const filter = createFilter([/zxcvbnmasdfg/]);
t.truthy(filter(resolve('zxcvbnmasdfg')));
t.falsy(filter(resolve('zxcvbnmasdfe')));
});

test('pass a regular expression to the include parameter with g flag', (t) => {
const filter = createFilter([/zxcvbnmasdfg/g]);
t.truthy(filter(resolve('zxcvbnmasdfg')));
t.truthy(filter(resolve('zxcvbnmasdfg')));
});

test('pass a regular expression to the exclude parameter', (t) => {
const filter = createFilter(null, [/zxcvbnmasdfg/]);
t.falsy(filter(resolve('zxcvbnmasdfg')));
t.truthy(filter(resolve('zxcvbnmasdfe')));
});

test('pass a regular expression to the exclude parameter with g flag', (t) => {
const filter = createFilter(null, [/zxcvbnmasdfg/g]);
t.falsy(filter(resolve('zxcvbnmasdfg')));
t.falsy(filter(resolve('zxcvbnmasdfg')));
});

0 comments on commit feca9ea

Please sign in to comment.