1#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
8
9use 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
17pub trait Alterator
21where
22 Self: Checker,
23{
24 #[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 #[must_use]
43 fn get_text_span(node: &Node, code: &[u8], span: bool, text: bool) -> (String, Option<Span>) {
44 let text = if text {
45 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 (
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 #[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 #[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 #[inline]
127 #[must_use]
128 fn keeps_children<'a>(_node: &Node<'a>, _code: &[u8], _ancestors: Ancestors<'a, '_>) -> bool {
129 false
130 }
131
132 #[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 _ 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 _ 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 let kind = node.kind_id();
244 match Objc::from(kind) {
245 _ 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 _ 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 let kind = node.kind_id();
655 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 let kind = node.kind_id();
678 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 let kind = node.kind_id();
704 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 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 #[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 #[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 #[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 #[cfg(feature = "javascript")]
847 #[test]
848 fn javascript_string_nodes_all_flattened() {
849 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 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 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 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 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 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 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 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 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 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 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 #[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 #[cfg(feature = "javascript")]
1141 #[test]
1142 fn javascript_template_string_flattened() {
1143 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 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 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 #[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 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 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 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 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 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 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 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 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}