Skip to content

Commit

Permalink
refactor(pluginutils,node-resolve): replace test with includes (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
3 people authored Dec 15, 2024
1 parent 3952b73 commit 4cfc1c3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,15 @@ 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;
if (alreadyResolved) {
return alreadyResolved;
}

if (/\0/.test(importer)) {
if (importer && importer.includes('\0')) {
importer = undefined;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/pluginutils/src/attachScopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
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
26 changes: 25 additions & 1 deletion packages/pluginutils/test/createFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
});
Expand Down 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 4cfc1c3

Please sign in to comment.