harn-rules 0.8.74

Declarative structural rule engine for Harn — rule model, pattern compiler, and matcher built on the harn-hostlib tree-sitter machinery.
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
//! Compile a [`Rule`] into a runnable matcher and run it against source.
//!
//! The atomic tier supports three matcher forms, all reduced to a single
//! [`RuleMatch`] stream:
//!
//! - `pattern` → compiled to a tree-sitter query via [`crate::pattern`].
//! - `kind` → the trivial query `(<kind>) @__match`.
//! - `regex` → a text regex over the source, yielding spans with no AST
//!   metavar bindings.

use std::collections::BTreeMap;

use harn_hostlib::ast::Language;
use serde::Serialize;

use crate::constraint::CompiledConstraint;
use crate::error::RulesError;
use crate::evaluator::CompiledRuleTree;
use crate::fix::{interpolate, splice, AppliedEdit};
use crate::model::{Applicability, Rule, Safety, Severity};
use crate::semantic::enrich_harn_matches;
use crate::transform::CompiledTransform;

/// A byte + row/col span. Rows/cols are 0-based, matching the rest of the
/// Harn AST wire format.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct Span {
    /// Start byte offset.
    pub start_byte: usize,
    /// End byte offset (exclusive).
    pub end_byte: usize,
    /// 0-based start row.
    pub start_row: usize,
    /// 0-based start column.
    pub start_col: usize,
    /// 0-based end row.
    pub end_row: usize,
    /// 0-based end column.
    pub end_col: usize,
}

impl Span {
    pub(crate) fn of(node: tree_sitter::Node<'_>) -> Self {
        let start = node.start_position();
        let end = node.end_position();
        Span {
            start_byte: node.start_byte(),
            end_byte: node.end_byte(),
            start_row: start.row,
            start_col: start.column,
            end_row: end.row,
            end_col: end.column,
        }
    }
}

/// Semantic metadata for a Harn capture. Populated for Harn sources when the
/// engine can resolve the captured node to a local declaration/binding or infer
/// a simple static type.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct BindingMetadata {
    /// Resolved Harn declaration/binding identity for this capture.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resolved: Option<ResolvedBinding>,
    /// Static type label for this capture.
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub ty: Option<String>,
}

impl BindingMetadata {
    /// True when no semantic metadata is available.
    pub fn is_empty(&self) -> bool {
        self.resolved.is_none() && self.ty.is_none()
    }
}

/// A resolved Harn declaration or binding identity.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ResolvedBinding {
    /// Stable id: `<kind>:<name>@<line>:<column>` (1-based line/column).
    pub id: String,
    /// Binding name.
    pub name: String,
    /// Binding kind (`fn`, `pipeline`, `tool`, `struct`, `type`, `let`,
    /// `var`, `const`, `param`, ...).
    pub kind: String,
    /// Span of the declaration/binding name.
    #[serde(flatten)]
    pub span: Span,
}

/// A metavariable binding: the captured text plus where it lives.
#[derive(Debug, Clone)]
pub struct Binding {
    /// The captured source text.
    pub text: String,
    /// The captured node's span.
    pub span: Span,
    /// Optional Harn semantic metadata for this capture.
    pub metadata: BindingMetadata,
}

impl Binding {
    pub(crate) fn new(text: String, span: Span) -> Self {
        Binding {
            text,
            span,
            metadata: BindingMetadata::default(),
        }
    }
}

/// One match of a rule against a file.
#[derive(Debug, Clone)]
pub struct RuleMatch {
    /// The rule that produced this match.
    pub rule_id: String,
    /// The whole matched range (the pattern root, or the regex span).
    pub span: Span,
    /// The matched source text.
    pub text: String,
    /// Metavar bindings, keyed by name (without the leading `$`). Empty for
    /// `kind` and `regex` matchers.
    pub bindings: BTreeMap<String, Binding>,
}

/// The result of applying a codemod rule's `fix` to a source string.
#[derive(Debug, Clone)]
pub struct CodemodResult {
    /// The rewritten source (equals the input when nothing matched).
    pub rewritten: String,
    /// The per-match edits that were spliced in, in document order.
    pub edits: Vec<AppliedEdit>,
    /// Whether the rewrite changed the source.
    pub changed: bool,
    /// The rule's declared safety tier.
    pub safety: Safety,
    /// Whether the fix may be auto-applied or is opt-in only.
    pub applicability: Applicability,
    /// Whether re-running the fix on `rewritten` yields no further change
    /// (a fix should reach a fixed point).
    pub idempotent: bool,
}

/// A rule whose matcher has been compiled and is ready to run.
pub struct CompiledRule {
    rule_id: String,
    language: Language,
    execution: Execution,
    /// `where` predicates; a match survives only when all hold.
    constraints: Vec<CompiledConstraint>,
    /// `transform` definitions: (new metavar name, compiled transform).
    transforms: Vec<(String, CompiledTransform)>,
    /// The `fix` replacement template, if this is a codemod.
    fix: Option<String>,
    /// The fix's safety tier (gates auto-apply).
    safety: Safety,
    /// The diagnostic message (empty for search-only rules).
    message: String,
    /// The diagnostic severity.
    severity: Severity,
}

/// A diagnostic produced by running a rule — the mapping surface onto the
/// linter's `LintDiagnostic` / `FixEdit` (Epic C / the LSP reuse this).
#[derive(Debug, Clone)]
pub struct Diagnostic {
    /// The rule id (also the diagnostic code).
    pub rule_id: String,
    /// The diagnostic message.
    pub message: String,
    /// The severity.
    pub severity: Severity,
    /// The flagged span.
    pub span: Span,
    /// Whether a fix, if present, is auto-applicable or a suggestion.
    pub applicability: Applicability,
    /// The interpolated fix replacement for this match, if the rule has a
    /// `fix`.
    pub fix: Option<String>,
}

enum Execution {
    /// A top-level pure-`regex` rule: scan the raw source text (grep-style),
    /// independent of the tree. Its match span is the regex match range.
    SourceRegex(regex::Regex),
    /// The full matching algebra (atomic + relational + composite).
    Tree(Box<CompiledRuleTree>),
}

impl CompiledRule {
    /// Resolve the rule's language and grammar, then compile its matcher.
    pub fn compile(rule: &Rule) -> Result<Self, RulesError> {
        let language =
            Language::from_name(&rule.language).ok_or_else(|| RulesError::UnknownLanguage {
                rule: rule.id.clone(),
                language: rule.language.clone(),
            })?;

        // A top-level pure-`regex` rule greps the source text directly; any
        // other shape (pattern / kind / relational / composite) compiles to
        // the tree-walking algebra.
        let execution = if rule.rule.is_pure_regex() {
            let pattern = rule.rule.regex.as_ref().expect("pure regex");
            Execution::SourceRegex(regex::Regex::new(pattern).map_err(|err| {
                RulesError::PatternCompile {
                    rule: rule.id.clone(),
                    message: format!("invalid regex `{pattern}`: {err}"),
                }
            })?)
        } else {
            Execution::Tree(Box::new(CompiledRuleTree::compile(
                &rule.id,
                language,
                &rule.rule,
                &rule.utils,
            )?))
        };

        let constraints = rule
            .where_constraints
            .iter()
            .map(|c| CompiledConstraint::compile(&rule.id, language, c))
            .collect::<Result<Vec<_>, _>>()?;

        let transforms = rule
            .transform
            .iter()
            .map(|(name, t)| {
                CompiledTransform::compile(&rule.id, name, t).map(|c| (name.clone(), c))
            })
            .collect::<Result<Vec<_>, _>>()?;

        Ok(CompiledRule {
            rule_id: rule.id.clone(),
            language,
            execution,
            constraints,
            transforms,
            fix: rule.fix.clone(),
            safety: rule.safety,
            message: rule.message.clone(),
            severity: rule.severity,
        })
    }

    /// The language this rule targets.
    pub fn language(&self) -> Language {
        self.language
    }

    /// The fix's declared safety tier.
    pub fn safety(&self) -> Safety {
        self.safety
    }

    /// Whether this rule's fix may be auto-applied (machine-applicable) or
    /// is opt-in only (suggestion).
    pub fn applicability(&self) -> Applicability {
        self.safety.applicability()
    }

    /// The rule's id.
    pub fn id(&self) -> &str {
        &self.rule_id
    }

    /// The rule's diagnostic severity (the default for any diagnostic the
    /// rule produces).
    pub fn severity(&self) -> Severity {
        self.severity
    }

    /// The rule's static diagnostic message (empty for a search-only rule).
    pub fn message(&self) -> &str {
        &self.message
    }

    /// Run the compiled rule against `source`, returning matches in
    /// document order. Matches that fail any `where` constraint are dropped.
    pub fn run(&self, source: &str) -> Result<Vec<RuleMatch>, RulesError> {
        let mut matches = match &self.execution {
            Execution::SourceRegex(regex) => self.run_regex(regex, source),
            Execution::Tree(tree) => tree
                .find(&self.rule_id, self.language, source)?
                .into_iter()
                .map(|m| RuleMatch {
                    rule_id: self.rule_id.clone(),
                    span: m.span,
                    text: m.text,
                    bindings: m.bindings,
                })
                .collect(),
        };
        if self.language == Language::Harn && !matches.is_empty() {
            enrich_harn_matches(source, &mut matches).map_err(|message| {
                RulesError::SourceParse {
                    rule: self.rule_id.clone(),
                    message,
                }
            })?;
        }
        if !self.constraints.is_empty() {
            matches.retain(|m| self.satisfies_constraints(m));
        }
        Ok(matches)
    }

    /// True when every `where` constraint holds for this match. A
    /// constraint whose metavar is unbound (not captured) fails closed.
    fn satisfies_constraints(&self, m: &RuleMatch) -> bool {
        self.constraints
            .iter()
            .all(|c| m.bindings.get(&c.metavar).is_some_and(|b| c.evaluate(b)))
    }

    /// Apply this codemod rule's `fix` to `source`, returning the rewritten
    /// text plus the per-match edits. Each match's `fix` template is
    /// interpolated from its captured metavars plus any `transform`-derived
    /// ones. Errors if the rule has no `fix`.
    ///
    /// This computes the preview only — it does not enforce the safety gate.
    /// Use [`CompiledRule::auto_apply`] to refuse non-machine-applicable
    /// fixes, or [`CompiledRule::apply_checked`] to also assert idempotency.
    pub fn apply(&self, source: &str) -> Result<CodemodResult, RulesError> {
        let (rewritten, edits) = self.rewrite(source)?;
        let changed = rewritten != source;
        // Idempotency: re-running the fix on its own output must not change
        // it further (the fix should reach a fixed point).
        let (twice, _) = self.rewrite(&rewritten)?;
        let idempotent = twice == rewritten;
        Ok(CodemodResult {
            rewritten,
            edits,
            changed,
            safety: self.safety,
            applicability: self.applicability(),
            idempotent,
        })
    }

    /// Like [`CompiledRule::apply`], but refuses to apply a fix whose
    /// `safety` is above the machine-applicable threshold (`scope-local` and
    /// riskier). This is the gate `harn codemod --apply` uses by default.
    pub fn auto_apply(&self, source: &str) -> Result<CodemodResult, RulesError> {
        if !self.safety.is_auto_applicable() {
            return Err(RulesError::NotAutoApplicable {
                rule: self.rule_id.clone(),
                safety: format!("{:?}", self.safety),
            });
        }
        self.apply(source)
    }

    /// Like [`CompiledRule::apply`], but fails if the fix is not idempotent.
    /// Used by the codemod runner and the rule-test harness to assert a fix
    /// reaches a fixed point.
    pub fn apply_checked(&self, source: &str) -> Result<CodemodResult, RulesError> {
        let result = self.apply(source)?;
        if !result.idempotent {
            return Err(RulesError::NotIdempotent {
                rule: self.rule_id.clone(),
            });
        }
        Ok(result)
    }

    /// Run the rule and produce one [`Diagnostic`] per match — the surface
    /// the linter (Epic C) and the LSP convert into `LintDiagnostic` /
    /// `FixEdit`. Each diagnostic carries the interpolated fix (if any) and
    /// its applicability tier.
    pub fn diagnostics(&self, source: &str) -> Result<Vec<Diagnostic>, RulesError> {
        let applicability = self.applicability();
        let matches = self.run(source)?;
        Ok(matches
            .iter()
            .map(|m| Diagnostic {
                rule_id: self.rule_id.clone(),
                message: self.message.clone(),
                severity: self.severity,
                span: m.span,
                applicability,
                fix: self.fix.as_ref().map(|template| {
                    let vars = self.metavars_for(m);
                    interpolate(template, &vars)
                }),
            })
            .collect())
    }

    /// The core rewrite: run the rule and splice each match's interpolated
    /// fix. Returns the rewritten text and the edits.
    fn rewrite(&self, source: &str) -> Result<(String, Vec<AppliedEdit>), RulesError> {
        let template = self
            .fix
            .as_ref()
            .ok_or_else(|| RulesError::PatternCompile {
                rule: self.rule_id.clone(),
                message: "apply requires a `fix` template; this rule has none".into(),
            })?;

        let matches = dedupe_overlapping(self.run(source)?);
        let edits: Vec<AppliedEdit> = matches
            .iter()
            .map(|m| {
                let vars = self.metavars_for(m);
                AppliedEdit {
                    span: m.span,
                    before: m.text.clone(),
                    replacement: interpolate(template, &vars),
                }
            })
            .collect();
        Ok((splice(source, &edits), edits))
    }

    /// Build the full metavar map for a match: captured bindings plus the
    /// `transform`-synthesized metavars (which may shadow captures).
    fn metavars_for(&self, m: &RuleMatch) -> BTreeMap<String, String> {
        let mut vars: BTreeMap<String, String> = m
            .bindings
            .iter()
            .map(|(name, binding)| (name.clone(), binding.text.clone()))
            .collect();
        for (name, transform) in &self.transforms {
            let input = m
                .bindings
                .get(&transform.source)
                .map(|b| b.text.as_str())
                .unwrap_or("");
            vars.insert(name.clone(), transform.apply(input));
        }
        vars
    }

    fn run_regex(&self, regex: &regex::Regex, source: &str) -> Vec<RuleMatch> {
        let mut matches = Vec::new();
        // `find_iter` yields non-overlapping matches in ascending byte order, so
        // a single forward-walking cursor computes every row/col without
        // rescanning from the start of the document for each match (which made
        // this O(matches × len) — quadratic on files with many hits).
        let mut cursor = RowColCursor::new(source);
        for m in regex.find_iter(source) {
            let (start_row, start_col) = cursor.advance_to(m.start());
            let (end_row, end_col) = cursor.advance_to(m.end());
            matches.push(RuleMatch {
                rule_id: self.rule_id.clone(),
                span: Span {
                    start_byte: m.start(),
                    end_byte: m.end(),
                    start_row,
                    start_col,
                    end_row,
                    end_col,
                },
                text: m.as_str().to_string(),
                bindings: BTreeMap::new(),
            });
        }
        matches
    }
}

/// Drop matches that overlap an already-kept match, keeping the **outermost**
/// (and, among equal extents, the first). A tree query naturally yields nested
/// matches — e.g. `$X + $Y` matches both `(a+b)+c` and its inner `a+b` — and
/// splicing both would apply overlapping byte edits, corrupting the output or
/// panicking `replace_range` on a stale offset. A codemod must rewrite each
/// region once, so we keep the enclosing match and discard anything nested in
/// or straddling it. Matches are returned in document (start-byte) order.
fn dedupe_overlapping(mut matches: Vec<RuleMatch>) -> Vec<RuleMatch> {
    // Outermost-first: smallest start, then largest end (widest span wins a tie
    // on start). After filtering we restore document order.
    matches.sort_by(|a, b| {
        a.span
            .start_byte
            .cmp(&b.span.start_byte)
            .then(b.span.end_byte.cmp(&a.span.end_byte))
    });
    let mut kept: Vec<RuleMatch> = Vec::with_capacity(matches.len());
    let mut covered_to = 0usize; // exclusive end of the last kept span
    for m in matches {
        // Keep only when this match starts at or after the end of the last kept
        // one (adjacency is fine; any earlier start means it is nested in, or
        // straddles, an already-kept span).
        if m.span.start_byte >= covered_to {
            covered_to = m.span.end_byte.max(covered_to);
            kept.push(m);
        }
    }
    kept
}

/// Forward-only cursor that maps byte offsets to `(row, col)` while walking a
/// document at most once. The regex matcher has no tree-sitter node to read
/// positions from and visits offsets in ascending order, so advancing this
/// cursor avoids the per-match rescan a stateless lookup would cost.
struct RowColCursor<'a> {
    source: &'a str,
    byte: usize,
    row: usize,
    col: usize,
}

impl<'a> RowColCursor<'a> {
    fn new(source: &'a str) -> Self {
        Self {
            source,
            byte: 0,
            row: 0,
            col: 0,
        }
    }

    /// Advance to `target` (which must be `>=` the current position and a char
    /// boundary) and return the row/col there. `row` counts preceding newlines;
    /// `col` counts characters since the last newline.
    fn advance_to(&mut self, target: usize) -> (usize, usize) {
        for ch in self.source[self.byte..target].chars() {
            if ch == '\n' {
                self.row += 1;
                self.col = 0;
            } else {
                self.col += 1;
            }
        }
        self.byte = target;
        (self.row, self.col)
    }
}

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

    fn rule(toml: &str) -> CompiledRule {
        let parsed = Rule::from_toml_str(toml).expect("rule parses");
        CompiledRule::compile(&parsed).expect("rule compiles")
    }

    #[test]
    fn pattern_rule_binds_metavars() {
        let compiled = rule(
            r#"
            id = "destructure-default"
            language = "typescript"
            fix = "{ $KEY: $SRC }"
            [rule]
            pattern = "$SRC?.$KEY ?? $DEFAULT"
            "#,
        );
        let matches = compiled
            .run("const a = cfg?.timeout ?? 30;\nconst b = opts?.retries ?? 3;\n")
            .unwrap();
        assert_eq!(matches.len(), 2);
        assert_eq!(matches[0].bindings["SRC"].text, "cfg");
        assert_eq!(matches[0].bindings["KEY"].text, "timeout");
        assert_eq!(matches[0].bindings["DEFAULT"].text, "30");
        assert_eq!(matches[1].bindings["SRC"].text, "opts");
        // The match span covers the whole expression.
        assert_eq!(matches[0].text, "cfg?.timeout ?? 30");
        assert_eq!(matches[0].span.start_row, 0);
        assert_eq!(matches[1].span.start_row, 1);
    }

    #[test]
    fn nested_matches_do_not_corrupt_or_panic_on_apply() {
        // `$X + $Y` matches the outer `(a+b)+c` AND the inner `a+b` (distinct
        // spans). Without overlap resolution, splicing both byte-edits panics
        // `replace_range` on a stale offset or corrupts the output. The engine
        // must keep the outermost match and rewrite the region exactly once.
        let compiled = rule(
            r#"
            id = "sum-binop"
            language = "typescript"
            fix = "sum($X, $Y)"
            [rule]
            pattern = "$X + $Y"
            "#,
        );
        // More than one match exists (outer + inner) before dedup.
        assert!(compiled.run("const z = a + b + c;\n").unwrap().len() >= 2);
        let result = compiled.apply("const z = a + b + c;\n").unwrap();
        // Exactly one outer rewrite; inner `a + b` survives verbatim inside $X.
        assert_eq!(result.rewritten, "const z = sum(a + b, c);\n");
        assert_eq!(result.edits.len(), 1);
        assert!(result.changed);
    }

    #[test]
    fn dedupe_overlapping_keeps_outermost_in_document_order() {
        let span = |s: usize, e: usize| Span {
            start_byte: s,
            end_byte: e,
            start_row: 0,
            start_col: s,
            end_row: 0,
            end_col: e,
        };
        let m = |s: usize, e: usize| RuleMatch {
            rule_id: "r".into(),
            span: span(s, e),
            text: String::new(),
            bindings: BTreeMap::new(),
        };
        // outer [0,9) contains inner [0,5); [10,14) is disjoint.
        let kept = dedupe_overlapping(vec![m(0, 5), m(0, 9), m(10, 14)]);
        let spans: Vec<_> = kept
            .iter()
            .map(|m| (m.span.start_byte, m.span.end_byte))
            .collect();
        assert_eq!(spans, vec![(0, 9), (10, 14)]);
    }

    #[test]
    fn kind_rule_matches_node_kind() {
        let compiled = rule(
            r#"
            id = "find-calls"
            language = "python"
            [rule]
            kind = "call"
            "#,
        );
        let matches = compiled.run("print(x)\nlog(y)\n").unwrap();
        assert_eq!(matches.len(), 2);
        assert_eq!(matches[0].text, "print(x)");
        assert!(matches[0].bindings.is_empty());
    }

    #[test]
    fn regex_rule_matches_text() {
        let compiled = rule(
            r#"
            id = "todo"
            language = "rust"
            message = "Found a TODO"
            [rule]
            regex = "TODO\\(\\w+\\)"
            "#,
        );
        let matches = compiled
            .run("fn f() {\n    // TODO(ken) fix\n    // todo lower\n}\n")
            .unwrap();
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].text, "TODO(ken)");
        assert_eq!(matches[0].span.start_row, 1);
    }

    #[test]
    fn unknown_language_is_an_error() {
        let parsed = Rule::from_toml_str(
            r#"
            id = "x"
            language = "cobol"
            [rule]
            kind = "foo"
            "#,
        )
        .unwrap();
        assert!(matches!(
            CompiledRule::compile(&parsed),
            Err(RulesError::UnknownLanguage { .. })
        ));
    }

    #[test]
    fn invalid_pattern_surfaces_compile_error() {
        let parsed = Rule::from_toml_str(
            r#"
            id = "x"
            language = "typescript"
            [rule]
            pattern = "foo($$$ARGS)"
            "#,
        )
        .unwrap();
        assert!(matches!(
            CompiledRule::compile(&parsed),
            Err(RulesError::PatternCompile { .. })
        ));
    }

    #[test]
    fn harn_resolves_same_named_call_sites_by_binding_identity() {
        let compiled = rule(
            r#"
            id = "top-level-target"
            language = "harn"
            [rule]
            pattern = "$FN($ARG)"

            [[where]]
            metavar = "FN"
            resolvesTo = { name = "target", kind = "fn", line = 1 }
            "#,
        );
        let source = r"fn target(value: int) -> int {
  return value
}

fn call_shadowed(target: fn(int) -> int) {
  target(1)
}

fn call_global() {
  target(2)
}
";
        let matches = compiled.run(source).unwrap();
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].text, "target(2)");
        let binding = &matches[0].bindings["FN"];
        let resolved = binding.metadata.resolved.as_ref().unwrap();
        assert_eq!(resolved.name, "target");
        assert_eq!(resolved.kind, "fn");
        assert_eq!(resolved.span.start_row, 0);
        assert_eq!(binding.metadata.ty.as_deref(), Some("fn(int) -> int"));
    }

    #[test]
    fn harn_capture_type_constraint_filters_matches() {
        let compiled = rule(
            r#"
            id = "int-logs"
            language = "harn"
            [rule]
            pattern = "log($VALUE)"

            [[where]]
            metavar = "VALUE"
            type = "int"
            "#,
        );
        let source = r#"fn main() {
  let count: int = 1
  let label: string = "one"
  log(count)
  log(label)
}
"#;
        let matches = compiled.run(source).unwrap();
        assert_eq!(matches.len(), 1);
        let value = &matches[0].bindings["VALUE"];
        assert_eq!(value.text, "count");
        assert_eq!(value.metadata.ty.as_deref(), Some("int"));
        assert_eq!(
            value
                .metadata
                .resolved
                .as_ref()
                .map(|resolved| resolved.kind.as_str()),
            Some("let")
        );
    }

    #[test]
    fn harn_initializer_uses_outer_binding_scope() {
        let compiled = rule(
            r#"
            id = "outer-initializer"
            language = "harn"
            [rule]
            pattern = "log($VALUE)"

            [[where]]
            metavar = "VALUE"
            resolvesTo = { name = "value", kind = "let", line = 2 }
            "#,
        );
        let source = r"fn main() {
  let value: int = 1
  if true {
    let value: string = log(value)
  }
}
";
        let matches = compiled.run(source).unwrap();
        assert_eq!(matches.len(), 1);
        let value = &matches[0].bindings["VALUE"];
        assert_eq!(value.text, "value");
        assert_eq!(
            value
                .metadata
                .resolved
                .as_ref()
                .map(|resolved| resolved.span.start_row),
            Some(1)
        );
    }
}