#!/usr/bin/env bash # Ratchet lint: comments are banned. Each file may hold at most the number of # comment lines recorded in .comment-baseline; the number can only go down. # scripts/lint-comments.sh check (CI) # scripts/lint-comments.sh --update rewrite the baseline from the tree set -euo pipefail cd "$(git rev-parse --show-toplevel)" baseline=.comment-baseline rust='^[[:space:]]*//|[;{},)][[:space:]]+//[[:space:]]' rust_ok='^[[:space:]]*// (SAFETY:|rustfmt::|clippy::)' hash='^[[:space:]]*#' hash_ok='^[[:space:]]*#(!|[[:space:]]*(shellcheck|yaml-language-server|nolint))' count() { case "$1" in *.rs) grep -E "$rust" "$1" | grep -vEc "$rust_ok" || true ;; *) grep -E "$hash" "$1" | grep -vEc "$hash_ok" || true ;; esac } current=$( git ls-files '*.rs' '*.sh' '*.py' '*.nix' '*.yml' '*.yaml' | while read -r f; do [ -f "$f" ] || continue n=$(count "$f") if [ "$n" -gt 0 ]; then echo "$n $f"; fi done | sort -k2 ) if [ "${1:-}" = "--update" ]; then printf '%s\n' "$current" | sed '/^$/d' > "$baseline" echo "baseline written: $(wc -l < "$baseline") files, $(awk '{s+=$1} END {print s+0}' "$baseline") comment lines" exit 0 fi touch "$baseline" fail=0 while read -r n f; do [ -n "${f:-}" ] || continue allowed=$(awk -v f="$f" '$2 == f {print $1}' "$baseline") allowed=${allowed:-0} if [ "$n" -gt "$allowed" ]; then echo "FAIL $f: $n comment lines, allowed $allowed. Delete the comment; if it records a decision, write an ADR in docs/adr/." fail=1 elif [ "$n" -lt "$allowed" ]; then echo "FAIL $f: $n comment lines, baseline says $allowed. Run scripts/lint-comments.sh --update to lock in the cleanup." fail=1 fi done <<< "$current" while read -r n f; do [ -n "${f:-}" ] || continue if ! printf '%s\n' "$current" | awk -v f="$f" '$2 == f {found=1} END {exit !found}'; then echo "FAIL $f: baseline lists $n comment lines but the file has none. Run scripts/lint-comments.sh --update." fail=1 fi done < "$baseline" [ "$fail" -eq 0 ] && echo "comment lint ok" exit "$fail"