heal-cli 0.2.1

Hook-driven Evaluation & Autonomous Loop — code-health harness CLI for AI coding agents
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
//! Language registry — maps file extensions to a tree-sitter grammar plus the
//! per-language compiled query set used by complexity observers.
//!
//! Compiled queries (and their typed capture indices) are cached statically per
//! language variant via `OnceLock`, so callers that analyze thousands of files
//! pay query-compile cost exactly once per (language, query) pair.
//!
//! Variants are gated by `#[non_exhaustive]` so adding `JavaScript` / `Jsx`
//! later doesn't break exhaustive matches downstream. Each variant is also
//! gated behind a Cargo feature (`lang-ts`, `lang-rust`) so a downstream binary
//! can drop unused grammars to shrink the build. The crate `compile_error!`s
//! when no language feature is enabled — we have no need for a registry that
//! supports zero languages, and excluding the variant guard keeps the public
//! API total.

use std::path::Path;
use std::sync::OnceLock;

use tree_sitter::{Language as TsLanguage, Query};
#[cfg(feature = "lang-ts")]
use tree_sitter_typescript::{LANGUAGE_TSX, LANGUAGE_TYPESCRIPT};

#[cfg(not(any(
    feature = "lang-ts",
    feature = "lang-js",
    feature = "lang-py",
    feature = "lang-go",
    feature = "lang-scala",
    feature = "lang-rust"
)))]
compile_error!(
    "heal-observer requires at least one language feature: lang-ts / lang-js / lang-py / lang-go / lang-scala / lang-rust"
);

#[cfg(feature = "lang-ts")]
const TYPESCRIPT_FUNCTIONS_QUERY: &str = include_str!("../../queries/typescript/functions.scm");
#[cfg(feature = "lang-ts")]
const TYPESCRIPT_CCN_QUERY: &str = include_str!("../../queries/typescript/ccn.scm");
#[cfg(feature = "lang-ts")]
const TYPESCRIPT_COGNITIVE_QUERY: &str = include_str!("../../queries/typescript/cognitive.scm");
#[cfg(feature = "lang-ts")]
const TYPESCRIPT_LCOM_QUERY: &str = include_str!("../../queries/typescript/lcom.scm");

#[cfg(feature = "lang-js")]
const JAVASCRIPT_FUNCTIONS_QUERY: &str = include_str!("../../queries/javascript/functions.scm");
#[cfg(feature = "lang-js")]
const JAVASCRIPT_CCN_QUERY: &str = include_str!("../../queries/javascript/ccn.scm");
#[cfg(feature = "lang-js")]
const JAVASCRIPT_COGNITIVE_QUERY: &str = include_str!("../../queries/javascript/cognitive.scm");
#[cfg(feature = "lang-js")]
const JAVASCRIPT_LCOM_QUERY: &str = include_str!("../../queries/javascript/lcom.scm");

#[cfg(feature = "lang-py")]
const PYTHON_FUNCTIONS_QUERY: &str = include_str!("../../queries/python/functions.scm");
#[cfg(feature = "lang-py")]
const PYTHON_CCN_QUERY: &str = include_str!("../../queries/python/ccn.scm");
#[cfg(feature = "lang-py")]
const PYTHON_COGNITIVE_QUERY: &str = include_str!("../../queries/python/cognitive.scm");
#[cfg(feature = "lang-py")]
const PYTHON_LCOM_QUERY: &str = include_str!("../../queries/python/lcom.scm");

#[cfg(feature = "lang-go")]
const GO_FUNCTIONS_QUERY: &str = include_str!("../../queries/go/functions.scm");
#[cfg(feature = "lang-go")]
const GO_CCN_QUERY: &str = include_str!("../../queries/go/ccn.scm");
#[cfg(feature = "lang-go")]
const GO_COGNITIVE_QUERY: &str = include_str!("../../queries/go/cognitive.scm");
#[cfg(feature = "lang-go")]
const GO_LCOM_QUERY: &str = include_str!("../../queries/go/lcom.scm");

#[cfg(feature = "lang-scala")]
const SCALA_FUNCTIONS_QUERY: &str = include_str!("../../queries/scala/functions.scm");
#[cfg(feature = "lang-scala")]
const SCALA_CCN_QUERY: &str = include_str!("../../queries/scala/ccn.scm");
#[cfg(feature = "lang-scala")]
const SCALA_COGNITIVE_QUERY: &str = include_str!("../../queries/scala/cognitive.scm");
#[cfg(feature = "lang-scala")]
const SCALA_LCOM_QUERY: &str = include_str!("../../queries/scala/lcom.scm");

#[cfg(feature = "lang-rust")]
const RUST_FUNCTIONS_QUERY: &str = include_str!("../../queries/rust/functions.scm");
#[cfg(feature = "lang-rust")]
const RUST_CCN_QUERY: &str = include_str!("../../queries/rust/ccn.scm");
#[cfg(feature = "lang-rust")]
const RUST_COGNITIVE_QUERY: &str = include_str!("../../queries/rust/cognitive.scm");
#[cfg(feature = "lang-rust")]
const RUST_LCOM_QUERY: &str = include_str!("../../queries/rust/lcom.scm");

#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Language {
    #[cfg(feature = "lang-ts")]
    TypeScript,
    #[cfg(feature = "lang-ts")]
    Tsx,
    #[cfg(feature = "lang-js")]
    JavaScript,
    #[cfg(feature = "lang-js")]
    Jsx,
    #[cfg(feature = "lang-py")]
    Python,
    #[cfg(feature = "lang-go")]
    Go,
    #[cfg(feature = "lang-scala")]
    Scala,
    #[cfg(feature = "lang-rust")]
    Rust,
}

/// A compiled tree-sitter `Query` paired with its typed capture indices.
/// The `captures` payload differs per query role (functions/CCN/cognitive).
pub struct CompiledQuery<C: 'static> {
    pub query: Query,
    pub captures: C,
}

pub struct FunctionCaptures {
    pub scope: u32,
}

pub struct CcnCaptures {
    pub point: u32,
    pub binary: u32,
}

pub struct CognitiveCaptures {
    pub if_: u32,
    pub else_: u32,
    pub inc_and_nest: u32,
    pub inc: u32,
    pub binary: u32,
}

pub struct LcomCaptures {
    pub class_scope: u32,
}

struct LanguageQueries {
    functions: OnceLock<CompiledQuery<FunctionCaptures>>,
    ccn: OnceLock<CompiledQuery<CcnCaptures>>,
    cognitive: OnceLock<CompiledQuery<CognitiveCaptures>>,
    lcom: OnceLock<CompiledQuery<LcomCaptures>>,
}

impl LanguageQueries {
    const fn new() -> Self {
        Self {
            functions: OnceLock::new(),
            ccn: OnceLock::new(),
            cognitive: OnceLock::new(),
            lcom: OnceLock::new(),
        }
    }
}

#[cfg(feature = "lang-ts")]
static TYPESCRIPT_QUERIES: LanguageQueries = LanguageQueries::new();
#[cfg(feature = "lang-ts")]
static TSX_QUERIES: LanguageQueries = LanguageQueries::new();
// JS and JSX share the same grammar (`tree_sitter_javascript::LANGUAGE`)
// and the same `queries/javascript/*.scm` sources, so a single cache
// serves both — TSX needs its own because tree-sitter ships a
// distinct `LANGUAGE_TSX`.
#[cfg(feature = "lang-js")]
static JAVASCRIPT_QUERIES: LanguageQueries = LanguageQueries::new();
#[cfg(feature = "lang-py")]
static PYTHON_QUERIES: LanguageQueries = LanguageQueries::new();
#[cfg(feature = "lang-go")]
static GO_QUERIES: LanguageQueries = LanguageQueries::new();
#[cfg(feature = "lang-scala")]
static SCALA_QUERIES: LanguageQueries = LanguageQueries::new();
#[cfg(feature = "lang-rust")]
static RUST_QUERIES: LanguageQueries = LanguageQueries::new();

impl Language {
    /// Dispatch on file extension. Returns `None` for unsupported types — the
    /// caller decides whether that's a skip or an error.
    #[must_use]
    pub fn from_path(path: &Path) -> Option<Self> {
        let ext = path.extension()?.to_str()?;
        match ext {
            #[cfg(feature = "lang-ts")]
            "ts" | "mts" | "cts" => Some(Self::TypeScript),
            #[cfg(feature = "lang-ts")]
            "tsx" => Some(Self::Tsx),
            #[cfg(feature = "lang-js")]
            "js" | "mjs" | "cjs" => Some(Self::JavaScript),
            #[cfg(feature = "lang-js")]
            "jsx" => Some(Self::Jsx),
            #[cfg(feature = "lang-py")]
            "py" | "pyi" => Some(Self::Python),
            #[cfg(feature = "lang-go")]
            "go" => Some(Self::Go),
            #[cfg(feature = "lang-scala")]
            "scala" | "sc" => Some(Self::Scala),
            #[cfg(feature = "lang-rust")]
            "rs" => Some(Self::Rust),
            _ => None,
        }
    }

    /// Display name (stable; used in serialized output).
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            #[cfg(feature = "lang-ts")]
            Self::TypeScript => "typescript",
            #[cfg(feature = "lang-ts")]
            Self::Tsx => "tsx",
            #[cfg(feature = "lang-js")]
            Self::JavaScript => "javascript",
            #[cfg(feature = "lang-js")]
            Self::Jsx => "jsx",
            #[cfg(feature = "lang-py")]
            Self::Python => "python",
            #[cfg(feature = "lang-go")]
            Self::Go => "go",
            #[cfg(feature = "lang-scala")]
            Self::Scala => "scala",
            #[cfg(feature = "lang-rust")]
            Self::Rust => "rust",
        }
    }

    /// Whether this language has a class-aware LCOM implementation in
    /// the v0.2 tree-sitter-approx backend. Go has no class scope at
    /// all (methods attach to types via receivers); Scala's class /
    /// trait / object / case-class richness needs the LSP backend.
    /// Both lower no Findings and the observer skips parsing entirely.
    #[must_use]
    pub fn supports_lcom(self) -> bool {
        match self {
            #[cfg(feature = "lang-ts")]
            Self::TypeScript | Self::Tsx => true,
            #[cfg(feature = "lang-js")]
            Self::JavaScript | Self::Jsx => true,
            #[cfg(feature = "lang-py")]
            Self::Python => true,
            #[cfg(feature = "lang-go")]
            Self::Go => false,
            #[cfg(feature = "lang-scala")]
            Self::Scala => false,
            #[cfg(feature = "lang-rust")]
            Self::Rust => true,
        }
    }

    #[must_use]
    pub fn ts_language(self) -> TsLanguage {
        match self {
            #[cfg(feature = "lang-ts")]
            Self::TypeScript => LANGUAGE_TYPESCRIPT.into(),
            #[cfg(feature = "lang-ts")]
            Self::Tsx => LANGUAGE_TSX.into(),
            #[cfg(feature = "lang-js")]
            Self::JavaScript | Self::Jsx => tree_sitter_javascript::LANGUAGE.into(),
            #[cfg(feature = "lang-py")]
            Self::Python => tree_sitter_python::LANGUAGE.into(),
            #[cfg(feature = "lang-go")]
            Self::Go => tree_sitter_go::LANGUAGE.into(),
            #[cfg(feature = "lang-scala")]
            Self::Scala => tree_sitter_scala::LANGUAGE.into(),
            #[cfg(feature = "lang-rust")]
            Self::Rust => tree_sitter_rust::LANGUAGE.into(),
        }
    }

    #[must_use]
    pub fn functions_query(self) -> &'static CompiledQuery<FunctionCaptures> {
        self.cache().functions.get_or_init(|| {
            let lang = self.ts_language();
            let query = Query::new(&lang, self.functions_query_source())
                .expect("functions.scm must compile");
            let captures = FunctionCaptures {
                scope: capture_index(&query, "function.scope"),
            };
            CompiledQuery { query, captures }
        })
    }

    #[must_use]
    pub fn ccn_query(self) -> &'static CompiledQuery<CcnCaptures> {
        self.cache().ccn.get_or_init(|| {
            let lang = self.ts_language();
            let query = Query::new(&lang, self.ccn_query_source()).expect("ccn.scm must compile");
            let captures = CcnCaptures {
                point: capture_index(&query, "ccn.point"),
                binary: capture_index(&query, "ccn.binary"),
            };
            CompiledQuery { query, captures }
        })
    }

    #[must_use]
    pub fn cognitive_query(self) -> &'static CompiledQuery<CognitiveCaptures> {
        self.cache().cognitive.get_or_init(|| {
            let lang = self.ts_language();
            let query = Query::new(&lang, self.cognitive_query_source())
                .expect("cognitive.scm must compile");
            let captures = CognitiveCaptures {
                if_: capture_index(&query, "if"),
                else_: capture_index(&query, "else"),
                inc_and_nest: capture_index(&query, "inc_and_nest"),
                inc: capture_index(&query, "inc"),
                binary: capture_index(&query, "binary"),
            };
            CompiledQuery { query, captures }
        })
    }

    #[must_use]
    pub fn lcom_query(self) -> &'static CompiledQuery<LcomCaptures> {
        self.cache().lcom.get_or_init(|| {
            let lang = self.ts_language();
            let query = Query::new(&lang, self.lcom_query_source()).expect("lcom.scm must compile");
            let captures = LcomCaptures {
                class_scope: capture_index(&query, "class.scope"),
            };
            CompiledQuery { query, captures }
        })
    }

    fn cache(self) -> &'static LanguageQueries {
        match self {
            #[cfg(feature = "lang-ts")]
            Self::TypeScript => &TYPESCRIPT_QUERIES,
            #[cfg(feature = "lang-ts")]
            Self::Tsx => &TSX_QUERIES,
            #[cfg(feature = "lang-js")]
            Self::JavaScript | Self::Jsx => &JAVASCRIPT_QUERIES,
            #[cfg(feature = "lang-py")]
            Self::Python => &PYTHON_QUERIES,
            #[cfg(feature = "lang-go")]
            Self::Go => &GO_QUERIES,
            #[cfg(feature = "lang-scala")]
            Self::Scala => &SCALA_QUERIES,
            #[cfg(feature = "lang-rust")]
            Self::Rust => &RUST_QUERIES,
        }
    }

    fn functions_query_source(self) -> &'static str {
        match self {
            #[cfg(feature = "lang-ts")]
            Self::TypeScript | Self::Tsx => TYPESCRIPT_FUNCTIONS_QUERY,
            #[cfg(feature = "lang-js")]
            Self::JavaScript | Self::Jsx => JAVASCRIPT_FUNCTIONS_QUERY,
            #[cfg(feature = "lang-py")]
            Self::Python => PYTHON_FUNCTIONS_QUERY,
            #[cfg(feature = "lang-go")]
            Self::Go => GO_FUNCTIONS_QUERY,
            #[cfg(feature = "lang-scala")]
            Self::Scala => SCALA_FUNCTIONS_QUERY,
            #[cfg(feature = "lang-rust")]
            Self::Rust => RUST_FUNCTIONS_QUERY,
        }
    }

    fn ccn_query_source(self) -> &'static str {
        match self {
            #[cfg(feature = "lang-ts")]
            Self::TypeScript | Self::Tsx => TYPESCRIPT_CCN_QUERY,
            #[cfg(feature = "lang-js")]
            Self::JavaScript | Self::Jsx => JAVASCRIPT_CCN_QUERY,
            #[cfg(feature = "lang-py")]
            Self::Python => PYTHON_CCN_QUERY,
            #[cfg(feature = "lang-go")]
            Self::Go => GO_CCN_QUERY,
            #[cfg(feature = "lang-scala")]
            Self::Scala => SCALA_CCN_QUERY,
            #[cfg(feature = "lang-rust")]
            Self::Rust => RUST_CCN_QUERY,
        }
    }

    fn cognitive_query_source(self) -> &'static str {
        match self {
            #[cfg(feature = "lang-ts")]
            Self::TypeScript | Self::Tsx => TYPESCRIPT_COGNITIVE_QUERY,
            #[cfg(feature = "lang-js")]
            Self::JavaScript | Self::Jsx => JAVASCRIPT_COGNITIVE_QUERY,
            #[cfg(feature = "lang-py")]
            Self::Python => PYTHON_COGNITIVE_QUERY,
            #[cfg(feature = "lang-go")]
            Self::Go => GO_COGNITIVE_QUERY,
            #[cfg(feature = "lang-scala")]
            Self::Scala => SCALA_COGNITIVE_QUERY,
            #[cfg(feature = "lang-rust")]
            Self::Rust => RUST_COGNITIVE_QUERY,
        }
    }

    fn lcom_query_source(self) -> &'static str {
        match self {
            #[cfg(feature = "lang-ts")]
            Self::TypeScript | Self::Tsx => TYPESCRIPT_LCOM_QUERY,
            #[cfg(feature = "lang-js")]
            Self::JavaScript | Self::Jsx => JAVASCRIPT_LCOM_QUERY,
            #[cfg(feature = "lang-py")]
            Self::Python => PYTHON_LCOM_QUERY,
            #[cfg(feature = "lang-go")]
            Self::Go => GO_LCOM_QUERY,
            #[cfg(feature = "lang-scala")]
            Self::Scala => SCALA_LCOM_QUERY,
            #[cfg(feature = "lang-rust")]
            Self::Rust => RUST_LCOM_QUERY,
        }
    }
}

fn capture_index(query: &Query, name: &str) -> u32 {
    query
        .capture_index_for_name(name)
        .unwrap_or_else(|| panic!("query missing @{name} capture"))
}

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

    #[cfg(feature = "lang-ts")]
    #[test]
    fn dispatches_typescript_extensions() {
        assert_eq!(
            Language::from_path(&PathBuf::from("foo.ts")),
            Some(Language::TypeScript)
        );
        assert_eq!(
            Language::from_path(&PathBuf::from("nested/dir/foo.mts")),
            Some(Language::TypeScript)
        );
        assert_eq!(
            Language::from_path(&PathBuf::from("foo.cts")),
            Some(Language::TypeScript)
        );
    }

    #[cfg(feature = "lang-ts")]
    #[test]
    fn dispatches_tsx_extension() {
        assert_eq!(
            Language::from_path(&PathBuf::from("Component.tsx")),
            Some(Language::Tsx)
        );
    }

    #[cfg(feature = "lang-rust")]
    #[test]
    fn dispatches_rust_extension() {
        assert_eq!(
            Language::from_path(&PathBuf::from("crates/core/src/lib.rs")),
            Some(Language::Rust)
        );
    }

    #[test]
    fn rejects_unsupported_extensions() {
        assert_eq!(Language::from_path(&PathBuf::from("README.md")), None);
        assert_eq!(Language::from_path(&PathBuf::from("Cargo.toml")), None);
    }

    #[cfg(feature = "lang-js")]
    #[test]
    fn dispatches_javascript_extensions() {
        assert_eq!(
            Language::from_path(&PathBuf::from("src/foo.js")),
            Some(Language::JavaScript)
        );
        assert_eq!(
            Language::from_path(&PathBuf::from("src/foo.mjs")),
            Some(Language::JavaScript)
        );
        assert_eq!(
            Language::from_path(&PathBuf::from("src/foo.cjs")),
            Some(Language::JavaScript)
        );
        assert_eq!(
            Language::from_path(&PathBuf::from("src/foo.jsx")),
            Some(Language::Jsx)
        );
    }

    #[test]
    fn rejects_extensionless_paths() {
        assert_eq!(Language::from_path(&PathBuf::from("Makefile")), None);
        assert_eq!(Language::from_path(&PathBuf::from("")), None);
    }

    #[cfg(all(feature = "lang-ts", feature = "lang-rust"))]
    #[test]
    fn loads_grammars() {
        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&Language::TypeScript.ts_language())
            .expect("typescript grammar loads");
        parser
            .set_language(&Language::Tsx.ts_language())
            .expect("tsx grammar loads");
        parser
            .set_language(&Language::Rust.ts_language())
            .expect("rust grammar loads");
    }

    #[cfg(all(feature = "lang-ts", feature = "lang-rust"))]
    #[test]
    fn cached_queries_compile_and_index() {
        for lang in [Language::TypeScript, Language::Tsx, Language::Rust] {
            let f = lang.functions_query();
            assert!(f.query.pattern_count() > 0);
            let _ = f.captures.scope;

            let c = lang.ccn_query();
            assert!(c.query.pattern_count() > 0);
            let _ = (c.captures.point, c.captures.binary);

            let g = lang.cognitive_query();
            assert!(g.query.pattern_count() > 0);
            let _ = (
                g.captures.if_,
                g.captures.else_,
                g.captures.inc_and_nest,
                g.captures.inc,
                g.captures.binary,
            );
        }
    }

    #[cfg(feature = "lang-js")]
    #[test]
    fn javascript_queries_compile_and_grammar_loads() {
        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&Language::JavaScript.ts_language())
            .expect("javascript grammar loads");
        parser
            .set_language(&Language::Jsx.ts_language())
            .expect("jsx grammar loads");
        for lang in [Language::JavaScript, Language::Jsx] {
            assert!(lang.functions_query().query.pattern_count() > 0);
            assert!(lang.ccn_query().query.pattern_count() > 0);
            assert!(lang.cognitive_query().query.pattern_count() > 0);
            assert!(lang.lcom_query().query.pattern_count() > 0);
        }
    }

    #[cfg(feature = "lang-py")]
    #[test]
    fn dispatches_python_extensions() {
        assert_eq!(
            Language::from_path(&PathBuf::from("src/foo.py")),
            Some(Language::Python)
        );
        assert_eq!(
            Language::from_path(&PathBuf::from("stubs/foo.pyi")),
            Some(Language::Python)
        );
    }

    #[cfg(feature = "lang-py")]
    #[test]
    fn python_queries_compile_and_grammar_loads() {
        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&Language::Python.ts_language())
            .expect("python grammar loads");
        assert!(Language::Python.functions_query().query.pattern_count() > 0);
        assert!(Language::Python.ccn_query().query.pattern_count() > 0);
        assert!(Language::Python.cognitive_query().query.pattern_count() > 0);
        assert!(Language::Python.lcom_query().query.pattern_count() > 0);
    }

    #[cfg(feature = "lang-go")]
    #[test]
    fn dispatches_go_extension() {
        assert_eq!(
            Language::from_path(&PathBuf::from("cmd/main.go")),
            Some(Language::Go)
        );
    }

    #[cfg(feature = "lang-go")]
    #[test]
    fn go_queries_compile_and_grammar_loads() {
        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&Language::Go.ts_language())
            .expect("go grammar loads");
        assert!(Language::Go.functions_query().query.pattern_count() > 0);
        assert!(Language::Go.ccn_query().query.pattern_count() > 0);
        assert!(Language::Go.cognitive_query().query.pattern_count() > 0);
        assert!(Language::Go.lcom_query().query.pattern_count() > 0);
    }

    #[cfg(feature = "lang-scala")]
    #[test]
    fn dispatches_scala_extensions() {
        assert_eq!(
            Language::from_path(&PathBuf::from("src/main/scala/Foo.scala")),
            Some(Language::Scala)
        );
        assert_eq!(
            Language::from_path(&PathBuf::from("script.sc")),
            Some(Language::Scala)
        );
    }

    #[cfg(feature = "lang-scala")]
    #[test]
    fn scala_queries_compile_and_grammar_loads() {
        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&Language::Scala.ts_language())
            .expect("scala grammar loads");
        assert!(Language::Scala.functions_query().query.pattern_count() > 0);
        assert!(Language::Scala.ccn_query().query.pattern_count() > 0);
        assert!(Language::Scala.cognitive_query().query.pattern_count() > 0);
        assert!(Language::Scala.lcom_query().query.pattern_count() > 0);
    }
}