Skip to content

Commit 4cfc1c3

Browse files
bteashellscape
andauthored
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 <[email protected]> Co-authored-by: shellscape <[email protected]>
1 parent 3952b73 commit 4cfc1c3

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

packages/node-resolve/src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,15 @@ export function nodeResolve(opts = {}) {
292292
return importee;
293293
}
294294
// ignore IDs with null character, these belong to other plugins
295-
if (/\0/.test(importee)) return null;
295+
if (importee && importee.includes('\0')) return null;
296296

297297
const { custom = {} } = resolveOptions;
298298
const { 'node-resolve': { resolved: alreadyResolved } = {} } = custom;
299299
if (alreadyResolved) {
300300
return alreadyResolved;
301301
}
302302

303-
if (/\0/.test(importer)) {
303+
if (importer && importer.includes('\0')) {
304304
importer = undefined;
305305
}
306306

packages/pluginutils/src/attachScopes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const attachScopes: AttachScopes = function attachScopes(ast, propertyName = 'sc
8282
let newScope: AttachedScope | undefined;
8383

8484
// create new function scope
85-
if (/Function/.test(node.type)) {
85+
if (node.type.includes('Function')) {
8686
const func = node as estree.Function;
8787
newScope = new Scope({
8888
parent: scope,
@@ -106,7 +106,7 @@ const attachScopes: AttachScopes = function attachScopes(ast, propertyName = 'sc
106106
}
107107

108108
// create new block scope
109-
if (node.type === 'BlockStatement' && !/Function/.test(parent.type)) {
109+
if (node.type === 'BlockStatement' && !parent.type.includes('Function')) {
110110
newScope = new Scope({
111111
parent: scope,
112112
block: true

packages/pluginutils/src/createFilter.ts

+6
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,17 @@ const createFilter: CreateFilter = function createFilter(include?, exclude?, opt
5454

5555
for (let i = 0; i < excludeMatchers.length; ++i) {
5656
const matcher = excludeMatchers[i];
57+
if (matcher instanceof RegExp) {
58+
matcher.lastIndex = 0;
59+
}
5760
if (matcher.test(pathId)) return false;
5861
}
5962

6063
for (let i = 0; i < includeMatchers.length; ++i) {
6164
const matcher = includeMatchers[i];
65+
if (matcher instanceof RegExp) {
66+
matcher.lastIndex = 0;
67+
}
6268
if (matcher.test(pathId)) return true;
6369
}
6470

packages/pluginutils/test/createFilter.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const resolve = (...parts: string[]) => normalizePath(rawResolve(...parts));
88

99
test.beforeEach(() => process.chdir(__dirname));
1010

11-
test('includes by default', (t) => {
11+
test('includes by default ', (t) => {
1212
const filter = createFilter();
1313
t.truthy(filter(resolve('x')));
1414
});
@@ -175,3 +175,27 @@ test('normalizes path when pattern has resolution base', (t) => {
175175
t.truthy(filterPosix(resolve('test/a')));
176176
t.truthy(filterWin(resolve('test/a')));
177177
});
178+
179+
test('pass a regular expression to the include parameter', (t) => {
180+
const filter = createFilter([/zxcvbnmasdfg/]);
181+
t.truthy(filter(resolve('zxcvbnmasdfg')));
182+
t.falsy(filter(resolve('zxcvbnmasdfe')));
183+
});
184+
185+
test('pass a regular expression to the include parameter with g flag', (t) => {
186+
const filter = createFilter([/zxcvbnmasdfg/g]);
187+
t.truthy(filter(resolve('zxcvbnmasdfg')));
188+
t.truthy(filter(resolve('zxcvbnmasdfg')));
189+
});
190+
191+
test('pass a regular expression to the exclude parameter', (t) => {
192+
const filter = createFilter(null, [/zxcvbnmasdfg/]);
193+
t.falsy(filter(resolve('zxcvbnmasdfg')));
194+
t.truthy(filter(resolve('zxcvbnmasdfe')));
195+
});
196+
197+
test('pass a regular expression to the exclude parameter with g flag', (t) => {
198+
const filter = createFilter(null, [/zxcvbnmasdfg/g]);
199+
t.falsy(filter(resolve('zxcvbnmasdfg')));
200+
t.falsy(filter(resolve('zxcvbnmasdfg')));
201+
});

0 commit comments

Comments
 (0)