Skip to content

Commit

Permalink
reverse-exports: fix matching pattern to use single asterisk as catch…
Browse files Browse the repository at this point in the history
…-all
  • Loading branch information
lolmaus committed Nov 21, 2023
1 parent 30a1199 commit df09d0a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
4 changes: 0 additions & 4 deletions packages/reverse-exports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/minimatch": "^3.0.4"
},
"dependencies": {
"minimatch": "^3.0.4",
"resolve.exports": "^2.0.2"
}
}
21 changes: 15 additions & 6 deletions packages/reverse-exports/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { posix } from 'path';
import minimatch from 'minimatch';
import { exports as resolveExports } from 'resolve.exports';

type Exports = string | string[] | { [key: string]: Exports };
Expand Down Expand Up @@ -82,12 +81,9 @@ export default function reversePackageExports(
}

const maybeKeyValuePair = _findPathRecursively(exportsObj, candidate => {
// miminatch does not treat directories as full of content without glob
if (candidate.endsWith('/')) {
candidate += '**';
}
const regex = new RegExp(_prepareStringForRegex(candidate));

return minimatch(relativePath, candidate);
return regex.test(relativePath);
});

if (!maybeKeyValuePair) {
Expand All @@ -114,3 +110,16 @@ export default function reversePackageExports(

return resolvedPath.replace(/^./, name);
}

export function _prepareStringForRegex(input: string): string {
let result = input
.split('*')
.map(substr => substr.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&'))
.join('.*');

if (result.endsWith('/')) {
result += '.*';
}

return `^${result}$`;
}
17 changes: 16 additions & 1 deletion packages/reverse-exports/tests/reverse-exports.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import reversePackageExports, { _findPathRecursively } from '../src';
import reversePackageExports, { _findPathRecursively, _prepareStringForRegex } from '../src';

describe('reverse exports', function () {
it('exports is missing', function () {
Expand Down Expand Up @@ -278,3 +278,18 @@ describe('_findKeyRecursively', function () {
expect(_findPathRecursively(exports, str => str === './missing-path.js')).toBe(undefined);
});
});

///

describe('_prepareStringForRegex', function () {
[
{ input: './foo', expected: '^\\.\\/foo$' },
{ input: './foo.js', expected: '^\\.\\/foo\\.js$' },
{ input: './foo/*.js', expected: '^\\.\\/foo\\/.*\\.js$' },
{ input: './foo/', expected: '^\\.\\/foo\\/.*$' },
].forEach(({ input, expected }) => {
it(input, function () {
expect(_prepareStringForRegex(input)).toStrictEqual(expected);
});
});
});
7 changes: 0 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit df09d0a

Please sign in to comment.