splice 2.6.1

Span-safe refactoring kernel for 7 languages with Magellan code graph integration
Documentation
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Compiler and AST validation.
//!
//! This module runs cargo check and rust-analyzer to validate
//! that patches produce valid Rust code.

pub mod gates;

use crate::error::{Diagnostic, DiagnosticLevel, Result, SpliceError};
use std::path::{Path, PathBuf};
use std::process::Command;
use which::which;

/// rust-analyzer execution mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AnalyzerMode {
    /// rust-analyzer disabled (default).
    Off,

    /// Use rust-analyzer from PATH.
    Path,

    /// Use rust-analyzer from explicit path.
    Explicit(&'static str),
}

/// Validation result from cargo check.
#[derive(Debug, Clone, PartialEq)]
pub enum ValidationResult {
    /// Validation passed (no errors).
    Pass,

    /// Validation failed with compiler errors.
    Fail {
        /// List of compiler errors found.
        errors: Vec<CompilerError>,
    },
}

/// Represents a compiler error or warning.
#[derive(Debug, Clone, PartialEq)]
pub struct CompilerError {
    /// Error level (error, warning, etc.).
    pub level: ErrorLevel,

    /// File path where the error occurred.
    pub file: String,

    /// Line number.
    pub line: usize,

    /// Column number.
    pub column: usize,

    /// Error message.
    pub message: String,

    /// Optional compiler/analyzer error code.
    pub code: Option<String>,

    /// Optional help/note text associated with this diagnostic.
    pub note: Option<String>,
}

/// Error level from compiler.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ErrorLevel {
    /// Error-level diagnostic.
    Error,
    /// Warning-level diagnostic.
    Warning,
    /// Note-level diagnostic.
    Note,
    /// Help-level diagnostic.
    Help,
}

/// Run rust-analyzer validation gate.
///
/// This function invokes rust-analyzer as an external process and treats
/// ANY diagnostic output as a failure. No LSP parsing, no JSON, just
/// simple pass/fail based on diagnostic presence.
///
/// # Arguments
/// * `workspace_dir` - Directory containing Cargo.toml
/// * `mode` - Analyzer execution mode (off/path/explicit)
///
/// # Returns
/// * `Ok(())` - No diagnostics found
/// * `Err(SpliceError::AnalyzerNotAvailable)` - rust-analyzer not found
/// * `Err(SpliceError::AnalyzerFailed)` - Diagnostics detected
pub fn gate_rust_analyzer(workspace_dir: &Path, mode: AnalyzerMode) -> Result<()> {
    // If analyzer is off, skip gate
    if matches!(mode, AnalyzerMode::Off) {
        return Ok(());
    }

    // Determine rust-analyzer binary path
    let analyzer_binary = match mode {
        AnalyzerMode::Path => "rust-analyzer",
        AnalyzerMode::Explicit(path) => path,
        AnalyzerMode::Off => unreachable!(),
    };
    let analyzer_meta = collect_tool_metadata(analyzer_binary, &["--version"]);

    // Invoke rust-analyzer to check for diagnostics
    // We use "analyze" command which outputs diagnostics to stdout
    let output = Command::new(analyzer_binary)
        .args(["check", "--workspace"])
        .current_dir(workspace_dir)
        .output();

    match output {
        Ok(result) => {
            // rust-analyzer exits with 0 even if diagnostics are present
            // We need to check stdout/stderr for any diagnostic output
            let stdout = String::from_utf8_lossy(&result.stdout);
            let stderr = String::from_utf8_lossy(&result.stderr);

            // Combine stdout and stderr
            let combined = format!("{}{}", stdout, stderr);

            // If there's ANY output, treat it as a failure
            if !combined.trim().is_empty() {
                let compiler_errors = parse_rust_analyzer_output(&combined);

                let diagnostics = if compiler_errors.is_empty() {
                    vec![
                        Diagnostic::new("rust-analyzer", DiagnosticLevel::Error, combined.clone())
                            .with_file(workspace_dir.to_path_buf())
                            .with_tool_metadata(Some(&analyzer_meta)),
                    ]
                } else {
                    compiler_errors
                        .into_iter()
                        .map(|err| {
                            let remediation =
                                err.code.as_deref().and_then(remediation_link_for_code);
                            Diagnostic::new(
                                "rust-analyzer",
                                DiagnosticLevel::from(err.level),
                                err.message,
                            )
                            .with_file(Path::new(&err.file).to_path_buf())
                            .with_position(nonzero(err.line), nonzero(err.column))
                            .with_code(err.code.clone())
                            .with_note(err.note.clone())
                            .with_tool_metadata(Some(&analyzer_meta))
                            .with_remediation(remediation)
                        })
                        .collect()
                };

                return Err(SpliceError::AnalyzerFailed {
                    output: combined,
                    diagnostics,
                });
            }

            Ok(())
        }
        Err(e) => {
            // Failed to invoke rust-analyzer
            if e.kind() == std::io::ErrorKind::NotFound {
                return Err(SpliceError::AnalyzerNotAvailable {
                    mode: format!("{:?}", mode),
                });
            }

            // Other I/O error
            Err(SpliceError::Other(format!(
                "Failed to invoke rust-analyzer: {}",
                e
            )))
        }
    }
}

/// Run cargo check and parse the output.
///
/// Returns ValidationResult::Pass if no errors, or Fail with error details.
pub fn validate_with_cargo(project_dir: &Path) -> Result<ValidationResult> {
    let output = Command::new("cargo")
        .args(["check", "--message-format=short"])
        .current_dir(project_dir)
        .output()?;

    if output.status.success() {
        return Ok(ValidationResult::Pass);
    }

    // Parse stderr for error messages
    let stderr = String::from_utf8_lossy(&output.stderr);
    let errors = parse_cargo_output(&stderr);

    Ok(ValidationResult::Fail { errors })
}

/// Parse cargo check output into CompilerError structs.
///
/// This function is public for testing purposes.
pub fn parse_cargo_output(output: &str) -> Vec<CompilerError> {
    parse_rust_style_output(output)
}

/// Parse rust-analyzer CLI output into CompilerError structs.
pub fn parse_rust_analyzer_output(output: &str) -> Vec<CompilerError> {
    parse_rust_style_output(output)
}

/// Parse TypeScript compiler (tsc) output into CompilerError structs.
///
/// TypeScript error format:
///   file.ts(line,col): error TSXXXX: message
///   file.ts(line,col): warning TSXXXX: message
///
/// # Examples
/// ```text
/// test.ts(2,5): error TS1002: Unterminated string literal
/// another.ts(10,12): warning TS2304: Cannot find name 'foo'
/// ```
///
/// This function is public for testing purposes.
pub fn parse_typescript_output(output: &str) -> Vec<CompilerError> {
    use regex::Regex;

    // TypeScript format: file(line,col): error/warning TSXXXX: message
    // Example: test.ts(2,5): error TS1002: Unterminated string
    let re = match Regex::new(r"^(.+?)\((\d+),(\d+)\): (error|warning) (TS\d+): (.+)$") {
        Ok(re) => re,
        Err(_) => return Vec::new(),
    };

    let mut errors = Vec::new();

    for line in output.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }

        if let Some(caps) = re.captures(trimmed) {
            let file = caps
                .get(1)
                .map(|m| m.as_str().to_string())
                .unwrap_or_default();
            let line_num = caps
                .get(2)
                .and_then(|m| m.as_str().parse().ok())
                .unwrap_or(1);
            let column = caps
                .get(3)
                .and_then(|m| m.as_str().parse().ok())
                .unwrap_or(0);
            let level_str = caps.get(4).map(|m| m.as_str()).unwrap_or("error");
            let code = caps.get(5).map(|m| m.as_str().to_string());
            let message = caps
                .get(6)
                .map(|m| m.as_str().to_string())
                .unwrap_or_default();

            let level = match level_str {
                "error" => ErrorLevel::Error,
                "warning" => ErrorLevel::Warning,
                _ => ErrorLevel::Error,
            };

            errors.push(CompilerError {
                level,
                file,
                line: line_num,
                column,
                code,
                message,
                note: None,
            });
        }
    }

    errors
}

fn parse_rust_style_output(output: &str) -> Vec<CompilerError> {
    let mut errors = Vec::new();
    let mut pending_error: Option<PendingDiagnostic> = None;
    let mut last_index: Option<usize> = None;

    for line in output.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }

        if let Some(diag) = parse_error_header(trimmed) {
            pending_error = Some(diag);
            continue;
        }

        if let Some((file, line_num, column)) = parse_location_line(trimmed) {
            if let Some(pending) = pending_error.take() {
                errors.push(CompilerError {
                    level: pending.level,
                    file,
                    line: line_num,
                    column,
                    message: pending.message,
                    code: pending.code,
                    note: None,
                });
                last_index = Some(errors.len() - 1);
            }
            continue;
        }

        if let Some(note) = parse_note_line(trimmed) {
            if let Some(idx) = last_index {
                if let Some(entry) = errors.get_mut(idx) {
                    entry.note = Some(match &entry.note {
                        Some(existing) => format!("{}\n{}", existing, note),
                        None => note,
                    });
                }
            }
            continue;
        }

        if let Some(help) = parse_help_line(trimmed) {
            if let Some(idx) = last_index {
                if let Some(entry) = errors.get_mut(idx) {
                    entry.note = Some(match &entry.note {
                        Some(existing) => format!("{}\n{}", existing, help),
                        None => help,
                    });
                }
            }
            continue;
        }
    }

    errors
}

/// Parse an error/warning header line.
fn parse_error_header(line: &str) -> Option<PendingDiagnostic> {
    if let Some(rest) = line.strip_prefix("error[") {
        if let Some(idx) = rest.find("]:") {
            let code = rest[..idx].to_string();
            let message = rest[idx + 2..].trim().to_string();
            return Some(PendingDiagnostic {
                level: ErrorLevel::Error,
                message,
                code: Some(code),
            });
        }
    } else if let Some(rest) = line.strip_prefix("error:") {
        return Some(PendingDiagnostic {
            level: ErrorLevel::Error,
            message: rest.trim().to_string(),
            code: None,
        });
    } else if let Some(rest) = line.strip_prefix("warning[") {
        if let Some(idx) = rest.find("]:") {
            let code = rest[..idx].to_string();
            let message = rest[idx + 2..].trim().to_string();
            return Some(PendingDiagnostic {
                level: ErrorLevel::Warning,
                message,
                code: Some(code),
            });
        }
    } else if let Some(rest) = line.strip_prefix("warning:") {
        return Some(PendingDiagnostic {
            level: ErrorLevel::Warning,
            message: rest.trim().to_string(),
            code: None,
        });
    }

    None
}

#[derive(Debug, Clone)]
struct PendingDiagnostic {
    level: ErrorLevel,
    message: String,
    code: Option<String>,
}

fn parse_note_line(line: &str) -> Option<String> {
    parse_labelled_line(line, "note")
}

fn parse_help_line(line: &str) -> Option<String> {
    parse_labelled_line(line, "help")
}

fn parse_labelled_line(line: &str, label: &str) -> Option<String> {
    let trimmed = line.trim_start_matches('|').trim();

    if let Some(rest) = trimmed.strip_prefix(label) {
        return Some(rest.trim_start_matches(':').trim().to_string());
    }

    if let Some(rest) = trimmed.strip_prefix(&format!("= {}", label)) {
        return Some(rest.trim_start_matches(':').trim().to_string());
    }

    None
}

/// Parse a location line like "   --> file.rs:line:column"
/// Returns (file, line, column) if successful.
fn parse_location_line(line: &str) -> Option<(String, usize, usize)> {
    let line = line.trim();

    // Match "   --> file:line:column" or "  --> file:line:column"
    if let Some(rest) = line.strip_prefix("-->") {
        let rest = rest.trim();
        // Parse "file:line:column"
        if let Some(colon_idx) = rest.rfind(':') {
            let column_str = &rest[colon_idx + 1..];
            let column = column_str.parse::<usize>().ok()?;

            let before_column = &rest[..colon_idx];
            if let Some(line_colon_idx) = before_column.rfind(':') {
                let line_str = &before_column[line_colon_idx + 1..];
                let line_num = line_str.parse::<usize>().ok()?;
                let file = before_column[..line_colon_idx].to_string();
                return Some((file, line_num, column));
            }
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_rust_analyzer_output_extracts_file_line() {
        let sample = r#"
error[E0425]: cannot find function `missing_helper` in this scope
 --> src/lib.rs:2:5
  |
2 |     missing_helper(name)
  |     ^^^^^^^^^^^^^^ not found in this scope
help: consider importing `missing_helper`
"#;

        let errors = parse_rust_analyzer_output(sample);
        assert_eq!(errors.len(), 1, "expected one diagnostic");
        let diag = &errors[0];
        assert_eq!(diag.file, "src/lib.rs");
        assert_eq!(diag.line, 2);
        assert_eq!(diag.column, 5);
        assert!(
            diag.message.contains("missing_helper"),
            "diagnostic message should mention missing helper"
        );
        assert_eq!(diag.code.as_deref(), Some("E0425"));
        assert!(
            diag.note
                .as_deref()
                .map(|n| n.contains("consider importing"))
                .unwrap_or(false),
            "diagnostic note should capture help text"
        );
    }
}

fn nonzero(value: usize) -> Option<usize> {
    if value == 0 {
        None
    } else {
        Some(value)
    }
}

/// Metadata about an external validation tool.
#[derive(Debug, Clone)]
pub struct ToolMetadata {
    /// Absolute path to the binary, if resolvable.
    pub path: Option<PathBuf>,
    /// Version string returned by the tool.
    pub version: Option<String>,
}

/// Resolve tool metadata (path + version) for diagnostics.
pub fn collect_tool_metadata(binary: &str, version_args: &[&str]) -> ToolMetadata {
    let path = which(binary).ok();
    let version = Command::new(binary)
        .args(version_args)
        .output()
        .ok()
        .and_then(|output| {
            let selected = if output.stdout.is_empty() {
                &output.stderr
            } else {
                &output.stdout
            };
            let text = String::from_utf8_lossy(selected).trim().to_string();
            if text.is_empty() {
                None
            } else {
                Some(text)
            }
        });

    ToolMetadata { path, version }
}

/// Best-effort remediation link for a compiler/analyzer error code.
pub fn remediation_link_for_code(code: &str) -> Option<String> {
    if code.starts_with('E') && code.len() == 5 && code[1..].chars().all(|ch| ch.is_ascii_digit()) {
        Some(format!(
            "https://doc.rust-lang.org/error-index.html#{}",
            code
        ))
    } else if code.starts_with("TS")
        && code.len() > 2
        && code[2..].chars().all(|ch| ch.is_ascii_digit())
    {
        Some(format!("https://www.typescriptlang.org/errors/{}", code))
    } else {
        None
    }
}