Skip to content
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
49 changes: 2 additions & 47 deletions dist/rules/matcher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createHash } from "node:crypto";
import picomatch from "picomatch";
const compiledPatternSets = new Map();
export function matchRule(input) {
if (input.isSingleFile) {
Expand Down Expand Up @@ -77,53 +78,7 @@ function compilePatternSet(patterns) {
return { positivePatterns, negativeMatchers };
}
function createGlobMatcher(pattern) {
const expression = globToRegExp(normalizePath(pattern));
return (path) => expression.test(path);
}
function globToRegExp(pattern) {
let source = "^";
for (let index = 0; index < pattern.length; index += 1) {
const char = pattern[index];
const nextChar = pattern[index + 1];
if (char === "*" && nextChar === "*") {
const afterGlobStar = pattern[index + 2];
if (afterGlobStar === "/") {
source += "(?:.*/)?";
index += 2;
}
else {
source += ".*";
index += 1;
}
continue;
}
if (char === "*") {
source += "[^/]*";
continue;
}
if (char === "?") {
source += "[^/]";
continue;
}
if (char === "{") {
const closeIndex = pattern.indexOf("}", index + 1);
if (closeIndex !== -1) {
const alternatives = pattern
.slice(index + 1, closeIndex)
.split(",")
.map(escapeRegExp)
.join("|");
source += `(?:${alternatives})`;
index = closeIndex;
continue;
}
}
source += escapeRegExp(char ?? "");
}
return new RegExp(`${source}$`);
}
function escapeRegExp(value) {
return value.replace(/[\\^$+?.()|[\]{}]/g, "\\$&");
return picomatch(normalizePath(pattern), { bash: true, dot: true });
}
function isExcluded(pathBase, negativeMatchers) {
for (const isMatch of negativeMatchers) {
Expand Down
12 changes: 11 additions & 1 deletion package-lock.json

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@
"lint:fix": "biome check --write .",
"check": "tsc --noEmit && biome check . && npm run build"
},
"dependencies": {
"picomatch": "^4.0.3"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Adding a runtime dependency violates the repo’s no-production-dependencies convention for marketplace installs.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At package.json, line 49:

<comment>Adding a runtime dependency violates the repo’s no-production-dependencies convention for marketplace installs.</comment>

<file context>
@@ -45,9 +45,13 @@
 		"check": "tsc --noEmit && biome check . && npm run build"
 	},
+	"dependencies": {
+		"picomatch": "^4.0.3"
+	},
 	"devDependencies": {
</file context>

},
"devDependencies": {
"@biomejs/biome": "2.4.15",
"@types/node": "^25.7.0",
"@types/picomatch": "^4.0.0",
"typescript": "^6.0.3",
"vitest": "^4.1.5"
},
Expand Down
56 changes: 2 additions & 54 deletions src/rules/matcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createHash } from "node:crypto";
import picomatch from "picomatch";
import type { MatchReason, RuleFrontmatter } from "./types.js";

export interface MatcherInput {
Expand Down Expand Up @@ -123,60 +124,7 @@ function compilePatternSet(patterns: ReadonlyArray<string>): CompiledPatternSet
}

function createGlobMatcher(pattern: string): (path: string) => boolean {
const expression = globToRegExp(normalizePath(pattern));
return (path: string) => expression.test(path);
}

function globToRegExp(pattern: string): RegExp {
let source = "^";
for (let index = 0; index < pattern.length; index += 1) {
const char = pattern[index];
const nextChar = pattern[index + 1];

if (char === "*" && nextChar === "*") {
const afterGlobStar = pattern[index + 2];
if (afterGlobStar === "/") {
source += "(?:.*/)?";
index += 2;
} else {
source += ".*";
index += 1;
}
continue;
}

if (char === "*") {
source += "[^/]*";
continue;
}

if (char === "?") {
source += "[^/]";
continue;
}

if (char === "{") {
const closeIndex = pattern.indexOf("}", index + 1);
if (closeIndex !== -1) {
const alternatives = pattern
.slice(index + 1, closeIndex)
.split(",")
.map(escapeRegExp)
.join("|");
source += `(?:${alternatives})`;
index = closeIndex;
continue;
}
}

source += escapeRegExp(char ?? "");
}

return new RegExp(`${source}$`);
}

function escapeRegExp(value: string): string {
return value.replace(/[\\^$+?.()|[\]{}]/g, "\\$&");
return picomatch(normalizePath(pattern), { bash: true, dot: true });
}

function isExcluded(pathBase: string, negativeMatchers: ReadonlyArray<(path: string) => boolean>): boolean {
Expand Down
22 changes: 22 additions & 0 deletions test/matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ describe("matchRule", () => {
expect(matched).toBe(true);
});

it("#given character class glob #when matching listed extension #then target matches", () => {
// given
const globs = "src/**/*.[tj]s";

// when
const matched = matchGlobs(globs, "src/features/app.ts");

// then
expect(matched).toBe(true);
});

it("#given extglob pattern #when matching allowed extension #then target matches", () => {
// given
const globs = "src/**/*.@(ts|tsx)";

// when
const matched = matchGlobs(globs, "src/features/app.tsx");

// then
expect(matched).toBe(true);
});

it("#given duplicate normalized patterns #when normalizing #then first unique pattern order is kept", () => {
// given
const frontmatter = {
Expand Down
2 changes: 1 addition & 1 deletion test/package-smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("plugin package metadata", () => {
// then
expect(packageJson.type).toBe("module");
expect(packageJson.packageManager).toBe("npm@11.12.1");
expect(packageJson.dependencies ?? {}).toEqual({});
expect(packageJson.dependencies ?? {}).toEqual({ picomatch: "^4.0.3" });
expect(packageJson.bin["codex-rules"]).toBe("./dist/cli.js");
expect(pluginJson.hooks).toBe("./hooks/hooks.json");
expect(cliSource.startsWith("#!/usr/bin/env node")).toBe(true);
Expand Down
Loading