Skip to content

Commit 04501be

Browse files
ci(antipattern): fix top-level dir matching + benchmarks/lsp/bench filename allowlists (#85)
1 parent 8a1f8c7 commit 04501be

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

0 commit comments

Comments
 (0)