big-code-analysis 2.1.0

Tool to compute and export code metrics
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
// Per-language metric and AST modules deliberately consume the macro-
// generated tree-sitter token enums via `use crate::*` and `use Foo::*`
// inside match expressions — explicit imports would list dozens of
// variants per arm and obscure the per-language token sets that are the
// point of these files. Allowed at the module level rather than per
// function so the per-language impl blocks stay readable.
#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
// Metric counts (token, function, branch, argument, etc.) are stored as
// `usize` and crossed with `f64` averages, ratios, and Halstead scores
// across the cyclomatic / MI / Halstead computations. The `usize as f64`
// and `f64 as usize` casts are intentional and snapshot-anchored — every
// site is bounded by the count it came from. Allowing the lints at the
// module level keeps the metric arithmetic legible.
#![allow(
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss
)]

use std::fmt;

use crate::checker::Checker;
use crate::macros::implement_metric_trait;

use crate::*;

/// The `Tokens` metric: per-function and per-file count of tree-sitter
/// leaf tokens, excluding any leaf that is itself a comment or has a
/// comment among its ancestors. Both halves matter: most grammars emit
/// comments as bare leaves, while some (Rust doc comments, Groovy
/// groovydoc, JSX `html_comment`) give them structured children whose
/// own leaves are not comment kinds.
///
/// This is a token-based size proxy: it counts the lexer's tokens
/// (identifiers, literals, keywords, punctuation) rather than lines or
/// Halstead operators/operands. Punctuation that Halstead skips
/// (parentheses, semicolons, separators) does contribute, so
/// `tokens` ≠ Halstead `N1 + N2`.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct Stats {
    tokens: usize,
    tokens_sum: usize,
    tokens_min: usize,
    tokens_max: usize,
    space_count: usize,
}

impl Default for Stats {
    fn default() -> Self {
        Self {
            tokens: 0,
            tokens_sum: 0,
            tokens_min: usize::MAX,
            tokens_max: 0,
            space_count: 1,
        }
    }
}

impl fmt::Display for Stats {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "tokens: {}, \
             tokens_average: {}, \
             tokens_min: {}, \
             tokens_max: {}",
            self.tokens_sum(),
            self.tokens_average(),
            self.tokens_min(),
            self.tokens_max(),
        )
    }
}

impl Stats {
    /// Merges a second `Tokens` metric suite into the first one.
    pub fn merge(&mut self, other: &Stats) {
        self.tokens_min = self.tokens_min.min(other.tokens_min);
        self.tokens_max = self.tokens_max.max(other.tokens_max);
        self.tokens_sum += other.tokens_sum;
        self.space_count += other.space_count;
    }

    /// Returns the total token count across all merged spaces.
    #[inline]
    #[must_use]
    pub fn tokens_sum(&self) -> u64 {
        self.tokens_sum as u64
    }

    /// Returns the average tokens per space.
    #[inline]
    #[must_use]
    pub fn tokens_average(&self) -> f64 {
        crate::metrics::average(self.tokens_sum() as f64, self.space_count)
    }

    /// Returns the smallest single-space token count.
    ///
    /// Diverges intentionally from `nom::Stats::functions_min`, which
    /// surfaces the raw `usize::MAX` sentinel for a never-observed
    /// space. We collapse the sentinel to `0` so a `Stats::default()`
    /// that bypasses the metric pipeline serializes to a meaningful
    /// number rather than `18446744073709551615`.
    #[inline]
    #[must_use]
    pub fn tokens_min(&self) -> u64 {
        if self.tokens_min == usize::MAX {
            0
        } else {
            self.tokens_min as u64
        }
    }

    /// Returns the largest single-space token count.
    #[inline]
    #[must_use]
    pub fn tokens_max(&self) -> u64 {
        self.tokens_max as u64
    }

    #[inline]
    pub(crate) fn compute_sum(&mut self) {
        self.tokens_sum += self.tokens;
    }

    #[inline]
    pub(crate) fn compute_minmax(&mut self) {
        self.tokens_min = self.tokens_min.min(self.tokens);
        self.tokens_max = self.tokens_max.max(self.tokens);
        self.compute_sum();
    }
}

#[doc(hidden)]
/// Per-language counting of tokens.
pub(crate) trait Tokens
where
    Self: Checker,
{
    /// Walk `node` and update `stats` with this metric for the language
    /// implementing the trait.
    ///
    /// `in_comment` is true when the node itself or any ancestor is a
    /// comment, so grammars whose comments have internal structure (e.g.
    /// Rust doc comments split into markers and content) exclude their
    /// inner leaves too. The walker propagates it down the traversal
    /// rather than having this function rediscover it per leaf: the old
    /// ancestor walk was `O(depth)` per leaf over an `O(depth)`
    /// `Node::parent`, which made the metric `O(leaves × depth²)` and let
    /// a few kilobytes of nested source burn minutes of CPU (#1052).
    fn compute(node: &Node, stats: &mut Stats, in_comment: bool) {
        if in_comment || node.child_count() != 0 {
            return;
        }
        stats.tokens += 1;
    }
}

implement_metric_trait!(
    [Tokens],
    PythonCode,
    MozjsCode,
    JavascriptCode,
    TypescriptCode,
    TsxCode,
    CppCode,
    MozcppCode,
    CCode,
    ObjcCode,
    RustCode,
    PreprocCode,
    CcommentCode,
    JavaCode,
    KotlinCode,
    GoCode,
    PerlCode,
    BashCode,
    LuaCode,
    TclCode,
    PhpCode,
    CsharpCode,
    ElixirCode,
    RubyCode,
    GroovyCode,
    IrulesCode
);

#[cfg(test)]
#[allow(
    clippy::float_cmp,
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::similar_names,
    clippy::doc_markdown,
    clippy::needless_raw_string_hashes,
    clippy::too_many_lines
)]
mod tests {
    use crate::test_support::{check_metrics_only_shim, metrics_verbatim};

    use super::*;

    check_metrics_only_shim!(check_metrics, Tokens);
    // `*_tokens_distinct_from_halstead` compares `tokens_sum()` against
    // Halstead's `N1 + N2`. Deselecting Halstead leaves that side at 0,
    // where `tokens_sum() > 0` holds for the wrong reason — so these two
    // ask for Halstead rather than passing vacuously.
    check_metrics_only_shim!(check_tokens_and_halstead, Tokens, Halstead);

    /// `def foo(x): return x` → leaves: `def`, `foo`, `(`, `x`, `)`,
    /// `:`, `return`, `x` = 8 tokens, hand-counted.
    #[test]
    fn python_tokens_exact_count() {
        check_metrics::<PythonParser>("def foo(x): return x", "foo.py", |metric| {
            assert_eq!(metric.tokens.tokens_sum(), 8);
            assert!(metric.tokens.tokens_max() >= 7);
        });
    }

    /// Adding a Python comment must not change the token count.
    #[test]
    fn python_tokens_comments_excluded() {
        check_metrics::<PythonParser>(
            "def foo(x): return x  # explanation\n# header\n",
            "foo.py",
            |metric| {
                assert_eq!(metric.tokens.tokens_sum(), 8);
            },
        );
    }

    /// Blank lines and indentation must not change the token count.
    #[test]
    fn python_tokens_whitespace_excluded() {
        check_metrics::<PythonParser>(
            "\n\n    def foo(x):\n        return x\n\n",
            "foo.py",
            |metric| {
                assert_eq!(metric.tokens.tokens_sum(), 8);
            },
        );
    }

    /// Tokens must exceed Halstead `N1 + N2` for code containing
    /// punctuation Halstead skips. Guards against accidental Halstead
    /// reuse.
    #[test]
    fn python_tokens_distinct_from_halstead() {
        check_tokens_and_halstead::<PythonParser>(
            "def foo(x): return (x + 1)",
            "foo.py",
            |metric| {
                let halstead_total =
                    metric.halstead.total_operators() + metric.halstead.total_operands();
                assert!(
                    metric.tokens.tokens_sum() > halstead_total,
                    "expected tokens ({}) > halstead N1+N2 ({}); punctuation \
                     like `(`, `)`, `:` should contribute to tokens but not Halstead",
                    metric.tokens.tokens_sum(),
                    halstead_total,
                );
            },
        );
    }

    /// Inner functions get attributed to their innermost scope. For
    /// `def outer(): def inner(): return 1`, the inner scope owns
    /// `def, inner, (, ), :, return, 1` = 7 tokens; the outer scope
    /// owns `def, outer, (, ), :` = 5; the unit owns 0 directly.
    /// Asserting the exact `tokens_max` is what catches an attribution
    /// regression — a broken implementation that credited all 12
    /// tokens to one scope would still pass `max <= sum`.
    #[test]
    fn python_tokens_nested_attribution() {
        check_metrics::<PythonParser>(
            "def outer():\n    def inner():\n        return 1\n",
            "foo.py",
            |metric| {
                assert_eq!(metric.tokens.tokens_sum(), 12);
                assert_eq!(metric.tokens.tokens_max(), 7);
                assert_eq!(metric.tokens.tokens_min(), 0);
            },
        );
    }

    /// C++ `/* … */` block comments must not contribute.
    /// Same fixture with and without comment yields the same count.
    #[test]
    fn cpp_tokens_block_comments_excluded() {
        check_metrics::<CppParser>(
            "int foo(int x) { /* multi\n   line */ return x; }",
            "foo.cpp",
            |m| {
                // Leaves outside the comment:
                // int, foo, (, int, x, ), {, return, x, ;, } = 11.
                assert_eq!(m.tokens.tokens_sum(), 11);
            },
        );
        check_metrics::<CppParser>("int foo(int x) { return x; }", "foo.cpp", |m| {
            assert_eq!(m.tokens.tokens_sum(), 11);
        });
    }

    /// C++ `// …` line comments must not contribute, matching the Python
    /// hand-counted style.  Leaves outside the comment:
    /// `int`, `x`, `=`, `1`, `;` = 5.
    #[test]
    fn cpp_tokens_line_comments_excluded() {
        check_metrics::<CppParser>("int x = 1; // a one-line comment\n", "foo.cpp", |m| {
            assert_eq!(m.tokens.tokens_sum(), 5);
        });
        check_metrics::<CppParser>("int x = 1;\n", "foo.cpp", |m| {
            assert_eq!(m.tokens.tokens_sum(), 5);
        });
    }

    /// Whitespace and blank lines must not contribute to the token count
    /// (mirrors `python_tokens_whitespace_excluded`).
    #[test]
    fn cpp_tokens_whitespace_excluded() {
        check_metrics::<CppParser>("\n\nint foo(int x) {\n    return x;\n}\n", "foo.cpp", |m| {
            // int, foo, (, int, x, ), {, return, x, ;, } = 11.
            assert_eq!(m.tokens.tokens_sum(), 11);
        });
    }

    /// Tokens count punctuation that Halstead skips (parentheses, braces,
    /// semicolons), so `tokens_sum` must exceed `N1 + N2` for a fixture
    /// with significant punctuation.  Mirrors
    /// `python_tokens_distinct_from_halstead`.
    #[test]
    fn cpp_tokens_distinct_from_halstead() {
        check_tokens_and_halstead::<CppParser>(
            "int foo(int x) { return (x + 1); }",
            "foo.cpp",
            |m| {
                let halstead_total = m.halstead.total_operators() + m.halstead.total_operands();
                assert!(
                    m.tokens.tokens_sum() > halstead_total,
                    "expected tokens ({}) > halstead N1+N2 ({}); punctuation like \
                     `(`, `)`, `{{`, `}}` and `;` should contribute to tokens but not Halstead",
                    m.tokens.tokens_sum(),
                    halstead_total,
                );
            },
        );
    }

    /// A C++ struct method and a free function each open their own
    /// `FuncSpace`, so the leaves of
    /// `struct S { int method(int a) { return a + 1; } }; int outer() { return 2; }`
    /// split across scopes: `method` owns 13 and `outer` owns 9, with the
    /// `struct`/unit framing owning the rest of the 27-token total.
    /// Asserting the exact `tokens_max` (13 — the largest single scope,
    /// strictly below the sum of 27) is what catches an attribution
    /// regression: a broken implementation that credited every leaf to one
    /// scope would raise `tokens_max` to 27 while still passing
    /// `max <= sum`, mirroring the Python sibling's exact-max guard.
    #[test]
    fn cpp_tokens_nested_attribution() {
        check_metrics::<CppParser>(
            "struct S {\n    int method(int a) { return a + 1; }\n};\nint outer() { return 2; }\n",
            "foo.cpp",
            |m| {
                assert_eq!(m.tokens.tokens_sum(), 27);
                assert_eq!(m.tokens.tokens_max(), 13);
                assert_eq!(m.tokens.tokens_min(), 1);
            },
        );
    }

    /// Java `// …` line comments must not contribute.
    #[test]
    fn java_tokens_line_comments_excluded() {
        check_metrics::<JavaParser>(
            "class A { void foo() { // hi\n return; } }",
            "A.java",
            |m| {
                // class, A, {, void, foo, (, ), {, return, ;, }, } = 12.
                assert_eq!(m.tokens.tokens_sum(), 12);
            },
        );
        check_metrics::<JavaParser>("class A { void foo() { return; } }", "A.java", |m| {
            assert_eq!(m.tokens.tokens_sum(), 12);
        });
    }

    #[test]
    fn groovy_tokens_line_comments_excluded() {
        // Groovy mirror — `// …` line comments must not contribute.
        check_metrics::<GroovyParser>(
            "class A { void foo() { // hi\n return\n } }",
            "A.groovy",
            |m| {
                // class, A, {, void, foo, (, ), {, return, newline,
                // }, } = 11 tokens (Groovy's newline acts as the
                // statement terminator that Java spells `;`).
                assert_eq!(m.tokens.tokens_sum(), 11);
            },
        );
    }

    /// JS-family `<!-- -->` Annex-B `html_comment` leaves must not
    /// contribute tokens — they classify as comments now (#697). The
    /// count must match the comment-free source exactly.
    #[test]
    fn javascript_tokens_html_comment_excluded() {
        check_metrics::<JavascriptParser>("<!-- hi -->\nlet x = 1;\n", "foo.js", |m| {
            // let, x, =, 1, ; = 5.
            assert_eq!(m.tokens.tokens_sum(), 5);
        });
        check_metrics::<JavascriptParser>("let x = 1;\n", "foo.js", |m| {
            assert_eq!(m.tokens.tokens_sum(), 5);
        });
    }

    /// Groovy `/** … */` `groovydoc_comment` leaves must not contribute
    /// tokens (#697 — `is_comment` previously missed this kind even
    /// though `Loc` counted it).
    #[test]
    fn groovy_tokens_groovydoc_excluded() {
        check_metrics::<GroovyParser>(
            "/** doc */\nclass A { void f() { return\n } }\n",
            "A.groovy",
            |m| {
                // class, A, {, void, f, (, ), {, return, newline,
                // }, } = 11.
                assert_eq!(m.tokens.tokens_sum(), 11);
            },
        );
        check_metrics::<GroovyParser>("class A { void f() { return\n } }\n", "A.groovy", |m| {
            assert_eq!(m.tokens.tokens_sum(), 11);
        });
    }

    /// Total token count for `source`, analysed byte-for-byte.
    ///
    /// Restricted to `Tokens` so the deep-nesting case below measures
    /// this metric and not the rest of the walk. When it was written,
    /// `cognitive`'s own parent lookups (#1062) dominated that case and
    /// would have misattributed a regression; they are linear now, but
    /// isolating the metric under test is still what makes the reading
    /// mean something.
    fn tokens_of(source: &str) -> u64 {
        metrics_verbatim(
            crate::LANG::Rust,
            source.as_bytes(),
            crate::MetricsOptions::default().with_only(&[crate::Metric::Tokens]),
        )
        .tokens
        .tokens_sum()
    }

    fn nested_parens(depth: usize) -> String {
        format!(
            "fn f() -> i32 {{ {}1{} }}\n",
            "(".repeat(depth),
            ")".repeat(depth)
        )
    }

    /// The token count holds thousands of levels deep (#1052).
    ///
    /// The metric used to walk each leaf's ancestor chain to decide
    /// comment membership. `Node::parent` is itself `O(depth)`, so that
    /// cost `O(leaves × depth²)`: depth 1000 took ~19 s and depth 2000
    /// over two minutes. The walker now propagates the flag down the
    /// traversal in `O(1)` per node, and this runs in milliseconds.
    ///
    /// The count is asserted by formula rather than hand-counted: each
    /// added paren pair contributes exactly two leaves, so the delta
    /// between consecutive depths pins the metric without depending on
    /// how the grammar tokenises the surrounding function.
    ///
    /// Counts only. The pre-#1052 implementation produced these *same*
    /// counts, only quadratically, so this test cannot tell the two
    /// apart — that job belongs to the `tokens/nested-paren` probe in
    /// the benchmark harness (#1068), which fits an exponent across
    /// three depths: `cargo bench -p big-code-analysis-bench --bench
    /// scaling`. The wall-clock budget that used to sit here was
    /// calibrated to one machine and this suite also runs under `cargo
    /// llvm-cov` and on shared Windows / macOS runners; the equivalent
    /// assertion in `cognitive` produced false failures in four
    /// separate environments before it was retired.
    #[test]
    fn tokens_count_holds_at_depth() {
        let shallow = tokens_of(&nested_parens(1));
        assert_eq!(tokens_of(&nested_parens(2)), shallow + 2);
        assert_eq!(tokens_of(&nested_parens(2000)), shallow + 2 * 1999);
    }

    /// A comment deep in the tree still contributes nothing.
    ///
    /// Honest scope: the comment's *own* subtree is only one level deep
    /// (`line_comment` → marker / `doc_comment`), so the enclosing block
    /// nesting exercises the walker's inheritance rather than any extra
    /// level of comment-internal structure —
    /// `rust_tokens_doc_comments_excluded` already covers the latter at
    /// depth 1. What this adds is the anchored differential below: a
    /// count that would not survive an `in_comment` wired to a constant.
    #[test]
    fn rust_tokens_comment_excluded_at_depth() {
        let deep_block = format!(
            "fn f() {{ {}let x = 1;{} }}\n",
            "{ ".repeat(50),
            " }".repeat(50)
        );
        let with_doc = deep_block.replace("let x = 1;", "/// doc\nlet x = 1;");
        let baseline = tokens_of(&deep_block);
        // Anchor the differential: without this, an `in_comment` wired
        // to always-true would return 0 on both sides and pass.
        assert_eq!(
            baseline, 111,
            // 4 (`fn f ( )`) + 2 (outer braces) + 100 (50 nested brace
            // pairs) + 5 (`let x = 1 ;`).
            "expected 111 tokens for the comment-free baseline"
        );
        assert_eq!(
            tokens_of(&with_doc),
            baseline,
            "a doc comment 50 blocks deep must contribute no tokens, \
             including its structured inner leaves"
        );
    }

    /// Rust doc comments split into structured children whose leaves are
    /// not themselves comment kinds (`//`, `outer_doc_comment_marker`,
    /// `doc_comment`), so excluding only the comment node is not enough
    /// — every leaf beneath it must be filtered too.
    #[test]
    fn rust_tokens_doc_comments_excluded() {
        check_metrics::<RustParser>(
            "/// outer doc\n/// more doc\nfn f() { let x = 1; }",
            "foo.rs",
            |m| {
                // fn, f, (, ), {, let, x, =, 1, ;, } = 11.
                assert_eq!(m.tokens.tokens_sum(), 11);
            },
        );
        check_metrics::<RustParser>("fn f() { let x = 1; }", "foo.rs", |m| {
            assert_eq!(m.tokens.tokens_sum(), 11);
        });
    }

    // -- Per-language smoke tests --------------------------------------
    //
    // Lesson 1 (`docs/development/lessons_learned.md`): every supported
    // language must have a positive test that asserts non-zero tokens
    // on real source. Catches the silent-zero regression where a
    // metric is registered but never fires. `check_metrics` takes a
    // `fn` pointer so each test inlines its assertion directly.

    #[test]
    fn smoke_python() {
        check_metrics::<PythonParser>("x = 1\n", "foo.py", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_rust() {
        check_metrics::<RustParser>("fn f() { let x = 1; }", "foo.rs", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_cpp() {
        check_metrics::<CppParser>("int x = 1;", "foo.cpp", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_java() {
        check_metrics::<JavaParser>("class A { int x = 1; }", "A.java", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_csharp() {
        check_metrics::<CsharpParser>("class A { int X = 1; }", "A.cs", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_javascript() {
        check_metrics::<JavascriptParser>("let x = 1;", "foo.js", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_mozjs() {
        check_metrics::<MozjsParser>("let x = 1;", "foo.js", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_typescript() {
        check_metrics::<TypescriptParser>("const x: number = 1;", "foo.ts", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_tsx() {
        check_metrics::<TsxParser>("const x: number = 1;", "foo.tsx", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_go() {
        check_metrics::<GoParser>("package main\nfunc f() {}", "foo.go", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_kotlin() {
        check_metrics::<KotlinParser>("fun f(): Int = 1", "foo.kt", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_lua() {
        check_metrics::<LuaParser>("local x = 1", "foo.lua", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_bash() {
        check_metrics::<BashParser>("x=1", "foo.sh", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_tcl() {
        check_metrics::<TclParser>("set x 1", "foo.tcl", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_perl() {
        check_metrics::<PerlParser>("my $x = 1;", "foo.pl", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_php() {
        check_metrics::<PhpParser>("<?php $x = 1;", "foo.php", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_preproc() {
        check_metrics::<PreprocParser>("#define FOO 1\n", "foo.h", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_ccomment() {
        // Ccomment's grammar parses bare C source; non-comment text
        // produces non-comment leaves.
        check_metrics::<CcommentParser>("int x = 1;", "foo.c", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_c() {
        check_metrics::<CParser>("int x = 1;\n", "foo.c", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_objc() {
        check_metrics::<ObjcParser>("int x = 1;\n", "foo.m", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_elixir() {
        check_metrics::<ElixirParser>("defmodule Foo do\n  :ok\nend\n", "foo.ex", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_ruby() {
        check_metrics::<RubyParser>("def foo\n  a = 1\nend\n", "foo.rb", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }

    #[test]
    fn smoke_irules() {
        check_metrics::<IrulesParser>("when X {\n    set x 1\n}\n", "foo.irule", |m| {
            assert!(m.tokens.tokens_sum() > 0);
        });
    }
}