Skip to content

Commit 36bdb07

Browse files
ci(antipattern): fix top-level dir + benchmark/lsp filename matching (#18)
Fix-up for v3 (which used fnmatch globs that don't match top-level directories like `tests/foo.ts`). Replaces fnmatch-based glob matching with explicit string predicates: - Allowed directory names match at any path depth (incl. top-level) - Filename suffixes for LSP servers (`lsp-server.ts` etc.) and benchmarks (`*_bench.ts`, `*.bench.ts`) - Per-repo .claude/CLAUDE.md exemption table parsed; `*` and `**` both treated as 'any chars including /' (loose user-friendly interpretation). Verified against the affinescript exemption table + ubicity tests/+benchmarks/ + various edge cases.
1 parent 86d5b97 commit 36bdb07

1 file changed

Lines changed: 109 additions & 1 deletion

File tree

.github/workflows/rsr-antipattern.yml

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,115 @@ jobs:
2525
- name: Check for TypeScript
2626
run: |
2727
python3 << 'PYEOF'
28-
import re, sys, fnmatch, pathlib
28+
import re, sys, pathlib
29+
30+
# Universal allowlist — bridges and conventions that need no per-repo declaration.
31+
# Implemented as explicit string predicates rather than glob patterns so that
32+
# top-level directories (e.g. tests/foo.ts) are matched the same as nested ones,
33+
# which fnmatch's * cannot do reliably.
34+
DIR_NAMES_ALLOWED = {
35+
'bindings', 'tests', 'test', 'scripts',
36+
'mcp-adapter', 'cli', 'vendor', 'examples', 'ffi',
37+
'node_modules', 'benchmarks',
38+
}
39+
40+
def builtin_allowed(p):
41+
# `p` is a posix-style path with no leading ./
42+
# 1. Type declaration files
43+
if p.endswith('.d.ts'):
44+
return True
45+
# 2. Canonical Deno entrypoint filenames
46+
base = p.rsplit('/', 1)[-1]
47+
if base == 'mod.ts':
48+
return True
49+
# 3. LSP server files (filename suffixes)
50+
if base in ('lsp-server.ts', 'lsp_server.ts', 'lsp.ts') or base.endswith('-lsp.ts'):
51+
return True
52+
# 4. Benchmark files (filename suffixes)
53+
if base.endswith('.bench.ts') or base.endswith('_bench.ts'):
54+
return True
55+
# 5. Any directory segment (excluding basename) matches an allowed dir
56+
segs = p.split('/')
57+
for s in segs[:-1]:
58+
if s in DIR_NAMES_ALLOWED:
59+
return True
60+
# vscode-anything or anything-vscode
61+
if 'vscode' in s:
62+
return True
63+
# deno-named subprojects
64+
if s.startswith('deno-'):
65+
return True
66+
return False
67+
68+
# Per-repo exemptions parsed from .claude/CLAUDE.md "TypeScript Exemptions" table.
69+
# This is the documented single source of truth: adding one row here unblocks CI.
70+
# Glob characters: '*' and '**' both mean "any chars including /". This loose
71+
# interpretation matches user intent when an exemption row reads, e.g.,
72+
# `affinescript-deno-test/*.ts` (covering nested files too).
73+
def glob_to_regex(g):
74+
out = []
75+
for c in g.lstrip('./'):
76+
if c == '*': out.append('.*')
77+
elif c == '?': out.append('.')
78+
elif c in '.+(){}[]|^$\\': out.append(re.escape(c))
79+
else: out.append(c)
80+
return re.compile('^' + ''.join(out) + '$')
81+
82+
exemption_patterns = []
83+
claude_md = pathlib.Path('.claude/CLAUDE.md')
84+
if claude_md.exists():
85+
in_table = False
86+
for line in claude_md.read_text(encoding='utf-8').splitlines():
87+
if re.search(r'TypeScript [Ee]xemptions', line):
88+
in_table = True
89+
continue
90+
if in_table and line.startswith(('### ', '## ', '# ')):
91+
break
92+
if in_table and line.startswith('|'):
93+
m = re.match(r'\|\s*`([^`]+)`', line)
94+
if m:
95+
exemption_patterns.append((m.group(1), glob_to_regex(m.group(1))))
96+
97+
def exempt(p):
98+
for raw, regex in exemption_patterns:
99+
if regex.match(p):
100+
return True
101+
# Also allow exact-path matches and prefix matches for paths
102+
# ending in `/`
103+
if p == raw.lstrip('./'):
104+
return True
105+
if raw.endswith('/') and p.startswith(raw.lstrip('./')):
106+
return True
107+
return False
108+
109+
# Find all .ts and .tsx files (excluding common dot-dirs that find normally skips)
110+
found = []
111+
for ext in ('ts', 'tsx'):
112+
for p in pathlib.Path('.').rglob(f'*.{ext}'):
113+
parts = p.parts
114+
if any(part.startswith('.') and part not in ('.', '..') for part in parts):
115+
continue
116+
found.append(p.as_posix().lstrip('./'))
117+
118+
bad = sorted(f for f in found if not (builtin_allowed(f) or exempt(f)))
119+
if bad:
120+
print("❌ TypeScript files detected outside the allowlist.\n")
121+
for f in bad:
122+
print(f" {f}")
123+
print()
124+
print("To resolve, choose one:")
125+
print(" (a) migrate the file to AffineScript")
126+
print(" (see Human_Programming_Guide.adoc 'Migrating from -script Languages')")
127+
print(" (b) move to an allowlisted bridge path")
128+
print(" (bindings/, tests/, test/, scripts/, benchmarks/, mcp-adapter/,")
129+
print(" *vscode*/, cli/, deno-*/, vendor/, examples/, ffi/)")
130+
print(" (c) add an entry to the 'TypeScript Exemptions' table in .claude/CLAUDE.md")
131+
print(" with rationale + unblock condition")
132+
if exemption_patterns:
133+
print(f"\n(Currently {len(exemption_patterns)} exemption(s) parsed from .claude/CLAUDE.md.)")
134+
sys.exit(1)
135+
print(f"✅ No TypeScript files outside allowlist ({len(exemption_patterns)} per-repo exemption(s) parsed).")
136+
PYEOF
29137
30138
# Universal builtin allowlist — bridges that need no per-repo declaration.
31139
# Files matching any of these patterns are always allowed.

0 commit comments

Comments
 (0)