11// Unless explicitly stated otherwise all files in this repository are licensed under the Apache License, Version 2.0.
22// This product includes software developed at Datadog (https://www.datadoghq.com/).
3- // Copyright 2024 Datadog, Inc.
3+ // Copyright 2024-2025 Datadog, Inc.
44
55// (NB: There is no need for any Rust business logic here, as `ddsa.js` is purely the user-facing JavaScript API)
66
@@ -12,14 +12,16 @@ mod tests {
1212 use crate :: analysis:: ddsa_lib:: test_utils:: {
1313 cfg_test_v8, js_class_eq, js_instance_eq, shorthand_execute_rule,
1414 } ;
15- use crate :: analysis:: ddsa_lib:: JsRuntime ;
1615 use crate :: analysis:: tree_sitter:: get_tree_sitter_language;
16+ use crate :: model:: common:: Language :: Python ;
1717
1818 #[ test]
1919 fn js_properties_canary ( ) {
2020 let expected = & [
2121 // Methods
2222 "getChildren" ,
23+ "findAncestor" ,
24+ "findDescendant" ,
2325 "getParent" ,
2426 "getTaintSinks" ,
2527 "getTaintSources" ,
@@ -35,6 +37,48 @@ mod tests {
3537 compat_helper_op_ts_node_named_children ( "ddsa.getChildren(node)" )
3638 }
3739
40+ #[ test]
41+ fn test_find_descendant ( ) {
42+ let mut rt = cfg_test_v8 ( ) . new_runtime ( ) ;
43+ let text = "print(foo)" ;
44+ let ts_query = "(module)@module" ;
45+ let rule_code = r#"
46+ function isIdentifier(n) { return n.cstType === "identifier"; }
47+
48+ function visit(query, filename, code) {{
49+ const n = query.captures.module;
50+ const c = ddsa.findDescendant(n, isIdentifier);
51+ console.log(c.text);
52+ }}
53+ "# ;
54+
55+ // Then execute the rule that fetches the children of the node.
56+ let res =
57+ shorthand_execute_rule ( & mut rt, Python , ts_query, & rule_code, text, None ) . unwrap ( ) ;
58+ assert_eq ! ( res. console_lines[ 0 ] , "print" ) ;
59+ }
60+
61+ #[ test]
62+ fn test_find_ancestor ( ) {
63+ let mut rt = cfg_test_v8 ( ) . new_runtime ( ) ;
64+ let text = "print(\" foo\" )" ;
65+ let ts_query = "(string_content)@sc" ;
66+ let rule_code = r#"
67+ function isModule(n) { return n.cstType === "module"; }
68+
69+ function visit(query, filename, code) {
70+ const n = query.captures.sc;
71+ const c = ddsa.findAncestor(n, isModule);
72+ console.log(c.text);
73+ }
74+ "# ;
75+
76+ // Then execute the rule that fetches the children of the node.
77+ let res =
78+ shorthand_execute_rule ( & mut rt, Python , ts_query, & rule_code, text, None ) . unwrap ( ) ;
79+ assert_eq ! ( res. console_lines[ 0 ] , "print(\" foo\" )" ) ;
80+ }
81+
3882 /// Stella syntax can get named children
3983 #[ test]
4084 fn op_ts_node_named_children_stella_compat ( ) {
0 commit comments