Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions hooks/check-file-size.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ if [ -z "$FILE_PATH" ] || [ ! -f "$FILE_PATH" ]; then
exit 0
fi

# Skip non-code files
# Skip non-code files. .mdx and .markdown belong here with .md: they are prose,
# and a long article is a long article, not a module that wants extracting into
# types.ts. The split advice below cannot apply to any of them.
case "$FILE_PATH" in
*.md|*.json|*.yaml|*.yml|*.toml|*.lock|*.svg|*.png|*.jpg|*.csv|*.txt)
*.md|*.mdx|*.markdown|*.json|*.yaml|*.yml|*.toml|*.lock|*.svg|*.png|*.jpg|*.csv|*.txt)
exit 0
;;
esac
Expand All @@ -40,6 +42,34 @@ case "$FILE_PATH" in

Keep each layer under the warn threshold. Do NOT split in the middle of a component."
;;
*.daml)
WARN_THRESHOLD=300
BLOCK_THRESHOLD=450
SPLIT_ADVICE="Split by contract concern - extract into separate modules:
- Types.daml - shared data types, enums, and type aliases
- One module per template family - keep a template with its own choices, never apart
- Test suites: one module per lifecycle path (happy, edge case, multi-party)

Keep a template and its choices together. Do NOT split a template from its choices to
get under a line count."
;;
*.astro)
# An .astro file is a component, its scoped stylesheet, and its client script in one
# file - that colocation is the framework's unit, not a smell. A real example: a 301
# line Header.astro was 21 frontmatter, 31 markup, 149 scoped <style>, 100 client
# <script>. Scored against the module default it is the CSS that trips the limit, and
# the module advice below has nothing to act on. Flattening the <style> into a global
# sheet to win back lines would lose the scoping and make the code worse.
WARN_THRESHOLD=250
BLOCK_THRESHOLD=400
SPLIT_ADVICE="Split by concern, keeping the Astro unit intact:
- extract a child .astro component (with its own scoped <style>) for a distinct region
- move a long client <script> into src/scripts/*.ts and import it
- lift genuinely shared rules into src/styles/*.css; keep component-scoped CSS in place
- move data arrays and helpers into src/lib/*.ts

Do NOT flatten scoped <style> into a global stylesheet to win back lines."
;;
*)
WARN_THRESHOLD=200
BLOCK_THRESHOLD=300
Expand All @@ -55,6 +85,30 @@ Do NOT just move code around - ensure clean imports and no circular dependencies
;;
esac

# A project may set its own thresholds, because the right number depends on the
# codebase and not on this file. Read last so it wins over the per-extension
# defaults above.
#
# The root comes from the edited file rather than from $PWD, the same way
# lib/log-event.sh finds where to put the ledger, so the config sits next to the
# ledger by construction and is found whatever directory the session runs from.
#
# Only the two numbers are read, by pattern, not by sourcing the file: a config
# in the repo would otherwise be arbitrary shell running on every edit. And
# SPLIT_ADVICE stays the hook's, so a project can move the line but cannot turn
# the advice into something unhelpful.
# No repo means no project, so no project config: falling back to $PWD here
# would hand a file edited outside any repo the thresholds of whatever project
# the shell happened to be sitting in.
CONF_DIR=$(cd "$(dirname "$FILE_PATH")" 2>/dev/null && git rev-parse --show-toplevel 2>/dev/null)
CONF="${CONF_DIR}/.harness/file-size.conf"
if [ -n "$CONF_DIR" ] && [ -f "$CONF" ]; then
PROJECT_WARN=$(sed -n 's/^WARN_THRESHOLD=\([0-9][0-9]*\).*/\1/p' "$CONF" | tail -1)
PROJECT_BLOCK=$(sed -n 's/^BLOCK_THRESHOLD=\([0-9][0-9]*\).*/\1/p' "$CONF" | tail -1)
[ -n "$PROJECT_WARN" ] && WARN_THRESHOLD="$PROJECT_WARN"
[ -n "$PROJECT_BLOCK" ] && BLOCK_THRESHOLD="$PROJECT_BLOCK"
fi

if [ "$LINE_COUNT" -gt "$BLOCK_THRESHOLD" ]; then
cat >&2 <<EOF
⛔ FILE TOO LARGE: $FILE_PATH has $LINE_COUNT lines (limit: $BLOCK_THRESHOLD)
Expand Down
7 changes: 7 additions & 0 deletions hooks/check-silent-errors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ hook_input_init "${1:-}"
FILE_PATH=$(hook_input_file)
[ -z "$FILE_PATH" ] || [ ! -f "$FILE_PATH" ] && exit 0

# Throwaway diagnostic scripts are not production code. A one-off probe in the
# session scratchpad gets deleted with the session, so holding it to the
# re-raise-or-log-with-context contract is noise, not safety.
case "$FILE_PATH" in
*/scratchpad/*|/tmp/*|/private/tmp/*) exit 0 ;;
esac

VIOLATIONS=""

case "$FILE_PATH" in
Expand Down
10 changes: 9 additions & 1 deletion hooks/lint-on-edit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,15 @@ fi

if [ "$FAIL" -eq 1 ]; then
printf '%s' "$OUT" >&2
[ -x "$(dirname "$0")/lib/log-event.sh" ] && "$(dirname "$0")/lib/log-event.sh" lint block "$FILE_PATH" "lint or typecheck failed"
# Log WHICH rules failed, not just that something did. A constant detail string
# makes the ledger uncountable: you cannot tell one rule breaking 200 times from
# 200 rules breaking once. ESLint stylish puts the rule id last on the line
# ("@typescript-eslint/no-unused-vars"); Biome prints "lint/suspicious/noExplicitAny".
# Both end in a slash-separated id, so take the last token of matching lines.
SIG=$( { printf '%s' "$OUT" | grep -oE 'lint/[a-zA-Z]+/[a-zA-Z0-9]+'
printf '%s' "$OUT" | awk '/^[[:space:]]*[0-9]+:[0-9]+[[:space:]]+(error|warning)/ {print $NF}'
} 2>/dev/null | sort -u | head -3 | paste -sd, -)
[ -x "$(dirname "$0")/lib/log-event.sh" ] && "$(dirname "$0")/lib/log-event.sh" lint block "$FILE_PATH" "lint: ${SIG:-unclassified}"
exit 2
fi

Expand Down
Loading