Fix bin/publish: use correct .env path for rspade_system Fix bin/publish script: prevent grep exit code 1 from terminating script 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
114 lines
6.3 KiB
PHP
Executable File
114 lines
6.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\RSpade\CodeQuality\Rules\JavaScript;
|
|
|
|
use App\RSpade\CodeQuality\Rules\CodeQualityRule_Abstract;
|
|
use App\RSpade\CodeQuality\Support\FileSanitizer;
|
|
|
|
class JsFallbackLegacy_CodeQualityRule extends CodeQualityRule_Abstract
|
|
{
|
|
public function get_id(): string
|
|
{
|
|
return 'JS-FALLBACK-01';
|
|
}
|
|
|
|
public function get_name(): string
|
|
{
|
|
return 'JavaScript Fallback/Legacy Code Check';
|
|
}
|
|
|
|
public function get_description(): string
|
|
{
|
|
return 'Enforces fail-loud principle - no fallback implementations allowed';
|
|
}
|
|
|
|
public function get_file_patterns(): array
|
|
{
|
|
return ['*.js'];
|
|
}
|
|
|
|
public function get_default_severity(): string
|
|
{
|
|
return 'critical';
|
|
}
|
|
|
|
/**
|
|
* Check JavaScript file for fallback/legacy code in comments and function calls (from line 1415)
|
|
* Enforces fail-loud principle - no fallback implementations allowed
|
|
*/
|
|
public function check(string $file_path, string $contents, array $metadata = []): void
|
|
{
|
|
// Skip vendor and node_modules directories
|
|
if (str_contains($file_path, '/vendor/') || str_contains($file_path, '/node_modules/')) {
|
|
return;
|
|
}
|
|
|
|
// Skip CodeQuality directory
|
|
if (str_contains($file_path, '/CodeQuality/')) {
|
|
return;
|
|
}
|
|
|
|
// Use original content to check comments before sanitization
|
|
$original_content = file_get_contents($file_path);
|
|
$lines = explode("\n", $original_content);
|
|
|
|
// Also get sanitized content to check for function calls
|
|
$sanitized_data = FileSanitizer::sanitize_javascript($file_path);
|
|
$sanitized_lines = $sanitized_data['lines'];
|
|
|
|
foreach ($lines as $line_num => $line) {
|
|
$line_number = $line_num + 1;
|
|
|
|
// Check for fallback or legacy in comments (case insensitive, with word boundaries)
|
|
// But allow fallback* or legacy* as marked exceptions
|
|
if (preg_match('/\/\/.*\b(fallback|legacy)\b(?!\*)/i', $line) ||
|
|
preg_match('/\/\*.*\b(fallback|legacy)\b(?!\*)/i', $line) ||
|
|
preg_match('/\*.*\b(fallback|legacy)\b(?!\*)/i', $line)) {
|
|
|
|
$this->add_violation(
|
|
$file_path,
|
|
$line_number,
|
|
"CRITICAL: Fallback/legacy code detected in comment. This violates the fail-loud principle - there must be ONE way to do things.",
|
|
trim($line),
|
|
"REMOVE the fallback/legacy code immediately. If primary code fails, it MUST throw an exception, NOT execute alternative code. Fallbacks create non-deterministic behavior and debugging nightmares. Only ONE implementation should exist. If you're an AI/LLM agent: DO NOT decide to keep fallback code - you MUST ask the developer for explicit approval. To mark legitimate uses (rare), add '*' after the word. Remember: Predictable failures are better than unpredictable 'working' code.",
|
|
'critical'
|
|
);
|
|
}
|
|
|
|
// Check for dual implementation patterns in single-line comments
|
|
// These patterns suggest old/new dual implementations which violate the no-fallback rule
|
|
if (preg_match('/\/\/.*\bold\W+.*\bnew\b/i', $line) || // "old" ... "new"
|
|
preg_match('/\/\/.*\bprevious\W+.*\bnew\b/i', $line) || // "previous" ... "new"
|
|
preg_match('/\/\/.*\bbackwards?\s+compatability/i', $line)) { // "backward(s) compatibility" (common misspelling)
|
|
|
|
$this->add_violation(
|
|
$file_path,
|
|
$line_number,
|
|
"🚨 CRITICAL DUAL IMPLEMENTATION VIOLATION: Comment suggests old/new or backwards compatibility patterns\n\nThis violates the fundamental architecture principle: There is NO 'old' and 'new' way - there is ONLY THE WAY.\nDual implementations cause:\n1. CONFUSION about which code path executes\n2. DUPLICATE CODE that diverges over time\n3. DEBUGGING NIGHTMARES when behavior differs\n4. MAINTENANCE BURDEN keeping both paths in sync",
|
|
trim($line),
|
|
"IMMEDIATE ACTION REQUIRED:\n1. IDENTIFY what 'old' vs 'new' refers to\n2. REMOVE the old implementation entirely\n3. UPDATE all references to use the single implementation\n4. DO NOT maintain backwards compatibility\n\nFor AI/LLM agents: This is a CRITICAL violation. You MUST:\n- Search for BOTH implementations referenced in the comment\n- Report findings to user before making changes\n- Remove the OLD implementation completely\n- Update all code to use the single 'new' (current) way\n- NEVER maintain dual implementations for compatibility\n\nRemember: There is only ONE way to do things. What's currently called 'new' IS the way.",
|
|
'critical'
|
|
);
|
|
}
|
|
|
|
// Check for function calls starting or ending with 'fallback'
|
|
// This catches: fallback(), fallback_loader(), document_loader_fallback(), etc.
|
|
// But NOT: document_fallback_loader() (fallback in middle)
|
|
// Use sanitized line to check if there's actual code, but check pattern on original line
|
|
if (isset($sanitized_lines[$line_num]) && trim($sanitized_lines[$line_num]) !== '') {
|
|
// There's non-comment code on this line
|
|
// Pattern matches functions that start with "fallback" OR end with "fallback"
|
|
if (preg_match('/\b(fallback\w*|\w+fallback)\s*\(/i', $line)) {
|
|
$this->add_violation(
|
|
$file_path,
|
|
$line_number,
|
|
"CRITICAL: Fallback function call detected. This violates the fail-loud principle - there must be ONE way to do things.",
|
|
trim($line),
|
|
"REMOVE the fallback function immediately or RENAME it if it's legitimate required program flow (not an alternative implementation). If primary code fails, it MUST throw an exception, NOT execute alternative code. Fallbacks create non-deterministic behavior and debugging nightmares. Only ONE implementation should exist.",
|
|
'critical'
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |