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]
131 #[must_use]
132 fn keeps_children<'a>(_node: &Node<'a>, _code: &[u8], _ancestors: Ancestors<'a, '_>) -> bool {
133 false
134 }
135
136 #[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 _ 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 _ 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 let kind = node.kind_id();
248 match Objc::from(kind) {
249 _ 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 _ 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 let kind = node.kind_id();
659 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 let kind = node.kind_id();
682 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 let kind = node.kind_id();
708 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 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 #[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 #[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 #[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 #[cfg(feature = "javascript")]
851 #[test]
852 fn javascript_string_nodes_all_flattened() {
853 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 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 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 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 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 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 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 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 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 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 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 #[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 #[cfg(feature = "javascript")]
1145 #[test]
1146 fn javascript_template_string_flattened() {
1147 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 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 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 #[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 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 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 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 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 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 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 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 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}