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

refactor(pluginutils,node-resolve): replace test with includes #1787

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
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
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')));
});