#!/usr/bin/env bash # Checks the documented premises (open questions such as Q-001) against the code. # For each table row in the doc whose first cell holds an id, it reports: # refs - code files that mention the id # where - backticked paths/identifiers in the row that were found / not found in the code # It also lists ids mentioned in the code but missing from the doc. # Usage: premises.sh [doc=README.md] [code-root=src|.] [id-pattern='Q-[0-9]+'] [path-filter] # path-filter: only rows whose locations or referencing files contain this text (e.g. a module name) set -uo pipefail DOC="${1:-README.md}" ROOT="${2:-}" if [ -z "$ROOT" ]; then if [ -d src ]; then ROOT=src; else ROOT=.; fi fi PATTERN="${3:-Q-[0-9]+}" FILTER="${4:-}" EXCLUDES=(--exclude-dir=node_modules --exclude-dir=dist --exclude-dir=build --exclude-dir=coverage) if [ ! -f "$DOC" ]; then echo "doc not found: $DOC" >&2 exit 2 fi # Resolves a backticked token to "ok" or "missing": paths by suffix, identifiers by word grep. resolve() { local token="$1" if [[ "$token" == */* || "$token" =~ \.(ts|tsx|sql|md)$ ]]; then local clean="${token%%:*}" [ -e "$clean" ] || [ -e "$ROOT/$clean" ] && { echo ok; return; } find "$ROOT" -path "*${clean}*" ! -path '*/node_modules/*' -print -quit 2>/dev/null | grep -q . && { echo ok; return; } echo missing elif [[ "$token" =~ ^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*$ ]]; then local last="${token##*.}" grep -rqw "${EXCLUDES[@]}" --include='*.ts' --include='*.tsx' -- "$last" "$ROOT" && { echo ok; return; } echo missing else echo skip fi } documented_ids=() total=0 untracked=0 broken=0 printf '%-10s %5s %s\n' "ID" "REFS" "LOCATIONS (ok / missing)" while IFS= read -r row; do first_cell=$(printf '%s' "$row" | awk -F'|' '{print $2}') ids=$(printf '%s' "$first_cell" | grep -oE "$PATTERN" | sort -u) [ -z "$ids" ] && continue tokens=$(printf '%s' "$row" | grep -oE '`[^`]+`' | tr -d '`' | sort -u) for id in $ids; do files=$(grep -rlE "${EXCLUDES[@]}" --include='*.ts' --include='*.tsx' "\\b${id}\\b" "$ROOT" 2>/dev/null) if [ -n "$FILTER" ] && ! printf '%s\n%s' "$tokens" "$files" | grep -q -- "$FILTER"; then continue fi documented_ids+=("$id") total=$((total + 1)) refs=$(printf '%s' "$files" | grep -c . || true) ok=() missing=() while IFS= read -r token; do [ -z "$token" ] && continue case "$(resolve "$token")" in ok) ok+=("$token") ;; missing) missing+=("$token") ;; esac done <<< "$tokens" flag="" [ "$refs" = 0 ] && { flag=" [no code reference]"; untracked=$((untracked + 1)); } [ "${#missing[@]}" -gt 0 ] && { flag="$flag [missing location]"; broken=$((broken + 1)); } printf '%-10s %5s ok=%s missing=%s%s\n' "$id" "$refs" "${#ok[@]}" "${missing[*]:-none}" "$flag" done done < <(grep -E '^\|' "$DOC") echo echo "== summary" echo "premises documented: $total" echo "without any code reference: $untracked" echo "with a location that no longer exists: $broken" echo echo "== ids in the code but not in $DOC" code_ids=$(grep -rohE "${EXCLUDES[@]}" --include='*.ts' --include='*.tsx' "\\b(${PATTERN})\\b" "$ROOT" 2>/dev/null | sort -u) for id in $code_ids; do printf '%s\n' "${documented_ids[@]:-}" | grep -qx "$id" || grep -qE "\\b${id}\\b" "$DOC" || echo " $id ($(grep -rlE "${EXCLUDES[@]}" --include='*.ts' --include='*.tsx' "\\b${id}\\b" "$ROOT" | head -1))" done