Files
hermes-scripts-backup/skills/software-development/alpinejs-debugging/scripts/audit-semicolons.py
T
2026-08-28 11:43:34 -04:00

64 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""Audit Alpine.js templates for semicolons in event handler expressions.
Alpine evaluates expressions via `return <expr>`. Semicolons inside
event handler expressions terminate the return prematurely:
Expression: "a = 1; b = 2"
→ return a = 1; b = 2
→ SyntaxError: Unexpected token ';'
When this happens in a PARENT component, ALL child x-data components
fail to initialize → cascading ReferenceError for every variable.
Usage:
python3 scripts/audit-semicolons.py app/templates/
Returns exit code 1 if any offending ; are found.
"""
import re
import sys
from pathlib import Path
def audit_file(filepath: Path) -> int:
with open(filepath) as f:
content = f.read()
count = 0
for m in re.finditer(r'@\w+(?:\.\w+)*="([^"]*)"', content):
val = m.group(1)
if ";" in val and not val.startswith("$nextTick"):
count += 1
line_no = content[: m.start()].count("\n") + 1
print(f" {filepath}:{line_no} {val[:100]}")
return count
def main():
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} <template_file_or_dir> [more files...]")
sys.exit(1)
total = 0
for arg in sys.argv[1:]:
p = Path(arg)
files = [p] if p.is_file() else sorted(p.rglob("*.html"))
for f in files:
n = audit_file(f)
if n:
print(f" → {n} issue(s) in {f.name}")
total += n
if total:
print(f"\nTOTAL: {total} semicolon issue(s) across all templates")
print("Fix: replace ';' with ',' (comma operator) in Alpine @event expressions")
sys.exit(1)
else:
print("CLEAN — no semicolons in Alpine expressions")
sys.exit(0)
if __name__ == "__main__":
main()