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): optimize createFilter the matching rules when regexp carry flags #1788

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')));
});