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: simplify filtering by moving back to picomatch #26

Merged
merged 3 commits into from
Mar 31, 2024
Merged
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
Binary file modified bun.lockb
Binary file not shown.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"connect": "^3.7.0",
"express": "^4.18.2",
"freeport-async": "^2.0.0",
"fuse.js": "^7.0.0",
"getenv": "^1.0.0",
"morgan": "^1.10.0",
"open": "^8.4.2",
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ export { StatsFileSource } from './data/StatsFileSource';

export { AtlasError, AtlasValidationError } from './utils/errors';
export { createAtlasMiddleware } from './utils/middleware';
export { fuzzyFilterModules } from './utils/search';
export { createStatsFile, validateStatsFile, getStatsMetdata, getStatsPath } from './utils/stats';
41 changes: 0 additions & 41 deletions src/utils/search.ts

This file was deleted.

4 changes: 2 additions & 2 deletions webui/src/app/api/stats/[entry]/modules/index+api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { statsModuleFiltersFromUrlParams } from '~/components/forms/StatsModuleFilter';
import { getSource } from '~/utils/atlas';
import { globFilterModules } from '~/utils/search';
import { type StatsEntry, type StatsModule } from '~core/data/types';
import { fuzzyFilterModules } from '~core/utils/search';

/** The partial module data, when listing all available modules from a stats entry */
export type ModuleMetadata = Omit<StatsModule, 'source' | 'output'> & {
Expand Down Expand Up @@ -62,7 +62,7 @@ function filterModules(request: Request, stats: StatsEntry): ModuleMetadata[] {
modules = modules.filter((module) => !module.package);
}

return fuzzyFilterModules(modules, filters).map((module) => ({
return globFilterModules(modules, stats.projectRoot, filters).map((module) => ({
...module,
source: undefined,
output: undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Note(cedric): this file was copied from core, and isn't currently used as test.
import { describe, expect, it } from 'bun:test';

import { type StatsModule } from '../../data/types';
import { fuzzyFilterModules } from '../search';
import { globFilterModules } from '../search';

import { type StatsModule } from '~core/data/types';

const projectRoot = '/user/expo';
const modules = [
asModule({ path: '/user/expo/node_modules/lodash/lodash.js' }),
asModule({ path: '/user/expo/node_modules/expo/package.json' }),
Expand All @@ -14,39 +17,39 @@ function asModule(module: Pick<StatsModule, 'path'>) {
return module as StatsModule;
}

describe.skip('fuzzyGlobSearch', () => {
describe.skip('globFilterModules', () => {
describe('include', () => {
it('filters by exact file name', () => {
expect(fuzzyFilterModules(modules, { include: 'index.ts' })).toEqual([
expect(globFilterModules(modules, projectRoot, { include: 'index.ts' })).toEqual([
asModule({ path: '/user/expo/src/index.ts' }),
asModule({ path: '/user/expo/src/app/index.ts' }),
]);
});

it('filters by exact directory name', () => {
expect(fuzzyFilterModules(modules, { include: 'node_modules' })).toEqual([
expect(globFilterModules(modules, projectRoot, { include: 'node_modules' })).toEqual([
asModule({ path: '/user/expo/node_modules/lodash/lodash.js' }),
asModule({ path: '/user/expo/node_modules/expo/package.json' }),
]);
});

it('filters by multiple exact file or directory names', () => {
expect(fuzzyFilterModules(modules, { include: 'index.ts, lodash' })).toEqual([
expect(globFilterModules(modules, projectRoot, { include: 'index.ts, lodash' })).toEqual([
asModule({ path: '/user/expo/src/index.ts' }),
asModule({ path: '/user/expo/src/app/index.ts' }),
asModule({ path: '/user/expo/node_modules/lodash/lodash.js' }),
]);
});

it('filters using star pattern on directory', () => {
expect(fuzzyFilterModules(modules, { include: 'src/*' })).toEqual([
expect(globFilterModules(modules, projectRoot, { include: 'src/*' })).toEqual([
asModule({ path: '/user/expo/src/index.ts' }),
asModule({ path: '/user/expo/src/app/index.ts' }),
]);
});

it('filters using star pattern on nested directory', () => {
expect(fuzzyFilterModules(modules, { include: 'expo/src/**' })).toEqual([
expect(globFilterModules(modules, projectRoot, { include: 'expo/src/**' })).toEqual([
asModule({ path: '/user/expo/src/index.ts' }),
asModule({ path: '/user/expo/src/app/index.ts' }),
]);
Expand All @@ -55,34 +58,34 @@ describe.skip('fuzzyGlobSearch', () => {

describe('exclude', () => {
it('filters by exact file name', () => {
expect(fuzzyFilterModules(modules, { exclude: 'index.ts' })).toEqual([
expect(globFilterModules(modules, projectRoot, { exclude: 'index.ts' })).toEqual([
asModule({ path: '/user/expo/node_modules/lodash/lodash.js' }),
asModule({ path: '/user/expo/node_modules/expo/package.json' }),
]);
});

it('filters by exact directory name', () => {
expect(fuzzyFilterModules(modules, { exclude: 'node_modules' })).toEqual([
expect(globFilterModules(modules, projectRoot, { exclude: 'node_modules' })).toEqual([
asModule({ path: '/user/expo/src/index.ts' }),
asModule({ path: '/user/expo/src/app/index.ts' }),
]);
});

it('filters by multiple exact file or directory names', () => {
expect(fuzzyFilterModules(modules, { exclude: 'index.ts, lodash' })).toEqual([
expect(globFilterModules(modules, projectRoot, { exclude: 'index.ts, lodash' })).toEqual([
asModule({ path: '/user/expo/node_modules/expo/package.json' }),
]);
});

it('filters using star pattern on directory', () => {
expect(fuzzyFilterModules(modules, { exclude: 'src/*' })).toEqual([
expect(globFilterModules(modules, projectRoot, { exclude: 'src/*' })).toEqual([
asModule({ path: '/user/expo/node_modules/lodash/lodash.js' }),
asModule({ path: '/user/expo/node_modules/expo/package.json' }),
]);
});

it('filters using star pattern on nested directory', () => {
expect(fuzzyFilterModules(modules, { exclude: 'expo/src/**' })).toEqual([
expect(globFilterModules(modules, projectRoot, { exclude: 'expo/src/**' })).toEqual([
asModule({ path: '/user/expo/node_modules/lodash/lodash.js' }),
asModule({ path: '/user/expo/node_modules/expo/package.json' }),
]);
Expand Down
42 changes: 42 additions & 0 deletions webui/src/utils/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import path from 'path';
import picomatch from 'picomatch';

import { type StatsModule } from '~core/data/types';

type ModuleFilters = {
include?: string;
exclude?: string;
};

/**
* Filter the modules based on the include and exclude glob patterns.
* Note, you can provide multiple patterns using the comma separator.
* This also only searches the relative module path from project root, avoiding false positives.
*/
export function globFilterModules(
items: StatsModule[],
projectRoot: string,
options: ModuleFilters
) {
if (!options.include && !options.exclude) {
return items;
}

const matcher = picomatch(options.include ? splitPattern(options.include) : '**', {
cwd: '',
dot: true,
nocase: true,
contains: true,
ignore: !options.exclude ? undefined : splitPattern(options.exclude),
});

return items.filter((item) => matcher(path.relative(projectRoot, item.path)));
}

/**
* Split the comma separated string into an array of separate patterns.
* This splits on any combination of `,` and whitespaces.
*/
function splitPattern(pattern: string) {
return pattern.split(/\s*,\s*/);
}