"""Run the Quirrel syntax checker over script files. Each file is parsed and run through Quirrel's static analyzer (`-syntax-check -sa`), in its own process so a failure is attributed to exactly one file and one bad file cannot hide the others. Analyzer warnings (unused bindings, suspicious expressions) count as failures: the files we check are our own, and we keep them clean. Not covered: names that are not defined anywhere. Quirrel reports those only when it fully compiles a file, and full compilation resolves imports, which needs the game's native modules. """ from __future__ import annotations import subprocess from dataclasses import dataclass from pathlib import Path @dataclass(frozen=True) class ScriptFailure: path: Path message: str def check_scripts(checker: Path, files: list[Path]) -> list[ScriptFailure]: """Return one ScriptFailure per file that does not parse; an empty list means all passed.""" failures: list[ScriptFailure] = [] for path in files: completed = subprocess.run([str(checker), "-syntax-check", "-sa", str(path)], capture_output=True, text=True) output = (completed.stderr + completed.stdout).strip() if completed.returncode != 0 or _reports_problem(output): failures.append(ScriptFailure(path, output or f"exit code {completed.returncode}")) return failures def _reports_problem(output: str) -> bool: # sq.exe exits with 0 even when parsing fails; it then prints "Error [...]" on stderr. # Analyzer findings start with "WARNING:". return any(line.startswith(("Error [", "WARNING:")) for line in output.splitlines()) def find_scripts(roots: list[Path]) -> list[Path]: """Expand directories into the .nut files under them; files are taken as given.""" found: list[Path] = [] for root in roots: found.extend(sorted(root.rglob("*.nut")) if root.is_dir() else [root]) return found