From feca9eabbb2986f638e25ef45a2c725ebd1e99d1 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Wed, 16 Oct 2024 13:11:29 +0800 Subject: [PATCH] fix(pluginutils): optimize `createFilter` the matching rules when regexp carry flags --- packages/pluginutils/src/createFilter.ts | 6 ++++++ packages/pluginutils/test/createFilter.ts | 24 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/packages/pluginutils/src/createFilter.ts b/packages/pluginutils/src/createFilter.ts index 9f2f093f6..86f9d3be5 100755 --- a/packages/pluginutils/src/createFilter.ts +++ b/packages/pluginutils/src/createFilter.ts @@ -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; } diff --git a/packages/pluginutils/test/createFilter.ts b/packages/pluginutils/test/createFilter.ts index 234bbc205..66116b5a4 100755 --- a/packages/pluginutils/test/createFilter.ts +++ b/packages/pluginutils/test/createFilter.ts @@ -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'))); +});