|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Lightweight provider scoping linter powered by ripgrep. |
| 4 | +# |
| 5 | +# Flags occurrences of provider-scoped models that do not call |
| 6 | +# `.for_provider()` immediately after `.objects`, and any usage of |
| 7 | +# `get_object_or_404` / `get_list_or_404`. |
| 8 | + |
| 9 | +set -euo pipefail |
| 10 | + |
| 11 | +if ! command -v rg >/dev/null 2>&1; then |
| 12 | + echo "lint_provider_scoping_grep: ripgrep (rg) is required" >&2 |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +if ! rg --pcre2 --version >/dev/null 2>&1; then |
| 17 | + echo "lint_provider_scoping_grep: ripgrep must be built with PCRE2 support" >&2 |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +PROVIDER_MODELS_REGEX='(Clinic|ClinicSlot|Appointment|Participant)' |
| 22 | +DEFAULT_TARGETS=(manage_breast_screening) |
| 23 | + |
| 24 | +targets=("$@") |
| 25 | +if [ ${#targets[@]} -eq 0 ]; then |
| 26 | + targets=("${DEFAULT_TARGETS[@]}") |
| 27 | +fi |
| 28 | + |
| 29 | +status=0 |
| 30 | + |
| 31 | +echo "Checking provider scoping with ripgrep…" >&2 |
| 32 | + |
| 33 | +# Detect `.objects` chains missing `.for_provider()` as the first call |
| 34 | +if rg --multiline --multiline-dotall --pcre2 -n \ |
| 35 | + "${PROVIDER_MODELS_REGEX}\.objects(?!\s*\.for_provider\()" \ |
| 36 | + "${targets[@]}" >/tmp/provider_scope_objects.out; then |
| 37 | + echo "PS001 (grep): found manager usage without .for_provider()" >&2 |
| 38 | + cat /tmp/provider_scope_objects.out >&2 |
| 39 | + status=1 |
| 40 | +fi |
| 41 | + |
| 42 | +# Detect banned shortcuts |
| 43 | +if rg --multiline --pcre2 -n \ |
| 44 | + '\bget_(object|list)_or_404\s*\(' \ |
| 45 | + "${targets[@]}" >/tmp/provider_scope_shortcuts.out; then |
| 46 | + echo "PS002 (grep): found provider-agnostic shortcut usage" >&2 |
| 47 | + cat /tmp/provider_scope_shortcuts.out >&2 |
| 48 | + status=1 |
| 49 | +fi |
| 50 | + |
| 51 | +rm -f /tmp/provider_scope_objects.out /tmp/provider_scope_shortcuts.out |
| 52 | + |
| 53 | +exit $status |
0 commit comments