Skip to content

Commit 1223c9a

Browse files
noelsaw1claude
andcommitted
Fix missing code snippets in N+1 pattern findings
The N+1 check emitted findings with an empty code field because find_meta_in_loop_line only returned the line number. Now extracts the actual source line via sed before passing to add_json_finding. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ffa67c7 commit 1223c9a

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

dist/bin/check-performance.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5531,36 +5531,39 @@ text_echo "${BLUE}▸ Potential N+1 patterns (meta in loops) ${N1_COLOR}[$N1_SEV
55315531
if [ -z "$N1_LINE" ]; then
55325532
continue
55335533
fi
5534+
# Extract the actual source line for the finding code snippet
5535+
N1_CODE=$(sed -n "${N1_LINE}p" "$f" 2>/dev/null | sed 's/^[[:space:]]*//')
5536+
55345537
# Smart detection: Prioritized checks for false positives and severity adjustment
55355538

55365539
# Priority 1: Check if this is a WordPress admin view where cache is pre-primed
55375540
if is_wp_cache_primed_view "$f"; then
55385541
# WordPress primes meta cache on admin pages like user-edit.php
55395542
# These are likely false positives - downgrade to INFO
55405543
VISIBLE_N1_OPTIMIZED="${VISIBLE_N1_OPTIMIZED}${f}"$'\n'
5541-
add_json_finding "n-plus-1-pattern" "info" "LOW" "$f" "$N1_LINE" "Potential N+1 in WP admin view - likely false positive (WordPress pre-primes meta cache on user-edit.php)" ""
5544+
add_json_finding "n-plus-1-pattern" "info" "LOW" "$f" "$N1_LINE" "Potential N+1 in WP admin view - likely false positive (WordPress pre-primes meta cache on user-edit.php)" "$N1_CODE"
55425545
((N1_OPTIMIZED_COUNT++)) || true
55435546
# Priority 2: Check if loop iterates over fields for a single object (not multiple objects)
55445547
elif is_single_object_field_loop "$f"; then
55455548
# Iterating over fields for ONE object - WordPress caches all meta on first call
55465549
VISIBLE_N1_OPTIMIZED="${VISIBLE_N1_OPTIMIZED}${f}"$'\n'
5547-
add_json_finding "n-plus-1-pattern" "info" "LOW" "$f" "$N1_LINE" "Potential N+1 but loop iterates over fields for single object - WordPress caches all meta on first call" ""
5550+
add_json_finding "n-plus-1-pattern" "info" "LOW" "$f" "$N1_LINE" "Potential N+1 but loop iterates over fields for single object - WordPress caches all meta on first call" "$N1_CODE"
55485551
((N1_OPTIMIZED_COUNT++)) || true
55495552
# Priority 3: Check if file uses explicit meta caching
55505553
elif has_meta_cache_optimization "$f"; then
55515554
# File uses update_meta_cache() - likely optimized, downgrade to INFO
55525555
VISIBLE_N1_OPTIMIZED="${VISIBLE_N1_OPTIMIZED}${f}"$'\n'
5553-
add_json_finding "n-plus-1-pattern" "info" "LOW" "$f" "$N1_LINE" "Potential N+1 (meta in loop), but update_meta_cache() is present - verify optimization" ""
5556+
add_json_finding "n-plus-1-pattern" "info" "LOW" "$f" "$N1_LINE" "Potential N+1 (meta in loop), but update_meta_cache() is present - verify optimization" "$N1_CODE"
55545557
((N1_OPTIMIZED_COUNT++)) || true
55555558
# Priority 4: Check for pagination guards
55565559
elif has_pagination_guard "$f"; then
55575560
VISIBLE_N1_PAGINATED="${VISIBLE_N1_PAGINATED}${f}"$'\n'
5558-
add_json_finding "n-plus-1-pattern" "warning" "LOW" "$f" "$N1_LINE" "Potential N+1 (meta in loop). File appears paginated (per_page/LIMIT) - review impact" ""
5561+
add_json_finding "n-plus-1-pattern" "warning" "LOW" "$f" "$N1_LINE" "Potential N+1 (meta in loop). File appears paginated (per_page/LIMIT) - review impact" "$N1_CODE"
55595562
((N1_PAGINATED_COUNT++)) || true
55605563
else
55615564
# No mitigations detected - standard warning (likely true N+1)
55625565
VISIBLE_N1_FILES="${VISIBLE_N1_FILES}${f}"$'\n'
5563-
add_json_finding "n-plus-1-pattern" "warning" "$N1_SEVERITY" "$f" "$N1_LINE" "Potential N+1 query pattern: meta call inside loop (heuristic). Review pagination/caching" ""
5566+
add_json_finding "n-plus-1-pattern" "warning" "$N1_SEVERITY" "$f" "$N1_LINE" "Potential N+1 query pattern: meta call inside loop (heuristic). Review pagination/caching" "$N1_CODE"
55645567
((N1_FINDING_COUNT++)) || true
55655568
fi
55665569
fi

0 commit comments

Comments
 (0)