big-code-analysis 1.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
// 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)]

use crate::*;

#[doc(hidden)]
/// A trait to create a richer `AST` node for a programming language, mainly
/// thought to be sent on the network.
pub trait Alterator
where
    Self: Checker,
{
    /// Creates a new `AST` node containing the code associated to the node,
    /// its span, the grammar field name through which the parent reaches
    /// it (if any), and its children.
    ///
    /// This function can be overloaded according to the needs of each
    /// programming language.
    #[must_use]
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        Self::get_default(node, code, span, field_name, children)
    }

    /// Gets the code as text and the span associated to a node.
    #[must_use]
    fn get_text_span(node: &Node, code: &[u8], span: bool, text: bool) -> (String, Span) {
        let text = if text {
            // Source may contain non-UTF-8 byte strings (e.g. binary literals); replacement
            // characters are acceptable in the AST payload produced by dump functions.
            String::from_utf8_lossy(&code[node.start_byte()..node.end_byte()]).into_owned()
        } else {
            String::new()
        };
        if span {
            let (spos_row, spos_column) = node.start_position();
            let (epos_row, epos_column) = node.end_position();
            (
                text,
                Some((spos_row + 1, spos_column + 1, epos_row + 1, epos_column + 1)),
            )
        } else {
            (text, None)
        }
    }

    /// Gets a default `AST` node containing the code associated to the
    /// node, its span, its grammar field name (if any), and its
    /// children.
    #[must_use]
    fn get_default(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        let (text, span) = Self::get_text_span(node, code, span, node.child_count() == 0);
        AstNode::with_field_name(node.kind(), text, span, field_name, children)
    }

    /// Gets a new `AST` node if and only if the code is not a comment,
    /// otherwise [`None`] is returned.
    ///
    /// Parameter order mirrors [`Self::alterate`] and [`Self::get_default`]
    /// (the flags-before-data convention `span, comment, field_name,
    /// children`) so positional confusion between adjacent boolean
    /// toggles is harder to introduce on the next edit.
    #[must_use]
    fn get_ast_node(
        node: &Node,
        code: &[u8],
        span: bool,
        comment: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> Option<AstNode> {
        if comment && Self::is_comment(node) {
            None
        } else {
            Some(Self::alterate(node, code, span, field_name, children))
        }
    }
}

impl Alterator for PreprocCode {}

impl Alterator for CcommentCode {}

impl Alterator for CppCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        mut children: Vec<AstNode>,
    ) -> AstNode {
        match Cpp::from(node.kind_id()) {
            Cpp::StringLiteral | Cpp::CharLiteral => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            Cpp::PreprocDef | Cpp::PreprocFunctionDef | Cpp::PreprocCall => {
                if let Some(last) = children.last()
                    && last.r#type == "\n"
                {
                    children.pop();
                }
                Self::get_default(node, code, span, field_name, children)
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for PythonCode {}

impl Alterator for JavaCode {}
impl Alterator for KotlinCode {}

impl Alterator for CsharpCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        match Csharp::from(node.kind_id()) {
            Csharp::StringLiteral
            | Csharp::VerbatimStringLiteral
            | Csharp::RawStringLiteral
            | Csharp::InterpolatedStringExpression
            | Csharp::CharacterLiteral => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for GoCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        match Go::from(node.kind_id()) {
            Go::InterpretedStringLiteral | Go::RawStringLiteral | Go::RuneLiteral => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for LuaCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        match Lua::from(node.kind_id()) {
            Lua::String => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for MozjsCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        match Mozjs::from(node.kind_id()) {
            Mozjs::String | Mozjs::String2 => {
                // Template strings may have interpolation children;
                // stripping them here is intentional (by design).
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for JavascriptCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        match Javascript::from(node.kind_id()) {
            Javascript::String | Javascript::String2 => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for TypescriptCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        match Typescript::from(node.kind_id()) {
            Typescript::String | Typescript::String2 => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for TsxCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        match Tsx::from(node.kind_id()) {
            Tsx::String | Tsx::String2 | Tsx::String3 => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for RustCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        match Rust::from(node.kind_id()) {
            Rust::StringLiteral | Rust::CharLiteral => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for PerlCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        match Perl::from(node.kind_id()) {
            Perl::StringSingleQuoted
            | Perl::StringDoubleQuoted
            | Perl::StringQQuoted
            | Perl::StringQqQuoted
            | Perl::BacktickQuoted
            | Perl::CommandQxQuoted => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for BashCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        match Bash::from(node.kind_id()) {
            Bash::String | Bash::RawString | Bash::AnsiCString | Bash::TranslatedString => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for TclCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        match Tcl::from(node.kind_id()) {
            // Preserve string literals verbatim to avoid whitespace trimming.
            Tcl::QuotedWord | Tcl::BracedWord | Tcl::BracedWordSimple => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for PhpCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        match Php::from(node.kind_id()) {
            // `String`/`String2`/`String3` are all aliased kind_ids
            // that the enum maps to `"string"`; flatten every alias to
            // preserve source text and keep the alterator aligned with
            // `Checker::is_string` and `Getter::get_op_type` (#288,
            // same pattern as #119 for JS/TS).
            Php::String
            | Php::String2
            | Php::String3
            | Php::EncapsedString
            | Php::Heredoc
            | Php::Nowdoc
            | Php::ShellCommandExpression => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for RubyCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        // Preserve verbatim text for string-like literals (regular strings,
        // chained strings, heredocs, regexes, subshells, symbol arrays,
        // delimited/simple symbols, character literals). Interpolation
        // children are intentionally collapsed into the flat text payload.
        match Ruby::from(node.kind_id()) {
            Ruby::String
            | Ruby::ChainedString
            | Ruby::BareString
            | Ruby::Subshell
            | Ruby::Regex
            | Ruby::HeredocBody
            | Ruby::StringArray
            | Ruby::SymbolArray
            | Ruby::DelimitedSymbol
            | Ruby::SimpleSymbol
            | Ruby::Character => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for ElixirCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        // Preserve string-like literal text verbatim. `Charlist` (single-
        // quoted) and `Sigil` (`~r/.../`, `~w[...]`, etc.) are treated
        // alongside ordinary strings so report output never trims their
        // bodies.
        match Elixir::from(node.kind_id()) {
            Elixir::String | Elixir::Charlist | Elixir::Sigil => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

impl Alterator for GroovyCode {
    fn alterate(
        node: &Node,
        code: &[u8],
        span: bool,
        field_name: Option<&'static str>,
        children: Vec<AstNode>,
    ) -> AstNode {
        // Preserve string and GString fragment text verbatim so report
        // output never trims their bodies. The dekobon Groovy grammar
        // ships several `StringFragment*` aliases for the same rule
        // applied inside different string contexts (single/double-quoted,
        // triple-quoted, slashy, dollar-slashy); each one carries body
        // text we must not collapse.
        match Groovy::from(node.kind_id()) {
            Groovy::StringLiteral
            | Groovy::StringFragment
            | Groovy::StringFragment2
            | Groovy::StringFragment3
            | Groovy::StringFragment4
            | Groovy::StringFragment5 => {
                let (text, span) = Self::get_text_span(node, code, span, true);
                AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
            }
            _ => Self::get_default(node, code, span, field_name, children),
        }
    }
}

#[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 std::path::PathBuf;

    use crate::{CppCode, CppParser, ParserTrait};

    use super::*;

    #[test]
    fn get_text_span_non_utf8_uses_replacement_char() {
        // Regression: `String::from_utf8(...).unwrap()` panicked on non-UTF-8
        // source bytes (e.g. binary literals). Now uses from_utf8_lossy so the
        // resulting AstNode text contains U+FFFD rather than causing a crash.
        let code = b"char c = '\xff';";
        let path = PathBuf::from("test.c");
        let parser = CppParser::new(code.to_vec(), &path, None);
        let root = parser.get_root();
        let (text, _) = CppCode::get_text_span(&root, code, false, true);
        assert!(
            text.contains('\u{FFFD}'),
            "expected U+FFFD replacement char for non-UTF-8 source, got: {text:?}"
        );
    }

    /// Collects all AstNode entries whose type matches `target_kind`,
    /// recursively walking the tree.
    fn collect_nodes_by_kind<'a>(node: &'a AstNode, target_kind: &str, out: &mut Vec<&'a AstNode>) {
        if node.r#type == target_kind {
            out.push(node);
        }
        for child in &node.children {
            collect_nodes_by_kind(child, target_kind, out);
        }
    }

    /// Builds an AST from source code using the given parser type.
    fn build_ast<P: ParserTrait>(code: &[u8], filename: &str) -> AstNode {
        let path = PathBuf::from(filename);
        let parser = P::new(code.to_vec(), &path, None);
        let cfg = crate::AstCfg {
            id: String::new(),
            comment: false,
            span: false,
        };
        let resp = crate::AstCallback::call(cfg, &parser);
        resp.root.expect("parser should produce a root AST node")
    }

    /// Asserts that every `"string"` node in the AST is flattened:
    /// non-empty text value and no children.
    fn assert_strings_flattened(root: &AstNode) {
        let mut strings = Vec::new();
        collect_nodes_by_kind(root, "string", &mut strings);
        assert!(
            !strings.is_empty(),
            "expected at least one 'string' node in the AST"
        );
        for node in &strings {
            assert!(
                node.children.is_empty(),
                "string node should be flattened (no children), got {} children; value={:?}",
                node.children.len(),
                node.value,
            );
            assert!(
                !node.value.is_empty(),
                "flattened string node should have non-empty text value"
            );
        }
    }

    // Regression tests for #119: String2 (and String3) variants must be
    // flattened the same way as String. These exercises string literals in
    // multiple grammatical positions to cover aliased kind_ids.
    #[test]
    fn javascript_string_nodes_all_flattened() {
        // Strings in expression, property key, and import positions
        // exercise different grammar productions (String vs String2).
        let code = br#"
            const a = 'single';
            const b = "double";
            const obj = {"key": 1};
            import "module";
        "#;
        let root = build_ast::<crate::JavascriptParser>(code, "test.js");
        assert_strings_flattened(&root);
    }

    #[test]
    fn typescript_string_nodes_all_flattened() {
        let code = br#"
            const a: string = 'single';
            const b: string = "double";
            const obj: Record<string, number> = {"key": 1};
            import "module";
        "#;
        let root = build_ast::<crate::TypescriptParser>(code, "test.ts");
        assert_strings_flattened(&root);
    }

    #[test]
    fn tsx_string_nodes_all_flattened() {
        // TSX has String, String2, and String3 — exercise JSX attribute
        // strings and regular string expressions.
        let code = br#"
            const a = 'single';
            const b = "double";
            const el = <div className="cls">{"text"}</div>;
        "#;
        let root = build_ast::<crate::TsxParser>(code, "test.tsx");
        assert_strings_flattened(&root);
    }

    #[test]
    fn php_string_like_nodes_all_flattened() {
        // Regression: issue #288. PHP `string`, `encapsed_string`,
        // `heredoc`, `nowdoc`, and `shell_command_expression` (backtick
        // form) must all flatten through the same `Alterator` arm.
        // The wave-1 fix also added `String2`/`String3` enum aliases
        // to the arm for defensive parity with `Checker::is_string`;
        // the current `tree-sitter-php` grammar emits `String2` only
        // as the `string` type keyword (a terminal) and never emits
        // `String3` (a hidden supertype) as a concrete node, so the
        // arm change is a no-op vs `get_default` for those aliases —
        // but locking the structural contract here keeps the three
        // sites aligned if future grammar revisions surface either id
        // as an interior node.
        let code = br#"<?php
            $single = 'single';
            $double = "double";
            $cmd = `ls`;
            $here = <<<EOT
                some text
                EOT;
            $now = <<<'EOT'
                literal
                EOT;
        "#;
        let root = build_ast::<crate::PhpParser>(code, "test.php");
        assert_strings_flattened(&root);
        // ShellCommandExpression flattens with the same shape as
        // EncapsedString — confirm it preserves source text and has
        // no children.
        let mut shells = Vec::new();
        collect_nodes_by_kind(&root, "shell_command_expression", &mut shells);
        assert!(
            !shells.is_empty(),
            "expected at least one shell_command_expression node"
        );
        for node in &shells {
            assert!(
                node.children.is_empty(),
                "shell_command_expression should be flattened; got {} children",
                node.children.len()
            );
            assert!(
                node.value.contains("ls"),
                "flattened backtick literal should preserve text; got {:?}",
                node.value
            );
        }
    }

    #[test]
    fn groovy_string_literal_preserved_verbatim() {
        // Regression for the `impl Alterator for GroovyCode` arms:
        // the `StringLiteral` arm (and its `StringFragment*` aliases
        // for interpolated / slashy / dollar-slashy contexts) must
        // keep the literal's text intact.
        let code = br#"
            class A {
                String single = 'hello'
                String double = "world"
                char ch = 'x'
            }
        "#;
        let root = build_ast::<crate::GroovyParser>(code, "test.groovy");
        // The dekobon Groovy grammar consolidates every string shape
        // under one `string_literal` kind name (single-quoted,
        // double-quoted, triple-quoted, slashy `/.../`, dollar-slashy
        // `$/.../$`, GString-interpolated); there is no separate
        // `character_literal` kind. The defensive collect-by-name for
        // `character_literal` below is a drift marker — it is expected
        // to find nothing today but would surface a future grammar
        // bump that promoted character literals to a distinct kind.
        let mut strings = Vec::new();
        collect_nodes_by_kind(&root, "string_literal", &mut strings);
        collect_nodes_by_kind(&root, "character_literal", &mut strings);
        assert!(
            !strings.is_empty(),
            "expected at least one string/character literal in the AST"
        );
        for node in &strings {
            assert!(
                node.children.is_empty(),
                "string-like node should be flattened (no children); got {} children, value={:?}",
                node.children.len(),
                node.value,
            );
            assert!(
                !node.value.is_empty(),
                "flattened string-like node should have non-empty text value"
            );
        }
    }

    #[test]
    fn groovy_multiline_string_fragment_preserves_newlines() {
        // The `StringLiteral`/`MultilineStringFragment` arms route
        // through `get_text_span(..., true)` to keep the body text
        // verbatim. A regression that flips that boolean to `false`
        // would trim embedded whitespace — including the newlines
        // inside triple-quoted strings. The outer `StringLiteral`
        // node is flattened by the alterator and carries the entire
        // triple-quoted body as its value.
        let code = b"def s = \"\"\"first line\nsecond line\nthird line\"\"\"";
        let root = build_ast::<crate::GroovyParser>(code, "test.groovy");
        let mut strings = Vec::new();
        collect_nodes_by_kind(&root, "string_literal", &mut strings);
        assert!(
            !strings.is_empty(),
            "expected at least one string_literal node for the triple-quoted body"
        );
        let any_with_newline = strings.iter().any(|n| n.value.contains('\n'));
        assert!(
            any_with_newline,
            "expected the flattened string_literal to keep its embedded newlines; got values: {:?}",
            strings.iter().map(|n| &n.value).collect::<Vec<_>>()
        );
    }
}