Skip to main content

big_code_analysis_ast/
alterator.rs

1// Per-language metric and AST modules deliberately consume the macro-
2// generated tree-sitter token enums via `use crate::*` and `use Foo::*`
3// inside match expressions — explicit imports would list dozens of
4// variants per arm and obscure the per-language token sets that are the
5// point of these files. Allowed at the module level rather than per
6// function so the per-language impl blocks stay readable.
7#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
8
9//! Per-language AST simplification for the dump: the [`Alterator`] hook a
10//! language implements to reshape a node before it is rendered as an
11//! [`AstNode`].
12
13use crate::lang_helpers::irules::BRACED_WORD_KINDS as IRULES_BRACED_WORD_KINDS;
14use crate::lang_helpers::tcl::BRACED_WORD_KINDS as TCL_BRACED_WORD_KINDS;
15use crate::*;
16
17/// A trait to create a richer `AST` node for a programming language, mainly
18/// thought to be sent on the network. Crate-internal extension over
19/// [`Checker`], used only by the per-language `Parser<T>` impls.
20pub trait Alterator
21where
22    Self: Checker,
23{
24    /// Creates a new `AST` node containing the code associated to the node,
25    /// its span, the grammar field name through which the parent reaches
26    /// it (if any), and its children.
27    ///
28    /// This function can be overloaded according to the needs of each
29    /// programming language.
30    #[must_use]
31    fn alterate(
32        node: &Node,
33        code: &[u8],
34        span: bool,
35        field_name: Option<&'static str>,
36        children: Vec<AstNode>,
37    ) -> AstNode {
38        Self::get_default(node, code, span, field_name, children)
39    }
40
41    /// Gets the code as text and the span associated to a node.
42    #[must_use]
43    fn get_text_span(node: &Node, code: &[u8], span: bool, text: bool) -> (String, Option<Span>) {
44        let text = if text {
45            // Source may contain non-UTF-8 byte strings (e.g. binary literals); replacement
46            // characters are acceptable in the AST payload produced by dump functions.
47            String::from_utf8_lossy(&code[node.start_byte()..node.end_byte()]).into_owned()
48        } else {
49            String::new()
50        };
51        if span {
52            let (spos_row, spos_column) = node.start_position();
53            let (epos_row, epos_column) = node.end_position();
54            // Tree-sitter positions are 0-based; the dump shape reports
55            // 1-based rows and columns. Byte offsets stay 0-based and
56            // half-open, mirroring tree-sitter (#727).
57            (
58                text,
59                Some(Span::new(
60                    spos_row + 1,
61                    spos_column + 1,
62                    epos_row + 1,
63                    epos_column + 1,
64                    node.start_byte(),
65                    node.end_byte(),
66                )),
67            )
68        } else {
69            (text, None)
70        }
71    }
72
73    /// Gets a default `AST` node containing the code associated to the
74    /// node, its span, its grammar field name (if any), and its
75    /// children.
76    #[must_use]
77    fn get_default(
78        node: &Node,
79        code: &[u8],
80        span: bool,
81        field_name: Option<&'static str>,
82        children: Vec<AstNode>,
83    ) -> AstNode {
84        let (text, span) = Self::get_text_span(node, code, span, node.child_count() == 0);
85        AstNode::with_field_name(node.kind(), text, span, field_name, children)
86    }
87
88    /// Renders `node` as one leaf holding its whole source text, children
89    /// dropped: what every string literal — the `StringLit` row's
90    /// owners, and the character literals the dump treats alike — becomes
91    /// in the dump (#699), so a raw string's delimiter and
92    /// `string_content` children, a template's `${…}` substitutions and
93    /// a heredoc's fragments all collapse into the flat text payload.
94    #[must_use]
95    fn flatten_verbatim(
96        node: &Node,
97        code: &[u8],
98        span: bool,
99        field_name: Option<&'static str>,
100    ) -> AstNode {
101        let (text, span) = Self::get_text_span(node, code, span, true);
102        AstNode::with_field_name(node.kind(), text, span, field_name, Vec::new())
103    }
104
105    /// Whether `node` must keep its children even though
106    /// [`Self::alterate`] would flatten it into a verbatim leaf.
107    ///
108    /// The flattening arms are keyed on node *kind*, which is the right
109    /// question for a language whose string literals have a kind of
110    /// their own. The Tcl family is the exception: `braced_word` is both
111    /// the literal of `lappend x {a b}` and the *script* of a `proc`
112    /// body or an iRules `when` handler, so flattening by kind dropped
113    /// every script body from the dump — the whole body collapsed into
114    /// one leaf holding its text (#1381).
115    ///
116    /// It is a veto rather than an extra `alterate` arm so that
117    /// `alterate` keeps its byte-and-kind signature: only this question
118    /// needs the ancestor chain, and threading it through all twenty-odd
119    /// `alterate` impls to serve two of them would make every language
120    /// pay for one grammar's ambiguity. `ancestors` is the chain the
121    /// dump walk descended through, for [`Node::parent`]'s `O(depth)`
122    /// reason (#1084) — off an unknown chain this question took
123    /// `Ast::dump` on 8 KB of nested Tcl braces from 10 ms to 418 ms —
124    /// the run `ast.rs` records beside the chain this reads, not a
125    /// second measurement.
126    ///
127    /// That walk records no siblings, so an impl must not ask
128    /// `ancestors` for one: under a wide parent that trips the walk
129    /// assertion in a debug build (#1601).
130    #[inline]
131    #[must_use]
132    fn keeps_children<'a>(_node: &Node<'a>, _code: &[u8], _ancestors: Ancestors<'a, '_>) -> bool {
133        false
134    }
135
136    /// Gets a new `AST` node if and only if the code is not a comment,
137    /// otherwise [`None`] is returned.
138    ///
139    /// Parameter order mirrors [`Self::alterate`] and [`Self::get_default`]
140    /// (the flags-before-data convention `span, comment, field_name,
141    /// children`) so positional confusion between adjacent boolean
142    /// toggles is harder to introduce on the next edit. `ancestors` is
143    /// last, matching every other chain-taking predicate in the crate.
144    #[must_use]
145    fn get_ast_node<'a>(
146        node: &Node<'a>,
147        code: &[u8],
148        span: bool,
149        comment: bool,
150        field_name: Option<&'static str>,
151        children: Vec<AstNode>,
152        ancestors: Ancestors<'a, '_>,
153    ) -> Option<AstNode> {
154        if comment && Self::is_comment(node) {
155            None
156        } else if Self::keeps_children(node, code, ancestors) {
157            Some(Self::get_default(node, code, span, field_name, children))
158        } else {
159            Some(Self::alterate(node, code, span, field_name, children))
160        }
161    }
162}
163
164impl Alterator for PreprocCode {}
165
166impl Alterator for CcommentCode {}
167
168impl Alterator for CppCode {
169    fn alterate(
170        node: &Node,
171        code: &[u8],
172        span: bool,
173        field_name: Option<&'static str>,
174        mut children: Vec<AstNode>,
175    ) -> AstNode {
176        let kind = node.kind_id();
177        match Cpp::from(kind) {
178            // The `StringLit` row's literals (`roles/cpp.rs`, #1698) render as
179            // one verbatim leaf, so the dump agrees with `find string` (#699); a
180            // `char_literal` is flattened beside them and is deliberately not a
181            // string — the same split Rust and Go apply to their char / rune
182            // literals.
183            _ if crate::roles::cpp::string_lit::lists_kind(kind)
184                || matches!(kind.into(), Cpp::CharLiteral) =>
185            {
186                Self::flatten_verbatim(node, code, span, field_name)
187            }
188            Cpp::PreprocDef | Cpp::PreprocFunctionDef | Cpp::PreprocCall => {
189                if let Some(last) = children.last()
190                    && last.r#type == "\n"
191                {
192                    children.pop();
193                }
194                Self::get_default(node, code, span, field_name, children)
195            }
196            _ => Self::get_default(node, code, span, field_name, children),
197        }
198    }
199}
200
201impl Alterator for CCode {
202    fn alterate(
203        node: &Node,
204        code: &[u8],
205        span: bool,
206        field_name: Option<&'static str>,
207        mut children: Vec<AstNode>,
208    ) -> AstNode {
209        let kind = node.kind_id();
210        match C::from(kind) {
211            // The `StringLit` row's literals (`roles/c.rs`, #1698) render as
212            // one verbatim leaf, so the dump agrees with `find string` (#699);
213            // a `char_literal` is flattened beside them and is deliberately
214            // not a string — the same split Rust and Go apply to their char /
215            // rune literals.
216            _ if crate::roles::c::string_lit::lists_kind(kind)
217                || matches!(kind.into(), C::CharLiteral) =>
218            {
219                Self::flatten_verbatim(node, code, span, field_name)
220            }
221            C::PreprocDef | C::PreprocFunctionDef | C::PreprocCall => {
222                if let Some(last) = children.last()
223                    && last.r#type == "\n"
224                {
225                    children.pop();
226                }
227                Self::get_default(node, code, span, field_name, children)
228            }
229            _ => Self::get_default(node, code, span, field_name, children),
230        }
231    }
232}
233
234impl Alterator for ObjcCode {
235    fn alterate(
236        node: &Node,
237        code: &[u8],
238        span: bool,
239        field_name: Option<&'static str>,
240        mut children: Vec<AstNode>,
241    ) -> AstNode {
242        // ObjC is C plus message sends; the literal-flattening and
243        // preprocessor newline-trimming rules are identical to the C
244        // alterator. `@"…"` is one `string_literal` whose first child
245        // is the `@` token, so the `StringLiteral` arm flattens the
246        // whole literal, marker included, exactly as for C.
247        let kind = node.kind_id();
248        match Objc::from(kind) {
249            // The `StringLit` row's literals (`roles/objc.rs`, #1698) render as
250            // one verbatim leaf, so the dump agrees with `find string` (#699); a
251            // `char_literal` is flattened beside them and is deliberately not a
252            // string (#1316) — the same split Rust and Go apply to their char /
253            // rune literals; `@"…"` is one `string_literal` whose first child is
254            // the `@` token, flattened whole, marker included.
255            _ if crate::roles::objc::string_lit::lists_kind(kind)
256                || matches!(kind.into(), Objc::CharLiteral) =>
257            {
258                Self::flatten_verbatim(node, code, span, field_name)
259            }
260            Objc::PreprocDef | Objc::PreprocFunctionDef | Objc::PreprocCall => {
261                if let Some(last) = children.last()
262                    && last.r#type == "\n"
263                {
264                    children.pop();
265                }
266                Self::get_default(node, code, span, field_name, children)
267            }
268            _ => Self::get_default(node, code, span, field_name, children),
269        }
270    }
271}
272
273impl Alterator for MozcppCode {
274    fn alterate(
275        node: &Node,
276        code: &[u8],
277        span: bool,
278        field_name: Option<&'static str>,
279        mut children: Vec<AstNode>,
280    ) -> AstNode {
281        let kind = node.kind_id();
282        match Mozcpp::from(kind) {
283            // The `StringLit` row's literals (`roles/mozcpp.rs`, #1698) render
284            // as one verbatim leaf, so the dump agrees with `find string`
285            // (#699); a `char_literal` is flattened beside them and is
286            // deliberately not a string — the same split Rust and Go apply to
287            // their char / rune literals.
288            _ if crate::roles::mozcpp::string_lit::lists_kind(kind)
289                || matches!(kind.into(), Mozcpp::CharLiteral) =>
290            {
291                Self::flatten_verbatim(node, code, span, field_name)
292            }
293            Mozcpp::PreprocDef | Mozcpp::PreprocFunctionDef | Mozcpp::PreprocCall => {
294                if let Some(last) = children.last()
295                    && last.r#type == "\n"
296                {
297                    children.pop();
298                }
299                Self::get_default(node, code, span, field_name, children)
300            }
301            _ => Self::get_default(node, code, span, field_name, children),
302        }
303    }
304}
305
306impl Alterator for PythonCode {
307    fn alterate(
308        node: &Node,
309        code: &[u8],
310        span: bool,
311        field_name: Option<&'static str>,
312        children: Vec<AstNode>,
313    ) -> AstNode {
314        let kind = node.kind_id();
315        // The `StringLit` row's literals (`roles/python.rs`, #1698) render
316        // as one verbatim leaf, so the dump agrees with `find string`
317        // (#699).
318        if crate::roles::python::string_lit::lists_kind(kind) {
319            Self::flatten_verbatim(node, code, span, field_name)
320        } else {
321            Self::get_default(node, code, span, field_name, children)
322        }
323    }
324}
325
326impl Alterator for JavaCode {
327    fn alterate(
328        node: &Node,
329        code: &[u8],
330        span: bool,
331        field_name: Option<&'static str>,
332        children: Vec<AstNode>,
333    ) -> AstNode {
334        let kind = node.kind_id();
335        // The `StringLit` row's literals (`roles/java.rs`, #1698) render as
336        // one verbatim leaf, so the dump agrees with `find string` (#699).
337        if crate::roles::java::string_lit::lists_kind(kind) {
338            Self::flatten_verbatim(node, code, span, field_name)
339        } else {
340            Self::get_default(node, code, span, field_name, children)
341        }
342    }
343}
344
345impl Alterator for KotlinCode {
346    fn alterate(
347        node: &Node,
348        code: &[u8],
349        span: bool,
350        field_name: Option<&'static str>,
351        children: Vec<AstNode>,
352    ) -> AstNode {
353        let kind = node.kind_id();
354        // The `StringLit` row's literals (`roles/kotlin.rs`, #1698) render
355        // as one verbatim leaf, so the dump agrees with `find string`
356        // (#699).
357        if crate::roles::kotlin::string_lit::lists_kind(kind) {
358            Self::flatten_verbatim(node, code, span, field_name)
359        } else {
360            Self::get_default(node, code, span, field_name, children)
361        }
362    }
363}
364
365impl Alterator for CsharpCode {
366    fn alterate(
367        node: &Node,
368        code: &[u8],
369        span: bool,
370        field_name: Option<&'static str>,
371        children: Vec<AstNode>,
372    ) -> AstNode {
373        let kind = node.kind_id();
374        // The `StringLit` row's literals (`roles/csharp.rs`, #1698) render
375        // as one verbatim leaf, so the dump agrees with `find string`
376        // (#699); a `character_literal` is flattened beside them and is not
377        // a string.
378        if crate::roles::csharp::string_lit::lists_kind(kind)
379            || matches!(kind.into(), Csharp::CharacterLiteral)
380        {
381            Self::flatten_verbatim(node, code, span, field_name)
382        } else {
383            Self::get_default(node, code, span, field_name, children)
384        }
385    }
386}
387
388impl Alterator for GoCode {
389    fn alterate(
390        node: &Node,
391        code: &[u8],
392        span: bool,
393        field_name: Option<&'static str>,
394        children: Vec<AstNode>,
395    ) -> AstNode {
396        let kind = node.kind_id();
397        // The `StringLit` row's literals (`roles/go.rs`, #1698) render as
398        // one verbatim leaf, so the dump agrees with `find string` (#699); a
399        // `rune_literal` is flattened beside them and is not a string.
400        if crate::roles::go::string_lit::lists_kind(kind) || matches!(kind.into(), Go::RuneLiteral)
401        {
402            Self::flatten_verbatim(node, code, span, field_name)
403        } else {
404            Self::get_default(node, code, span, field_name, children)
405        }
406    }
407}
408
409impl Alterator for LuaCode {
410    fn alterate(
411        node: &Node,
412        code: &[u8],
413        span: bool,
414        field_name: Option<&'static str>,
415        children: Vec<AstNode>,
416    ) -> AstNode {
417        let kind = node.kind_id();
418        // The `StringLit` row's literals (`roles/lua.rs`, #1698) render as
419        // one verbatim leaf, so the dump agrees with `find string` (#699).
420        if crate::roles::lua::string_lit::lists_kind(kind) {
421            Self::flatten_verbatim(node, code, span, field_name)
422        } else {
423            Self::get_default(node, code, span, field_name, children)
424        }
425    }
426}
427
428impl Alterator for MozjsCode {
429    fn alterate(
430        node: &Node,
431        code: &[u8],
432        span: bool,
433        field_name: Option<&'static str>,
434        children: Vec<AstNode>,
435    ) -> AstNode {
436        let kind = node.kind_id();
437        // The `StringLit` row's literals (`roles/mozjs.rs`, #1698) render as
438        // one verbatim leaf, so the dump agrees with `find string` (#699).
439        if crate::roles::mozjs::string_lit::lists_kind(kind) {
440            Self::flatten_verbatim(node, code, span, field_name)
441        } else {
442            Self::get_default(node, code, span, field_name, children)
443        }
444    }
445}
446
447impl Alterator for JavascriptCode {
448    fn alterate(
449        node: &Node,
450        code: &[u8],
451        span: bool,
452        field_name: Option<&'static str>,
453        children: Vec<AstNode>,
454    ) -> AstNode {
455        let kind = node.kind_id();
456        // The `StringLit` row's literals (`roles/javascript.rs`, #1698)
457        // render as one verbatim leaf, so the dump agrees with `find string`
458        // (#699).
459        if crate::roles::javascript::string_lit::lists_kind(kind) {
460            Self::flatten_verbatim(node, code, span, field_name)
461        } else {
462            Self::get_default(node, code, span, field_name, children)
463        }
464    }
465}
466
467impl Alterator for TypescriptCode {
468    fn alterate(
469        node: &Node,
470        code: &[u8],
471        span: bool,
472        field_name: Option<&'static str>,
473        children: Vec<AstNode>,
474    ) -> AstNode {
475        let kind = node.kind_id();
476        // The `StringLit` row's literals (`roles/typescript.rs`, #1698)
477        // render as one verbatim leaf, so the dump agrees with `find string`
478        // (#699).
479        if crate::roles::typescript::string_lit::lists_kind(kind) {
480            Self::flatten_verbatim(node, code, span, field_name)
481        } else {
482            Self::get_default(node, code, span, field_name, children)
483        }
484    }
485}
486
487impl Alterator for TsxCode {
488    fn alterate(
489        node: &Node,
490        code: &[u8],
491        span: bool,
492        field_name: Option<&'static str>,
493        children: Vec<AstNode>,
494    ) -> AstNode {
495        let kind = node.kind_id();
496        // The `StringLit` row's literals (`roles/tsx.rs`, #1698) render as
497        // one verbatim leaf, so the dump agrees with `find string` (#699).
498        if crate::roles::tsx::string_lit::lists_kind(kind) {
499            Self::flatten_verbatim(node, code, span, field_name)
500        } else {
501            Self::get_default(node, code, span, field_name, children)
502        }
503    }
504}
505
506impl Alterator for RustCode {
507    fn alterate(
508        node: &Node,
509        code: &[u8],
510        span: bool,
511        field_name: Option<&'static str>,
512        children: Vec<AstNode>,
513    ) -> AstNode {
514        let kind = node.kind_id();
515        // The `StringLit` row's literals (`roles/rust.rs`, #1698) render as
516        // one verbatim leaf, so the dump agrees with `find string` (#699); a
517        // `char_literal` is flattened beside them and is not a string
518        // (#391).
519        if crate::roles::rust::string_lit::lists_kind(kind)
520            || matches!(kind.into(), Rust::CharLiteral)
521        {
522            Self::flatten_verbatim(node, code, span, field_name)
523        } else {
524            Self::get_default(node, code, span, field_name, children)
525        }
526    }
527}
528
529impl Alterator for PerlCode {
530    fn alterate(
531        node: &Node,
532        code: &[u8],
533        span: bool,
534        field_name: Option<&'static str>,
535        children: Vec<AstNode>,
536    ) -> AstNode {
537        let kind = node.kind_id();
538        // The `StringLit` row's literals (`roles/perl.rs`, #1698) render as
539        // one verbatim leaf, so the dump agrees with `find string` (#699).
540        if crate::roles::perl::string_lit::lists_kind(kind) {
541            Self::flatten_verbatim(node, code, span, field_name)
542        } else {
543            Self::get_default(node, code, span, field_name, children)
544        }
545    }
546}
547
548impl Alterator for BashCode {
549    fn alterate(
550        node: &Node,
551        code: &[u8],
552        span: bool,
553        field_name: Option<&'static str>,
554        children: Vec<AstNode>,
555    ) -> AstNode {
556        let kind = node.kind_id();
557        // The `StringLit` row's literals (`roles/bash.rs`, #1698) render as
558        // one verbatim leaf, so the dump agrees with `find string` (#699).
559        if crate::roles::bash::string_lit::lists_kind(kind) {
560            Self::flatten_verbatim(node, code, span, field_name)
561        } else {
562            Self::get_default(node, code, span, field_name, children)
563        }
564    }
565}
566
567impl Alterator for TclCode {
568    fn alterate(
569        node: &Node,
570        code: &[u8],
571        span: bool,
572        field_name: Option<&'static str>,
573        children: Vec<AstNode>,
574    ) -> AstNode {
575        let kind = node.kind_id();
576        // The `StringLit` row's literals (`roles/tcl.rs`, #1698) render as
577        // one verbatim leaf, so the dump agrees with `find string` (#699);
578        // the row gates `braced_word` on being a literal, which needs the
579        // chain `alterate` does not carry; this reads the kind table, gates
580        // aside, and `keeps_children` below vetoes the script bodies
581        // (#1381).
582        if crate::roles::tcl::string_lit::lists_kind(kind) {
583            Self::flatten_verbatim(node, code, span, field_name)
584        } else {
585            Self::get_default(node, code, span, field_name, children)
586        }
587    }
588
589    fn keeps_children<'a>(node: &Node<'a>, code: &[u8], ancestors: Ancestors<'a, '_>) -> bool {
590        <Self as Getter>::is_braced_script_word(node, code, ancestors, &TCL_BRACED_WORD_KINDS)
591    }
592}
593
594impl Alterator for IrulesCode {
595    fn alterate(
596        node: &Node,
597        code: &[u8],
598        span: bool,
599        field_name: Option<&'static str>,
600        children: Vec<AstNode>,
601    ) -> AstNode {
602        let kind = node.kind_id();
603        // The `StringLit` row's literals (`roles/irules.rs`, #1698) render
604        // as one verbatim leaf, so the dump agrees with `find string`
605        // (#699); the twin of the Tcl arm, `keeps_children` veto and all — a
606        // `when` handler's body is the whole of a typical iRules file
607        // (#1381).
608        if crate::roles::irules::string_lit::lists_kind(kind) {
609            Self::flatten_verbatim(node, code, span, field_name)
610        } else {
611            Self::get_default(node, code, span, field_name, children)
612        }
613    }
614
615    fn keeps_children<'a>(node: &Node<'a>, code: &[u8], ancestors: Ancestors<'a, '_>) -> bool {
616        <Self as Getter>::is_braced_script_word(node, code, ancestors, &IRULES_BRACED_WORD_KINDS)
617    }
618}
619
620impl Alterator for PhpCode {
621    fn alterate(
622        node: &Node,
623        code: &[u8],
624        span: bool,
625        field_name: Option<&'static str>,
626        children: Vec<AstNode>,
627    ) -> AstNode {
628        let kind = node.kind_id();
629        // The `StringLit` row's literals (`roles/php.rs`, #1698) render as
630        // one verbatim leaf, so the dump agrees with `find string` (#699);
631        // `String2`, the `string` *type* keyword the row `excludes`, stays
632        // here: this is a dump-shape contract, not an `is_string` mirror
633        // (#1474, #1293), the keyword is a childless leaf so listing it is a
634        // no-op versus `get_default` (pinned by the test below), and
635        // deleting it would be grammar-dispatch §6's narrow-by-deletion — a
636        // grammar revision that gave the alias children would then silently
637        // start splitting text this arm exists to keep whole.
638        if crate::roles::php::string_lit::lists_kind(kind) || matches!(kind.into(), Php::String2) {
639            Self::flatten_verbatim(node, code, span, field_name)
640        } else {
641            Self::get_default(node, code, span, field_name, children)
642        }
643    }
644}
645
646impl Alterator for RubyCode {
647    fn alterate(
648        node: &Node,
649        code: &[u8],
650        span: bool,
651        field_name: Option<&'static str>,
652        children: Vec<AstNode>,
653    ) -> AstNode {
654        // Preserve verbatim text for string-like literals (regular strings,
655        // chained strings, heredocs, regexes, subshells, symbol arrays,
656        // delimited/simple symbols, character literals). Interpolation
657        // children are intentionally collapsed into the flat text payload.
658        let kind = node.kind_id();
659        // The `StringLit` row's literals (`roles/ruby.rs`, #1698) render as
660        // one verbatim leaf, so the dump agrees with `find string` (#699).
661        if crate::roles::ruby::string_lit::lists_kind(kind) {
662            Self::flatten_verbatim(node, code, span, field_name)
663        } else {
664            Self::get_default(node, code, span, field_name, children)
665        }
666    }
667}
668
669impl Alterator for ElixirCode {
670    fn alterate(
671        node: &Node,
672        code: &[u8],
673        span: bool,
674        field_name: Option<&'static str>,
675        children: Vec<AstNode>,
676    ) -> AstNode {
677        // Preserve string-like literal text verbatim. `Charlist` (single-
678        // quoted) and `Sigil` (`~r/.../`, `~w[...]`, etc.) are treated
679        // alongside ordinary strings so report output never trims their
680        // bodies.
681        let kind = node.kind_id();
682        // The `StringLit` row's literals (`roles/elixir.rs`, #1698) render
683        // as one verbatim leaf, so the dump agrees with `find string`
684        // (#699).
685        if crate::roles::elixir::string_lit::lists_kind(kind) {
686            Self::flatten_verbatim(node, code, span, field_name)
687        } else {
688            Self::get_default(node, code, span, field_name, children)
689        }
690    }
691}
692
693impl Alterator for GroovyCode {
694    fn alterate(
695        node: &Node,
696        code: &[u8],
697        span: bool,
698        field_name: Option<&'static str>,
699        children: Vec<AstNode>,
700    ) -> AstNode {
701        // Preserve string and GString fragment text verbatim so report
702        // output never trims their bodies. The dekobon Groovy grammar
703        // ships several `StringFragment*` aliases for the same rule
704        // applied inside different string contexts (single/double-quoted,
705        // triple-quoted, slashy, dollar-slashy); each one carries body
706        // text we must not collapse.
707        let kind = node.kind_id();
708        // The `StringLit` row's literals (`roles/groovy.rs`, #1698) render
709        // as one verbatim leaf, so the dump agrees with `find string`
710        // (#699); the `string_fragment*` aliases — the same rule applied
711        // inside the single / double-quoted, triple-quoted, slashy and
712        // dollar-slashy contexts — carry body text the dump keeps verbatim
713        // too.
714        if crate::roles::groovy::string_lit::lists_kind(kind)
715            || matches!(
716                kind.into(),
717                Groovy::StringFragment
718                    | Groovy::StringFragment2
719                    | Groovy::StringFragment3
720                    | Groovy::StringFragment4
721                    | Groovy::StringFragment5
722            )
723        {
724            Self::flatten_verbatim(node, code, span, field_name)
725        } else {
726            Self::get_default(node, code, span, field_name, children)
727        }
728    }
729}
730
731#[cfg(test)]
732#[allow(
733    clippy::float_cmp,
734    clippy::cast_precision_loss,
735    clippy::cast_possible_truncation,
736    clippy::cast_sign_loss,
737    clippy::similar_names,
738    clippy::doc_markdown,
739    clippy::needless_raw_string_hashes,
740    clippy::too_many_lines
741)]
742mod tests {
743    use std::path::PathBuf;
744
745    use crate::{CppCode, CppParser, ParserTrait};
746
747    use super::*;
748
749    #[cfg(feature = "cpp")]
750    #[test]
751    fn get_text_span_non_utf8_uses_replacement_char() {
752        // Regression: `String::from_utf8(...).unwrap()` panicked on non-UTF-8
753        // source bytes (e.g. binary literals). Now uses from_utf8_lossy so the
754        // resulting AstNode text contains U+FFFD rather than causing a crash.
755        let code = b"char c = '\xff';";
756        let path = PathBuf::from("test.c");
757        let parser = CppParser::new(code.to_vec(), &path, None);
758        let root = parser.root();
759        let (text, _) = CppCode::get_text_span(&root, code, false, true);
760        assert!(
761            text.contains('\u{FFFD}'),
762            "expected U+FFFD replacement char for non-UTF-8 source, got: {text:?}"
763        );
764    }
765
766    /// Collects all AstNode entries whose type matches `target_kind`,
767    /// recursively walking the tree.
768    #[cfg(any(
769        feature = "bash",
770        feature = "c",
771        feature = "cpp",
772        feature = "go",
773        feature = "groovy",
774        feature = "java",
775        feature = "javascript",
776        feature = "kotlin",
777        feature = "mozjs",
778        feature = "perl",
779        feature = "php",
780        feature = "python",
781        feature = "rust",
782        feature = "typescript",
783    ))]
784    fn collect_nodes_by_kind<'a>(node: &'a AstNode, target_kind: &str, out: &mut Vec<&'a AstNode>) {
785        if node.r#type == target_kind {
786            out.push(node);
787        }
788        for child in &node.children {
789            collect_nodes_by_kind(child, target_kind, out);
790        }
791    }
792
793    /// Builds an AST from source code using the given parser type.
794    #[cfg(any(
795        feature = "bash",
796        feature = "c",
797        feature = "cpp",
798        feature = "go",
799        feature = "groovy",
800        feature = "java",
801        feature = "javascript",
802        feature = "kotlin",
803        feature = "mozjs",
804        feature = "perl",
805        feature = "php",
806        feature = "python",
807        feature = "rust",
808        feature = "typescript",
809    ))]
810    fn build_ast<P: ParserTrait>(code: &[u8], filename: &str) -> AstNode {
811        let path = PathBuf::from(filename);
812        let parser = P::new(code.to_vec(), &path, None);
813        let cfg = crate::AstCfg {
814            id: String::new(),
815            language: String::new(),
816            comment: false,
817            span: false,
818        };
819        let resp = crate::ast::dump_inner(&parser, cfg);
820        resp.root.expect("parser should produce a root AST node")
821    }
822
823    /// Asserts that every `"string"` node in the AST is flattened:
824    /// non-empty text value and no children.
825    #[cfg(any(feature = "javascript", feature = "php", feature = "typescript"))]
826    fn assert_strings_flattened(root: &AstNode) {
827        let mut strings = Vec::new();
828        collect_nodes_by_kind(root, "string", &mut strings);
829        assert!(
830            !strings.is_empty(),
831            "expected at least one 'string' node in the AST"
832        );
833        for node in &strings {
834            assert!(
835                node.children.is_empty(),
836                "string node should be flattened (no children), got {} children; value={:?}",
837                node.children.len(),
838                node.value,
839            );
840            assert!(
841                !node.value.is_empty(),
842                "flattened string node should have non-empty text value"
843            );
844        }
845    }
846
847    // Regression tests for #119: String2 (and String3) variants must be
848    // flattened the same way as String. These exercises string literals in
849    // multiple grammatical positions to cover aliased kind_ids.
850    #[cfg(feature = "javascript")]
851    #[test]
852    fn javascript_string_nodes_all_flattened() {
853        // Strings in expression, property key, and import positions
854        // exercise different grammar productions (String vs String2).
855        let code = br#"
856            const a = 'single';
857            const b = "double";
858            const obj = {"key": 1};
859            import "module";
860        "#;
861        let root = build_ast::<crate::JavascriptParser>(code, "test.js");
862        assert_strings_flattened(&root);
863    }
864
865    #[cfg(feature = "typescript")]
866    #[test]
867    fn typescript_string_nodes_all_flattened() {
868        let code = br#"
869            const a: string = 'single';
870            const b: string = "double";
871            const obj: Record<string, number> = {"key": 1};
872            import "module";
873        "#;
874        let root = build_ast::<crate::TypescriptParser>(code, "test.ts");
875        assert_strings_flattened(&root);
876    }
877
878    #[cfg(feature = "typescript")]
879    #[test]
880    fn tsx_string_nodes_all_flattened() {
881        // TSX has String, String2, and String3 — exercise JSX attribute
882        // strings and regular string expressions.
883        let code = br#"
884            const a = 'single';
885            const b = "double";
886            const el = <div className="cls">{"text"}</div>;
887        "#;
888        let root = build_ast::<crate::TsxParser>(code, "test.tsx");
889        assert_strings_flattened(&root);
890    }
891
892    #[cfg(feature = "php")]
893    #[test]
894    fn php_string_like_nodes_all_flattened() {
895        // Regression: issue #288. PHP `string`, `encapsed_string`,
896        // `heredoc`, `nowdoc`, and `shell_command_expression` (backtick
897        // form) must all flatten through the same `Alterator` arm.
898        // The wave-1 fix also added the `String2`/`String3` enum
899        // aliases to the arm. The current `tree-sitter-php` grammar
900        // emits `String2` only as the `string` type keyword (a
901        // childless terminal, and not a string to `Checker::is_string`
902        // since #1474) and never emits `String3` (a hidden supertype)
903        // as a concrete node, so the arm is a no-op vs `get_default`
904        // for both aliases — but locking the structural contract here
905        // keeps the dump's text preserved if a future grammar revision
906        // surfaces either id as an interior node.
907        let code = br#"<?php
908            $single = 'single';
909            $double = "double";
910            $cmd = `ls`;
911            $here = <<<EOT
912                some text
913                EOT;
914            $now = <<<'EOT'
915                literal
916                EOT;
917        "#;
918        let root = build_ast::<crate::PhpParser>(code, "test.php");
919        assert_strings_flattened(&root);
920        // ShellCommandExpression flattens with the same shape as
921        // EncapsedString — confirm it preserves source text and has
922        // no children.
923        let mut shells = Vec::new();
924        collect_nodes_by_kind(&root, "shell_command_expression", &mut shells);
925        assert!(
926            !shells.is_empty(),
927            "expected at least one shell_command_expression node"
928        );
929        for node in &shells {
930            assert!(
931                node.children.is_empty(),
932                "shell_command_expression should be flattened; got {} children",
933                node.children.len()
934            );
935            assert!(
936                node.value.contains("ls"),
937                "flattened backtick literal should preserve text; got {:?}",
938                node.value
939            );
940        }
941    }
942
943    #[cfg(feature = "groovy")]
944    #[test]
945    fn groovy_string_literal_preserved_verbatim() {
946        // Regression for the `impl Alterator for GroovyCode` arms:
947        // the `StringLiteral` arm (and its `StringFragment*` aliases
948        // for interpolated / slashy / dollar-slashy contexts) must
949        // keep the literal's text intact.
950        let code = br#"
951            class A {
952                String single = 'hello'
953                String double = "world"
954                char ch = 'x'
955            }
956        "#;
957        let root = build_ast::<crate::GroovyParser>(code, "test.groovy");
958        // The dekobon Groovy grammar consolidates every string shape
959        // under one `string_literal` kind name (single-quoted,
960        // double-quoted, triple-quoted, slashy `/.../`, dollar-slashy
961        // `$/.../$`, GString-interpolated); there is no separate
962        // `character_literal` kind. The defensive collect-by-name for
963        // `character_literal` below is a drift marker — it is expected
964        // to find nothing today but would surface a future grammar
965        // bump that promoted character literals to a distinct kind.
966        let mut strings = Vec::new();
967        collect_nodes_by_kind(&root, "string_literal", &mut strings);
968        collect_nodes_by_kind(&root, "character_literal", &mut strings);
969        assert!(
970            !strings.is_empty(),
971            "expected at least one string/character literal in the AST"
972        );
973        for node in &strings {
974            assert!(
975                node.children.is_empty(),
976                "string-like node should be flattened (no children); got {} children, value={:?}",
977                node.children.len(),
978                node.value,
979            );
980            assert!(
981                !node.value.is_empty(),
982                "flattened string-like node should have non-empty text value"
983            );
984        }
985    }
986
987    #[cfg(feature = "groovy")]
988    #[test]
989    fn groovy_multiline_string_fragment_preserves_newlines() {
990        // The `StringLiteral`/`MultilineStringFragment` arms route
991        // through `get_text_span(..., true)` to keep the body text
992        // verbatim. A regression that flips that boolean to `false`
993        // would trim embedded whitespace — including the newlines
994        // inside triple-quoted strings. The outer `StringLiteral`
995        // node is flattened by the alterator and carries the entire
996        // triple-quoted body as its value.
997        let code = b"def s = \"\"\"first line\nsecond line\nthird line\"\"\"";
998        let root = build_ast::<crate::GroovyParser>(code, "test.groovy");
999        let mut strings = Vec::new();
1000        collect_nodes_by_kind(&root, "string_literal", &mut strings);
1001        assert!(
1002            !strings.is_empty(),
1003            "expected at least one string_literal node for the triple-quoted body"
1004        );
1005        let any_with_newline = strings.iter().any(|n| n.value.contains('\n'));
1006        assert!(
1007            any_with_newline,
1008            "expected the flattened string_literal to keep its embedded newlines; got values: {:?}",
1009            strings.iter().map(|n| &n.value).collect::<Vec<_>>()
1010        );
1011    }
1012
1013    #[cfg(feature = "rust")]
1014    #[test]
1015    fn rust_raw_string_literal_flattened() {
1016        // Regression for issue #391: `Rust::RawStringLiteral` was missing
1017        // from the `RustCode` Alterator arm, so `r#"hello"#` rendered with
1018        // structured children (`_raw_string_literal_start`,
1019        // `string_content`, `_raw_string_literal_end`) in the AST dump
1020        // while the regular `"hello"` was flattened to a single text
1021        // node. `Checker::is_string` and `Getter::get_op_type` already
1022        // treat both as equivalent; the alterator now matches.
1023        let code = br##"fn main() { let s = r#"hello"#; let t = "world"; }"##;
1024        let root = build_ast::<crate::RustParser>(code, "test.rs");
1025
1026        let mut raw_strings = Vec::new();
1027        collect_nodes_by_kind(&root, "raw_string_literal", &mut raw_strings);
1028        assert_eq!(
1029            raw_strings.len(),
1030            1,
1031            "expected exactly one raw_string_literal node"
1032        );
1033        let raw = raw_strings[0];
1034        assert_eq!(
1035            raw.children.len(),
1036            0,
1037            "raw_string_literal should be flattened (no children)"
1038        );
1039        assert_eq!(
1040            raw.value, "r#\"hello\"#",
1041            "raw_string_literal should preserve the verbatim source text"
1042        );
1043
1044        // The regular StringLiteral arm must still flatten too — confirm
1045        // the asymmetry that motivated #391 is gone.
1046        let mut strings = Vec::new();
1047        collect_nodes_by_kind(&root, "string_literal", &mut strings);
1048        assert_eq!(strings.len(), 1, "expected exactly one string_literal node");
1049        assert_eq!(
1050            strings[0].children.len(),
1051            0,
1052            "string_literal should be flattened (no children)"
1053        );
1054        assert_eq!(strings[0].value, "\"world\"");
1055    }
1056
1057    #[cfg(feature = "cpp")]
1058    #[test]
1059    fn cpp_raw_string_literal_flattened() {
1060        // Regression for issue #398: `Cpp::RawStringLiteral` was missing
1061        // from the `CppCode` Alterator arm, so `R"(hello)"` rendered with
1062        // structured delimiter / `string_content` children in the AST
1063        // dump while the regular `"hello"` was flattened to a single
1064        // text node. `Checker::is_string` already treats both as
1065        // equivalent; the alterator now matches. Peer of issue #391
1066        // for Rust.
1067        let code = br#"int main() { auto s = R"(hello)"; auto t = "world"; }"#;
1068        let root = build_ast::<crate::CppParser>(code, "test.cpp");
1069
1070        let mut raw_strings = Vec::new();
1071        collect_nodes_by_kind(&root, "raw_string_literal", &mut raw_strings);
1072        assert_eq!(
1073            raw_strings.len(),
1074            1,
1075            "expected exactly one raw_string_literal node"
1076        );
1077        let raw = raw_strings[0];
1078        assert_eq!(
1079            raw.children.len(),
1080            0,
1081            "raw_string_literal should be flattened (no children)"
1082        );
1083        assert_eq!(
1084            raw.value, "R\"(hello)\"",
1085            "raw_string_literal should preserve the verbatim source text"
1086        );
1087
1088        // The regular StringLiteral arm must still flatten too — confirm
1089        // the asymmetry that motivated #398 is gone.
1090        let mut strings = Vec::new();
1091        collect_nodes_by_kind(&root, "string_literal", &mut strings);
1092        assert_eq!(strings.len(), 1, "expected exactly one string_literal node");
1093        assert_eq!(
1094            strings[0].children.len(),
1095            0,
1096            "string_literal should be flattened (no children)"
1097        );
1098        assert_eq!(strings[0].value, "\"world\"");
1099    }
1100
1101    /// Asserts that every node of kind `target_kind` in the AST is
1102    /// flattened (no children) and carries non-empty verbatim text.
1103    /// Shared by the #699 per-language string-flattening regressions.
1104    #[cfg(any(
1105        feature = "bash",
1106        feature = "c",
1107        feature = "cpp",
1108        feature = "go",
1109        feature = "java",
1110        feature = "javascript",
1111        feature = "kotlin",
1112        feature = "mozjs",
1113        feature = "perl",
1114        feature = "python",
1115        feature = "typescript",
1116    ))]
1117    fn assert_kind_flattened(root: &AstNode, target_kind: &str) {
1118        let mut nodes = Vec::new();
1119        collect_nodes_by_kind(root, target_kind, &mut nodes);
1120        assert!(
1121            !nodes.is_empty(),
1122            "expected at least one '{target_kind}' node in the AST"
1123        );
1124        for node in &nodes {
1125            assert!(
1126                node.children.is_empty(),
1127                "'{target_kind}' node should be flattened (no children), got {} children; value={:?}",
1128                node.children.len(),
1129                node.value,
1130            );
1131            assert!(
1132                !node.value.is_empty(),
1133                "flattened '{target_kind}' node should have non-empty text value"
1134            );
1135        }
1136    }
1137
1138    // ===== #699: align alterator flattening with `Checker::is_string` =====
1139    // Before #699 these string-like literals kept their structured
1140    // children in the AST dump while `is_string` already treated the whole
1141    // node as a string — a 3-way (alterator / is_string / get_op_type)
1142    // dump asymmetry. Each test pins that the dump now collapses the kind.
1143
1144    #[cfg(feature = "javascript")]
1145    #[test]
1146    fn javascript_template_string_flattened() {
1147        // #699: `is_string` matches `TemplateString`; the alterator now
1148        // flattens it too, collapsing `${…}` interpolation children into
1149        // the flat text payload (the established convention for PHP
1150        // `encapsed_string` / Ruby / C# interpolated strings).
1151        let code = br#"const a = 1; const b = `bare`; const c = `pre ${a} post`;"#;
1152        let root = build_ast::<crate::JavascriptParser>(code, "test.js");
1153        assert_kind_flattened(&root, "template_string");
1154    }
1155
1156    #[cfg(feature = "typescript")]
1157    #[test]
1158    fn typescript_template_string_flattened() {
1159        let code = br#"const a = 1; const b = `bare`; const c = `pre ${a} post`;"#;
1160        let root = build_ast::<crate::TypescriptParser>(code, "test.ts");
1161        assert_kind_flattened(&root, "template_string");
1162    }
1163
1164    #[cfg(feature = "typescript")]
1165    #[test]
1166    fn tsx_template_string_flattened() {
1167        let code = br#"const a = 1; const b = `bare`; const c = `pre ${a} post`;"#;
1168        let root = build_ast::<crate::TsxParser>(code, "test.tsx");
1169        assert_kind_flattened(&root, "template_string");
1170    }
1171
1172    #[cfg(feature = "mozjs")]
1173    #[test]
1174    fn mozjs_template_string_flattened() {
1175        // The MozJS arm's comment previously claimed `TemplateString`
1176        // flattening it did not perform; #699 made the code match.
1177        let code = br#"const a = 1; const b = `bare`; const c = `pre ${a} post`;"#;
1178        let root = build_ast::<crate::MozjsParser>(code, "test.jsm");
1179        assert_kind_flattened(&root, "template_string");
1180    }
1181
1182    #[cfg(feature = "cpp")]
1183    #[test]
1184    fn cpp_concatenated_string_flattened() {
1185        // #699: `is_string` matches `concatenated_string` (`"a" "b"`);
1186        // the alterator now flattens the wrapper instead of leaving its
1187        // adjacent `string_literal` children structured.
1188        let code = br#"const char* s = "a" "b";"#;
1189        let root = build_ast::<crate::CppParser>(code, "test.cpp");
1190        assert_kind_flattened(&root, "concatenated_string");
1191    }
1192
1193    /// C's string flattening reads the `StringLit` row (#1698); it had
1194    /// no test of its own — the C++ ones covered the C++ arm only — so
1195    /// severing it from the row failed nothing until this.
1196    #[cfg(feature = "c")]
1197    #[test]
1198    fn c_string_and_concatenated_string_flattened() {
1199        let code = br#"const char *s = "a" "b"; const char *t = "c"; char ch = 'x';"#;
1200        let root = build_ast::<crate::CParser>(code, "test.c");
1201        assert_kind_flattened(&root, "concatenated_string");
1202        assert_kind_flattened(&root, "string_literal");
1203        assert_kind_flattened(&root, "char_literal");
1204    }
1205
1206    #[cfg(feature = "python")]
1207    #[test]
1208    fn python_string_and_concatenated_string_flattened() {
1209        // #699: Python had no alterator override, so `string` (incl.
1210        // f-strings) and `concatenated_string` kept structured children
1211        // while `is_string` matched them. The override now flattens both;
1212        // the f-string `{a}` interpolation collapses into the text value.
1213        let code = b"a = 1\nb = \"plain\"\nc = f\"x{a}y\"\nd = \"ab\" \"cd\"\n";
1214        let root = build_ast::<crate::PythonParser>(code, "test.py");
1215        assert_kind_flattened(&root, "string");
1216        assert_kind_flattened(&root, "concatenated_string");
1217    }
1218
1219    #[cfg(feature = "java")]
1220    #[test]
1221    fn java_string_literals_flattened() {
1222        // #699: Java had no alterator override, so `string_literal` kept
1223        // its `string_fragment` children. The override flattens it now.
1224        // Triple-quoted text blocks (`"""…"""`) surface as multi-row
1225        // `string_literal` nodes too (the `Java::MultilineStringLiteral`
1226        // enum variant is the hidden `_multiline_string_literal` supertype
1227        // and never appears concretely — see the checker drift marker in
1228        // `simple_is_string_macro_recognises_each_language`); the arm
1229        // lists it for defensive parity with `is_string` but the concrete
1230        // coverage rides on `string_literal`.
1231        let code = b"class T { void m() { String a = \"hi\"; String b = \"\"\"\nblock\"\"\"; } }";
1232        let root = build_ast::<crate::JavaParser>(code, "T.java");
1233        assert_kind_flattened(&root, "string_literal");
1234        // The text block produces a multi-row `string_literal`; confirm
1235        // the flattened node still carries the verbatim multi-line body.
1236        let mut strings = Vec::new();
1237        collect_nodes_by_kind(&root, "string_literal", &mut strings);
1238        assert!(
1239            strings.iter().any(|n| n.value.contains("block")),
1240            "expected the flattened text-block string_literal to keep its body"
1241        );
1242    }
1243
1244    #[cfg(feature = "kotlin")]
1245    #[test]
1246    fn kotlin_string_literals_flattened() {
1247        // #699: Kotlin had no alterator override. `string_literal` (incl.
1248        // `${…}` interpolation, collapsed into the text value) and
1249        // `multiline_string_literal` (`"""…"""`) now flatten.
1250        let code = b"fun m() { val a = 1; val b = \"x${a}y\"; val c = \"\"\"block\"\"\" }";
1251        let root = build_ast::<crate::KotlinParser>(code, "test.kt");
1252        assert_kind_flattened(&root, "string_literal");
1253        assert_kind_flattened(&root, "multiline_string_literal");
1254    }
1255
1256    #[cfg(feature = "go")]
1257    #[test]
1258    fn go_rune_literal_flattened_but_not_a_string_kind() {
1259        // #699 verdict: Go `rune_literal` is operand + flattened but
1260        // deliberately excluded from `is_string` (a rune is a character,
1261        // not a string) — mirroring Rust/Cpp `char_literal`. This pins the
1262        // flattening half; the `is_string` exclusion is pinned in
1263        // `checker.rs::go_rune_literal_is_not_a_string`.
1264        let code = b"package main\nfunc main() { r := 'x'; _ = r }\n";
1265        let root = build_ast::<crate::GoParser>(code, "test.go");
1266        assert_kind_flattened(&root, "rune_literal");
1267    }
1268
1269    #[cfg(feature = "bash")]
1270    #[test]
1271    fn bash_heredoc_body_flattened() {
1272        // #761: `Checker::is_string` matches Bash `heredoc_body`
1273        // (`Bash::HeredocBody2`) but the alterator omitted it, so a
1274        // heredoc body with `${…}` interpolation kept its structured
1275        // `expansion` children in the AST dump while `is_string` treated
1276        // the whole node as one string. The arm now flattens it — same
1277        // convention as Ruby `heredoc_body` / PHP `heredoc`.
1278        let code = b"x=1\ncat <<EOF\npre ${x} post\nEOF\n";
1279        let root = build_ast::<crate::BashParser>(code, "test.sh");
1280        assert_kind_flattened(&root, "heredoc_body");
1281        // The flattened body must carry the verbatim interpolated text,
1282        // not just the literal fragments.
1283        let mut bodies = Vec::new();
1284        collect_nodes_by_kind(&root, "heredoc_body", &mut bodies);
1285        assert!(
1286            bodies.iter().any(|n| n.value.contains("${x}")),
1287            "expected the flattened heredoc_body to keep its interpolation text; got {:?}",
1288            bodies.iter().map(|n| &n.value).collect::<Vec<_>>()
1289        );
1290    }
1291
1292    #[cfg(feature = "perl")]
1293    #[test]
1294    fn perl_heredoc_body_statement_flattened() {
1295        // #761: `Checker::is_string` matches Perl `heredoc_body_statement`
1296        // (`Perl::HeredocBodyStatement`) but the alterator omitted it, so a
1297        // heredoc body with interpolation kept its structured
1298        // `interpolation` children in the AST dump while `is_string`
1299        // treated the whole node as one string. The arm now flattens it.
1300        let code = b"my $x = 1;\nmy $s = <<\"EOF\";\npre $x post\nEOF\n";
1301        let root = build_ast::<crate::PerlParser>(code, "test.pl");
1302        assert_kind_flattened(&root, "heredoc_body_statement");
1303        let mut bodies = Vec::new();
1304        collect_nodes_by_kind(&root, "heredoc_body_statement", &mut bodies);
1305        assert!(
1306            bodies.iter().any(|n| n.value.contains("$x")),
1307            "expected the flattened heredoc_body_statement to keep its interpolation text; got {:?}",
1308            bodies.iter().map(|n| &n.value).collect::<Vec<_>>()
1309        );
1310    }
1311}