Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | // src/inspections/DiagnosticProvider.ts // ───────────────────────────────────────────────────────────────────────────── // Provides real-time red-underline diagnostics for common Laravel mistakes: // 1. Unknown route name – route('does-not-exist') // 2. Unknown view name – view('does-not-exist') // 3. Unknown Blade component – <x-does-not-exist> // 4. env() outside config files – using env() in app code // 5. Guarded attribute write – assigning to $guarded fields // 6. Unknown translation key – __('missing.key') // 7. Unknown config key – config('missing.key') // ───────────────────────────────────────────────────────────────────────────── import * as vscode from 'vscode'; import * as path from 'path'; import type { CacheManager } from '../cache/CacheManager'; export class LaravelDiagnosticProvider implements vscode.Disposable { private readonly collection: vscode.DiagnosticCollection; private readonly disposables: vscode.Disposable[] = []; private readonly debounceTimers = new Map<string, ReturnType<typeof setTimeout>>(); constructor(private readonly cache: CacheManager) { this.collection = vscode.languages.createDiagnosticCollection('laravel-maestro'); // Diagnose on open and on change this.disposables.push( vscode.workspace.onDidOpenTextDocument((doc) => this.scheduleDiagnose(doc)), vscode.workspace.onDidChangeTextDocument((e) => this.scheduleDiagnose(e.document)), vscode.workspace.onDidCloseTextDocument((doc) => { this.collection.delete(doc.uri); const timer = this.debounceTimers.get(doc.uri.fsPath); if (timer) { clearTimeout(timer); this.debounceTimers.delete(doc.uri.fsPath); } }) ); // Diagnose already-open editors on startup for (const doc of vscode.workspace.textDocuments) { this.scheduleDiagnose(doc); } } // ─── Debounced Diagnose ─────────────────────────────────────────────────── private scheduleDiagnose(doc: vscode.TextDocument, delayMs = 600): void { if (!this.isRelevantDocument(doc)) { return; } const key = doc.uri.fsPath; const existing = this.debounceTimers.get(key); if (existing) { clearTimeout(existing); } const timer = setTimeout(() => { this.debounceTimers.delete(key); this.diagnose(doc); }, delayMs); this.debounceTimers.set(key, timer); } private isRelevantDocument(doc: vscode.TextDocument): boolean { return doc.languageId === 'php' || doc.languageId === 'blade'; } // ─── Main Diagnostics Runner ────────────────────────────────────────────── private diagnose(doc: vscode.TextDocument): void { const text = doc.getText(); const diagnostics: vscode.Diagnostic[] = []; const index = this.cache.getIndex(); // 1. Unknown route names this.checkRouteNames(doc, text, diagnostics); // 2. Unknown view names this.checkViewNames(doc, text, diagnostics); // 3. Unknown Blade components if (doc.languageId === 'blade') { this.checkBladeComponents(doc, text, diagnostics); } // 4. env() outside config files if (!this.isConfigFile(doc.uri.fsPath)) { this.checkEnvOutsideConfig(doc, text, diagnostics); } // 5. Unknown translation keys (only if we have translation data) if (index.translations.size > 0) { this.checkTranslationKeys(doc, text, diagnostics); } // 6. Unknown config keys (only if we have config data) if (index.configs.size > 0) { this.checkConfigKeys(doc, text, diagnostics); } this.collection.set(doc.uri, diagnostics); } // ─── Rule 1: Unknown Route Names ───────────────────────────────────────── private checkRouteNames( doc: vscode.TextDocument, text: string, diags: vscode.Diagnostic[] ): void { const index = this.cache.getIndex(); if (index.routes.size === 0) { return; } // index not ready const pattern = /\b(?:route|to_route)\s*\(\s*['"]([^'"]+)['"]/g; let m: RegExpExecArray | null; while ((m = pattern.exec(text)) !== null) { const name = m[1]; if (index.routes.has(name)) { continue; } const start = doc.positionAt(m.index + m[0].indexOf(name)); const end = doc.positionAt(m.index + m[0].indexOf(name) + name.length); const diag = new vscode.Diagnostic( new vscode.Range(start, end), `Unknown route: '${name}'`, vscode.DiagnosticSeverity.Warning ); diag.code = 'laravel.unknown-route'; diag.source = 'Laravel Maestro'; diags.push(diag); } } // ─── Rule 2: Unknown View Names ─────────────────────────────────────────── private checkViewNames( doc: vscode.TextDocument, text: string, diags: vscode.Diagnostic[] ): void { const index = this.cache.getIndex(); if (index.views.size === 0) { return; } const pattern = /\b(?:view|View::make|View::first)\s*\(\s*['"]([^'"]+)['"]/g; let m: RegExpExecArray | null; while ((m = pattern.exec(text)) !== null) { const name = m[1]; if (index.views.has(name)) { continue; } // Also allow vendor:: namespaced views if (name.includes('::')) { continue; } const start = doc.positionAt(m.index + m[0].indexOf(name)); const end = doc.positionAt(m.index + m[0].indexOf(name) + name.length); const diag = new vscode.Diagnostic( new vscode.Range(start, end), `Unknown view: '${name}'`, vscode.DiagnosticSeverity.Warning ); diag.code = 'laravel.unknown-view'; diag.source = 'Laravel Maestro'; diags.push(diag); } // Also check @include, @extends in Blade const bladePattern = /@(?:include|extends|component)\s*\(\s*['"]([^'"]+)['"]/g; while ((m = bladePattern.exec(text)) !== null) { const name = m[1]; if (index.views.has(name) || name.includes('::')) { continue; } const start = doc.positionAt(m.index + m[0].indexOf(name)); const end = doc.positionAt(m.index + m[0].indexOf(name) + name.length); const diag = new vscode.Diagnostic( new vscode.Range(start, end), `Unknown view: '${name}'`, vscode.DiagnosticSeverity.Warning ); diag.code = 'laravel.unknown-view'; diag.source = 'Laravel Maestro'; diags.push(diag); } } // ─── Rule 3: Unknown Blade Components ──────────────────────────────────── private checkBladeComponents( doc: vscode.TextDocument, text: string, diags: vscode.Diagnostic[] ): void { const index = this.cache.getIndex(); if (index.bladeComponents.size === 0) { return; } const pattern = /<(x-[\w.-]+)/g; let m: RegExpExecArray | null; while ((m = pattern.exec(text)) !== null) { const tag = m[1]; const name = tag.replace(/^x-/, '').replace(/-/g, '.'); if (index.bladeComponents.has(name)) { continue; } const startOffset = m.index + 1; // skip < const start = doc.positionAt(startOffset); const end = doc.positionAt(startOffset + tag.length); const diag = new vscode.Diagnostic( new vscode.Range(start, end), `Unknown Blade component: <${tag}>`, vscode.DiagnosticSeverity.Warning ); diag.code = 'laravel.unknown-component'; diag.source = 'Laravel Maestro'; diags.push(diag); } } // ─── Rule 4: env() Outside Config Files ────────────────────────────────── private checkEnvOutsideConfig( doc: vscode.TextDocument, text: string, diags: vscode.Diagnostic[] ): void { // Only flag in non-config PHP files — config/**.php files are expected to use env() const pattern = /\benv\s*\(\s*['"][^'"]+['"]/g; let m: RegExpExecArray | null; while ((m = pattern.exec(text)) !== null) { // Skip if inside a comment const lineStart = text.lastIndexOf('\n', m.index) + 1; const lineText = text.slice(lineStart, m.index + m[0].length); if (lineText.trimStart().startsWith('//') || lineText.trimStart().startsWith('*')) { continue; } const start = doc.positionAt(m.index); const end = doc.positionAt(m.index + m[0].length); const diag = new vscode.Diagnostic( new vscode.Range(start, end), 'env() should only be called inside config files. Use config() instead.', vscode.DiagnosticSeverity.Warning ); diag.code = 'laravel.env-outside-config'; diag.source = 'Laravel Maestro'; diag.tags = [vscode.DiagnosticTag.Unnecessary]; diags.push(diag); } } // ─── Rule 5: Unknown Translation Keys ──────────────────────────────────── private checkTranslationKeys( doc: vscode.TextDocument, text: string, diags: vscode.Diagnostic[] ): void { const index = this.cache.getIndex(); const pattern = /\b(?:__|trans|@lang)\s*\(\s*['"]([^'"]+)['"]/g; let m: RegExpExecArray | null; while ((m = pattern.exec(text)) !== null) { const key = m[1]; // Check both locale-prefixed and plain keys const exists = Array.from(index.translations.values()).some((t) => t.key === key); if (exists || key.includes(':')) { continue; } // skip package:: keys const start = doc.positionAt(m.index + m[0].indexOf(key)); const end = doc.positionAt(m.index + m[0].indexOf(key) + key.length); const diag = new vscode.Diagnostic( new vscode.Range(start, end), `Unknown translation key: '${key}'`, vscode.DiagnosticSeverity.Hint ); diag.code = 'laravel.unknown-translation'; diag.source = 'Laravel Maestro'; diags.push(diag); } } // ─── Rule 6: Unknown Config Keys ───────────────────────────────────────── private checkConfigKeys( doc: vscode.TextDocument, text: string, diags: vscode.Diagnostic[] ): void { const index = this.cache.getIndex(); // Only flag deeply nested keys that clearly don't exist const pattern = /\bconfig\s*\(\s*['"]([a-z][a-z0-9_]+\.[a-z][a-z0-9_.]+)['"]/g; let m: RegExpExecArray | null; while ((m = pattern.exec(text)) !== null) { const key = m[1]; if (index.configs.has(key)) { continue; } // Don't flag if the top-level group exists (partial key access is valid) const topLevel = key.split('.')[0]; const groupExists = Array.from(index.configs.keys()).some((k) => k.startsWith(topLevel + '.')); if (!groupExists) { continue; } const start = doc.positionAt(m.index + m[0].indexOf(key)); const end = doc.positionAt(m.index + m[0].indexOf(key) + key.length); const diag = new vscode.Diagnostic( new vscode.Range(start, end), `Unknown config key: '${key}'`, vscode.DiagnosticSeverity.Hint ); diag.code = 'laravel.unknown-config'; diag.source = 'Laravel Maestro'; diags.push(diag); } } // ─── Helpers ────────────────────────────────────────────────────────────── private isConfigFile(filePath: string): boolean { return filePath.includes(`${path.sep}config${path.sep}`) || filePath.includes('/config/'); } /** Refresh diagnostics for all open editors (call after re-index). */ refreshAll(): void { for (const doc of vscode.workspace.textDocuments) { this.scheduleDiagnose(doc, 0); } } dispose(): void { this.collection.dispose(); for (const t of this.debounceTimers.values()) { clearTimeout(t); } for (const d of this.disposables) { d.dispose(); } } } |