Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

perf(sync): #6712 PR 4b fix 3, isCodeFilePath looks up the suffix instead of looping

garrytan5ee7f0630e9c
diff --git a/src/core/sync.ts b/src/core/sync.ts
index 87afbbb247..33af8da1d7 100644
--- a/src/core/sync.ts
+++ b/src/core/sync.ts
@@ -77,7 +77,8 @@ interface SyncableOptions {
// Kept as-is for now for `isAllowedByStrategy` fast-path + tests that
// structurally reference it. Derived from the chunker's language map at
// module load, not hardcoded.
-const CODE_EXTENSIONS = new Set<string>([
+/** Each entry is a dot and a dot-free suffix, so a path is code exactly when the suffix from its last dot is one (`isCodeFilePath`). */
+export const CODE_EXTENSIONS: ReadonlySet<string> = new Set<string>([
'.ts', '.tsx', '.mts', '.cts',
'.js', '.jsx', '.mjs', '.cjs',
'.py',
@@ -254,10 +255,8 @@ export function buildSyncManifest(gitDiffOutput: string): SyncManifest {
export function isCodeFilePath(path: string): boolean {
const lower = path.toLowerCase();
- for (const ext of CODE_EXTENSIONS) {
- if (lower.endsWith(ext)) return true;
- }
- return false;
+ const dot = lower.lastIndexOf('.');
+ return dot >= 0 && CODE_EXTENSIONS.has(lower.slice(dot));
}
/**
diff --git a/test/code-file-path-6712.test.ts b/test/code-file-path-6712.test.ts
new file mode 100644
index 0000000000..15aad113b4
--- /dev/null
+++ b/test/code-file-path-6712.test.ts
@@ -0,0 +1,26 @@
+/**
+ * #6712 PR 4b fix 3: `isCodeFilePath` looks up the suffix from the path's last dot instead of testing every extension
+ * (a 308k-path level asks it once per path). Each extension is a dot and a dot-free suffix, so a path ends with one of
+ * them exactly when that suffix is one.
+ *
+ * Protects: the lookup answers as the loop over `CODE_EXTENSIONS` with `endsWith` did, for every extension in any case,
+ * after one or several dots, after a dotted directory, inside a longer suffix, without a dot, and for the empty path.
+ */
+import { expect, test } from 'bun:test';
+import { CODE_EXTENSIONS, isCodeFilePath } from '../src/core/sync.ts';
+
+const byLoop = (path: string) => [...CODE_EXTENSIONS].some(ext => path.toLowerCase().endsWith(ext));
+
+test('every extension is a dot and a dot-free suffix', () => {
+ expect([...CODE_EXTENSIONS].filter(ext => !/^\.[^./]+$/.test(ext))).toEqual([]);
+});
+
+test('the suffix lookup answers as the loop over every extension', () => {
+ const paths = ['', '.', 'README', 'Makefile', 'a.', 'dir.ts/file', 'dir.ts/file.md', 'notes/a.md', 'img/x.png', 'x.d.ts', 'x.test.TSX', 'a.tfvars.bak', 'a.bak.tfvars',
+ '.ts', '..ts', 'src/.eslintrc.json', 'x.mdx', 'x.json5', 'x.c.md', 'x.md.c', 'x.sql~', 'x.\u212At'];
+ for (const ext of CODE_EXTENSIONS) for (const stem of ['a', 'dir/a', 'dir.v2/a.b', '']) for (const tail of ['', 'x', '.md', '/x'])
+ paths.push(stem + ext + tail, stem + ext.toUpperCase() + tail, stem + ext.slice(0, -1) + tail);
+ const differ = paths.filter(path => isCodeFilePath(path) !== byLoop(path));
+ expect(differ).toEqual([]);
+ expect(paths.filter(isCodeFilePath).length).toBeGreaterThan(CODE_EXTENSIONS.size * 4);
+});