From 4cfc1c31c2e03851fd3dca5808d20f93b315bb02 Mon Sep 17 00:00:00 2001
From: btea <2356281422@qq.com>
Date: Mon, 16 Dec 2024 00:14:50 +0800
Subject: [PATCH] refactor(pluginutils,node-resolve): replace `test` with
`includes` (#1787)
* refactor: replace `test` with `includes`
* fix: update
* fix(pluginutils): optimize `createFilter` the matching rules when regexp carry flags
* chore: bump workflows
---------
Co-authored-by: Andrew Powell
Co-authored-by: shellscape
---
packages/node-resolve/src/index.js | 4 ++--
packages/pluginutils/src/attachScopes.ts | 4 ++--
packages/pluginutils/src/createFilter.ts | 6 ++++++
packages/pluginutils/test/createFilter.ts | 26 ++++++++++++++++++++++-
4 files changed, 35 insertions(+), 5 deletions(-)
diff --git a/packages/node-resolve/src/index.js b/packages/node-resolve/src/index.js
index a675247aa..7e0614f58 100644
--- a/packages/node-resolve/src/index.js
+++ b/packages/node-resolve/src/index.js
@@ -292,7 +292,7 @@ export function nodeResolve(opts = {}) {
return importee;
}
// ignore IDs with null character, these belong to other plugins
- if (/\0/.test(importee)) return null;
+ if (importee && importee.includes('\0')) return null;
const { custom = {} } = resolveOptions;
const { 'node-resolve': { resolved: alreadyResolved } = {} } = custom;
@@ -300,7 +300,7 @@ export function nodeResolve(opts = {}) {
return alreadyResolved;
}
- if (/\0/.test(importer)) {
+ if (importer && importer.includes('\0')) {
importer = undefined;
}
diff --git a/packages/pluginutils/src/attachScopes.ts b/packages/pluginutils/src/attachScopes.ts
index fc6392ddb..b8a33a8ea 100755
--- a/packages/pluginutils/src/attachScopes.ts
+++ b/packages/pluginutils/src/attachScopes.ts
@@ -82,7 +82,7 @@ const attachScopes: AttachScopes = function attachScopes(ast, propertyName = 'sc
let newScope: AttachedScope | undefined;
// create new function scope
- if (/Function/.test(node.type)) {
+ if (node.type.includes('Function')) {
const func = node as estree.Function;
newScope = new Scope({
parent: scope,
@@ -106,7 +106,7 @@ const attachScopes: AttachScopes = function attachScopes(ast, propertyName = 'sc
}
// create new block scope
- if (node.type === 'BlockStatement' && !/Function/.test(parent.type)) {
+ if (node.type === 'BlockStatement' && !parent.type.includes('Function')) {
newScope = new Scope({
parent: scope,
block: true
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..783182eac 100755
--- a/packages/pluginutils/test/createFilter.ts
+++ b/packages/pluginutils/test/createFilter.ts
@@ -8,7 +8,7 @@ const resolve = (...parts: string[]) => normalizePath(rawResolve(...parts));
test.beforeEach(() => process.chdir(__dirname));
-test('includes by default', (t) => {
+test('includes by default ', (t) => {
const filter = createFilter();
t.truthy(filter(resolve('x')));
});
@@ -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')));
+});