From e5d55ae5eea65b6e3be39079d1e12ddb8fda6d63 Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Mon, 11 Nov 2024 10:53:01 -0500 Subject: [PATCH] feat: upgrade tree-sitter to `0.24.4` --- Cargo.toml | 2 +- crates/static-analysis-kernel/Cargo.toml | 1 + .../src/analysis/ddsa_lib/context/file_go.rs | 6 ++++-- .../src/analysis/ddsa_lib/context/file_js.rs | 4 ++-- .../src/analysis/ddsa_lib/context/file_tf.rs | 4 ++-- .../src/analysis/ddsa_lib/context/root.rs | 5 ++++- .../src/analysis/ddsa_lib/js/ts_node.rs | 1 + crates/static-analysis-kernel/src/analysis/tree_sitter.rs | 3 ++- 8 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f1f5ca96..b0f6c949 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,4 +39,4 @@ sha2 = "0.10.7" num_cpus = "1.15.0" tracing = "0.1.40" uuid = { version = "1.6.1", features = ["v4"] } -tree-sitter = "0.22.6" +tree-sitter = "0.24.4" diff --git a/crates/static-analysis-kernel/Cargo.toml b/crates/static-analysis-kernel/Cargo.toml index 8acee3a5..ec9bd238 100644 --- a/crates/static-analysis-kernel/Cargo.toml +++ b/crates/static-analysis-kernel/Cargo.toml @@ -22,6 +22,7 @@ globset = "0.4.14" graphviz-rust = "0.9.0" sequence_trie = "0.3.6" serde_yaml = "0.9.21" +streaming-iterator = "0.1.9" thiserror = "1.0.59" [build-dependencies] diff --git a/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/file_go.rs b/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/file_go.rs index 094545a7..923065a3 100644 --- a/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/file_go.rs +++ b/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/file_go.rs @@ -8,6 +8,7 @@ use crate::analysis::tree_sitter::get_tree_sitter_language; use crate::model::common::Language; use deno_core::v8; use deno_core::v8::HandleScope; +use streaming_iterator::StreamingIterator; /// Structure for the file context that is specific to Go. #[derive(Debug)] @@ -44,8 +45,9 @@ impl FileContextGo { // the second capture is the name of the package. let mut query_cursor = tree_sitter::QueryCursor::new(); - let query_result = query_cursor.matches(&self.ts_query, tree.root_node(), code.as_bytes()); - for query_match in query_result { + let mut query_result = + query_cursor.matches(&self.ts_query, tree.root_node(), code.as_bytes()); + while let Some(query_match) = query_result.next() { let mut package_name: Option<&str> = None; let mut package_alias: Option<&str> = None; diff --git a/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/file_js.rs b/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/file_js.rs index 138b3dd7..25c60efd 100644 --- a/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/file_js.rs +++ b/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/file_js.rs @@ -4,6 +4,7 @@ use deno_core::v8; use deno_core::v8::HandleScope; +use streaming_iterator::StreamingIterator; use crate::analysis::ddsa_lib::common::{Class, DDSAJsRuntimeError}; use crate::analysis::ddsa_lib::js::JSPackageImport; @@ -165,8 +166,7 @@ impl FileContextJavaScript { let query_result = query_cursor.matches(&self.query, tree.root_node(), code.as_bytes()); let imports = query_result - .into_iter() - .filter_map(|query_match| { + .filter_map_deref(|query_match| { let mut name = None; let mut imported_from = None; let mut has_default = false; diff --git a/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/file_tf.rs b/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/file_tf.rs index fdefa22a..5201db20 100644 --- a/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/file_tf.rs +++ b/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/file_tf.rs @@ -3,6 +3,7 @@ // Copyright 2024 Datadog, Inc. use deno_core::v8::{self, HandleScope}; +use streaming_iterator::StreamingIterator; use crate::analysis::ddsa_lib::common::{Class, DDSAJsRuntimeError}; use crate::analysis::ddsa_lib::js; @@ -55,8 +56,7 @@ impl FileContextTerraform { let query_result = query_cursor.matches(&self.query, tree.root_node(), code.as_bytes()); let resources = query_result - .into_iter() - .map(|query_match| { + .map_deref(|query_match| { let mut resource_type = None; let mut resource_name = None; diff --git a/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/root.rs b/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/root.rs index e918006e..9e50d29f 100644 --- a/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/root.rs +++ b/crates/static-analysis-kernel/src/analysis/ddsa_lib/context/root.rs @@ -118,7 +118,10 @@ impl RootContext { ///// let mut depth = 0; - while let Some(child) = current_node.child_containing_descendant(node) { + while let Some(child) = current_node.child_with_descendant(node) { + if child == node { + break; + } // Cache the relationship discovered as a side effect of traversing down from the root. // Note that because the `child_containing_descendant` API filter its output, we // only end up caching the path directly from the root to this node. diff --git a/crates/static-analysis-kernel/src/analysis/ddsa_lib/js/ts_node.rs b/crates/static-analysis-kernel/src/analysis/ddsa_lib/js/ts_node.rs index ab6315d8..7081f25f 100644 --- a/crates/static-analysis-kernel/src/analysis/ddsa_lib/js/ts_node.rs +++ b/crates/static-analysis-kernel/src/analysis/ddsa_lib/js/ts_node.rs @@ -106,6 +106,7 @@ mod tests { use crate::model::common::Language; use deno_core::v8; use std::marker::PhantomData; + use streaming_iterator::StreamingIterator; #[test] fn js_properties_canary() { diff --git a/crates/static-analysis-kernel/src/analysis/tree_sitter.rs b/crates/static-analysis-kernel/src/analysis/tree_sitter.rs index ec4d001d..9fa909d1 100644 --- a/crates/static-analysis-kernel/src/analysis/tree_sitter.rs +++ b/crates/static-analysis-kernel/src/analysis/tree_sitter.rs @@ -4,6 +4,7 @@ use common::model::position::Position; use indexmap::IndexMap; use std::collections::HashMap; use std::sync::Arc; +use streaming_iterator::StreamingIterator; use tree_sitter::CaptureQuantifier; pub fn get_tree_sitter_language(language: &Language) -> tree_sitter::Language { @@ -176,7 +177,7 @@ impl<'a, 'tree> TSQueryCursor<'a, 'tree> { MaybeOwnedMut::Owned(cursor) => cursor, }; let matches = cursor.matches(self.query, node, text.as_bytes()); - matches.map(|q_match| { + matches.map_deref(|q_match| { for capture in q_match.captures { self.captures_scratch .entry(capture.index)