Skip to content

Commit 205a257

Browse files
committed
Use import syntax for matcher
1 parent ead5629 commit 205a257

File tree

4 files changed

+58
-30
lines changed

4 files changed

+58
-30
lines changed

lib/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@
3434
"devDependencies": {
3535
"@repo/eslint-config": "workspace:*",
3636
"@repo/typescript-config": "workspace:*",
37+
"@types/micromatch": "^4.0.9",
3738
"@types/node": "^24.3.0",
39+
"@types/picomatch": "^4.0.2",
3840
"@vitest/coverage-v8": "^3.2.4",
3941
"esbuild-plugin-rdi": "^0.0.0",
42+
"micromatch": "^4.0.8",
43+
"picomatch": "^4.0.3",
4044
"tsup": "^8.5.0",
4145
"typescript": "^5.9.2",
4246
"vite-tsconfig-paths": "^5.1.4",

lib/src/matcher.test.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
1+
import { describe, it, expect } from "vitest";
22
import { basicMatcher, loadMatcher } from "./matcher";
33

44
describe("basicMatcher", () => {
@@ -44,34 +44,24 @@ describe("basicMatcher", () => {
4444
expect(basicMatcher.isMatch("a.b.c", ["a.**"])).toBe(true);
4545
expect(basicMatcher.isMatch("a.b.c", ["a.\\*\\*"])).toBe(false);
4646
expect(basicMatcher.isMatch("a.**", ["a.\\*\\*"])).toBe(true);
47+
expect(basicMatcher.isMatch("a.c.b", ["a.***"])).toBe(false);
4748
});
4849
});
4950

5051
describe("loadMatcher", () => {
51-
const realRequire = require;
52-
53-
beforeEach(() => {
54-
vi.resetModules();
55-
});
56-
57-
afterEach(() => {
58-
// restore require if patched
59-
global.require = realRequire;
60-
});
61-
62-
it("loads micromatch when available", () => {
63-
const mm = loadMatcher("micromatch");
52+
it("loads micromatch when available", async () => {
53+
const mm = await loadMatcher("micromatch");
6454
expect(mm.isMatch("foo", ["foo"])).toBe(true);
6555
expect(mm.isMatch("bar", ["foo"])).toBe(false);
6656
});
6757

68-
it("loads picomatch when available", () => {
69-
const pm = loadMatcher("picomatch");
58+
it("loads picomatch when available", async () => {
59+
const pm = await loadMatcher("picomatch");
7060
expect(pm.isMatch("okmyfield", ["ok*"])).toBe(true);
7161
expect(pm.isMatch("fail", ["pattern"])).toBe(false);
7262
});
7363

7464
it("throws on unknown matcher", () => {
75-
expect(() => loadMatcher("invalid" as any)).toThrow(/Unknown matcher/);
65+
expect(() => loadMatcher("invalid" as any)).rejects.toThrow(/Unknown matcher/);
7666
});
7767
});

lib/src/matcher.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ export const basicMatcher: Matcher = {
3535
* @returns A Matcher implementation.
3636
*/
3737

38-
export const loadMatcher = (name: "micromatch" | "picomatch"): Matcher => {
38+
export const loadMatcher = async (name: "micromatch" | "picomatch"): Promise<Matcher> => {
3939
if (name === "micromatch") {
4040
let micromatch: any;
4141
try {
42-
micromatch = require("micromatch");
42+
micromatch = await import("micromatch");
4343
} catch {
44+
/* v8 ignore next 4 - difficult to simulate this case with micromatch in devDeps */
4445
throw new Error(
4546
`micromatch is not installed. Please add it as a dependency if you want to use it.`,
4647
);
@@ -51,6 +52,7 @@ export const loadMatcher = (name: "micromatch" | "picomatch"): Matcher => {
5152
try {
5253
return micromatch.isMatch(str, pats);
5354
} catch (err) {
55+
/* v8 ignore next 4 - difficult to simulate this case with micromatch/picomatch in devDeps */
5456
throw new Error(`micromatch failed to run isMatch: ${(err as Error).message}`);
5557
}
5658
},
@@ -60,8 +62,9 @@ export const loadMatcher = (name: "micromatch" | "picomatch"): Matcher => {
6062
if (name === "picomatch") {
6163
let picomatch: any;
6264
try {
63-
picomatch = require("picomatch");
65+
picomatch = (await import("picomatch")).default;
6466
} catch {
67+
/* v8 ignore next 4 - difficult to simulate this case with micromatch/picomatch in devDeps */
6568
throw new Error(
6669
`picomatch is not installed. Please add it as a dependency if you want to use it.`,
6770
);
@@ -73,6 +76,7 @@ export const loadMatcher = (name: "micromatch" | "picomatch"): Matcher => {
7376
const fn = picomatch(pats);
7477
return fn(str);
7578
} catch (err) {
79+
/* v8 ignore next 4 - difficult to simulate this case with micromatch/picomatch in devDeps */
7680
throw new Error(`picomatch failed to run isMatch: ${(err as Error).message}`);
7781
}
7882
},
@@ -139,6 +143,7 @@ const matchSegments = (strSegments: string[], patternSegments: string[]): boolea
139143
return true;
140144
}
141145
}
146+
/* v8 ignore next 2 - unreachable in most cases */
142147
return false;
143148
}
144149

pnpm-lock.yaml

Lines changed: 39 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)