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