diff --git a/src/function_query.rs b/src/function_query.rs index 99d4d2f..e8e7a4a 100644 --- a/src/function_query.rs +++ b/src/function_query.rs @@ -2,7 +2,7 @@ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. * This product includes software developed at Datadog (/). Copyright 2025 Datadog, Inc. **/ -use swc_core::ecma::ast::{FnDecl, FnExpr, Function}; +use swc_core::ecma::ast::FnDecl; #[derive(Debug, Clone)] pub(crate) enum FunctionType { @@ -25,14 +25,6 @@ impl FunctionKind { matches!(self, FunctionKind::Async) } - #[must_use] - pub fn matches(&self, func: &Function) -> bool { - match self { - FunctionKind::Sync => !func.is_async && !func.is_generator, - FunctionKind::Async => func.is_async && !func.is_generator, - } - } - #[must_use] pub fn tracing_operator(&self) -> &'static str { match self { @@ -202,22 +194,19 @@ impl FunctionQuery { pub fn matches_decl(&self, func: &FnDecl, count: &mut usize) -> bool { let matches_except_count = matches!(self.typ(), FunctionType::FunctionDeclaration) - && self.kind().matches(&func.function) && func.ident.sym == self.name(); self.maybe_increment_count(matches_except_count, count) } - pub fn matches_expr(&self, func: &FnExpr, count: &mut usize, name: &str) -> bool { - let matches_except_count = matches!(self.typ(), FunctionType::FunctionExpression) - && self.kind().matches(&func.function) - && name == self.name(); + pub fn matches_expr(&self, count: &mut usize, name: &str) -> bool { + let matches_except_count = + matches!(self.typ(), FunctionType::FunctionExpression) && name == self.name(); self.maybe_increment_count(matches_except_count, count) } - pub fn matches_method(&self, func: &Function, count: &mut usize, name: &str) -> bool { - let matches_except_count = matches!(self.typ(), FunctionType::Method) - && self.kind().matches(func) - && name == self.name(); + pub fn matches_method(&self, count: &mut usize, name: &str) -> bool { + let matches_except_count = + matches!(self.typ(), FunctionType::Method) && name == self.name(); self.maybe_increment_count(matches_except_count, count) } } diff --git a/src/instrumentation.rs b/src/instrumentation.rs index ec9813c..4ba5cb7 100644 --- a/src/instrumentation.rs +++ b/src/instrumentation.rs @@ -166,7 +166,7 @@ impl Instrumentation { if self .config .function_query - .matches_expr(func_expr, &mut self.count, name.as_ref()) + .matches_expr(&mut self.count, name.as_ref()) && func_expr.function.body.is_some() { if let Some(body) = func_expr.function.body.as_mut() { @@ -251,7 +251,7 @@ impl Instrumentation { if self .config .function_query - .matches_method(&node.function, &mut self.count, name.as_ref()) + .matches_method(&mut self.count, name.as_ref()) && node.function.body.is_some() { if let Some(body) = node.function.body.as_mut() { @@ -284,7 +284,7 @@ impl Instrumentation { if self .config .function_query - .matches_method(&node.function, &mut self.count, name.as_ref()) + .matches_method(&mut self.count, name.as_ref()) && node.function.body.is_some() { if let Some(body) = node.function.body.as_mut() { diff --git a/tests/decl_mjs_mismatched_type/mod.mjs b/tests/decl_mjs_mismatched_type/mod.mjs new file mode 100644 index 0000000..0356810 --- /dev/null +++ b/tests/decl_mjs_mismatched_type/mod.mjs @@ -0,0 +1,10 @@ + +/** + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. + * This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2025 Datadog, Inc. + **/ +function fetch (url) { + return Promise.resolve(42); +} + +export { fetch }; diff --git a/tests/decl_mjs_mismatched_type/mod.rs b/tests/decl_mjs_mismatched_type/mod.rs new file mode 100644 index 0000000..2049cdd --- /dev/null +++ b/tests/decl_mjs_mismatched_type/mod.rs @@ -0,0 +1,15 @@ +use crate::common::*; +use orchestrion_js::*; + +#[test] +fn decl_mjs_mismatched_type() { + transpile_and_test( + file!(), + true, + Config::new_single(InstrumentationConfig::new( + "fetch_decl", + test_module_matcher(), + FunctionQuery::function_declaration("fetch", FunctionKind::Async), + )), + ); +} diff --git a/tests/decl_mjs_mismatched_type/test.mjs b/tests/decl_mjs_mismatched_type/test.mjs new file mode 100644 index 0000000..f507df8 --- /dev/null +++ b/tests/decl_mjs_mismatched_type/test.mjs @@ -0,0 +1,15 @@ +/** + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. + * This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2025 Datadog, Inc. + **/ +import { fetch } from './instrumented.mjs'; +import { assert, getContext } from '../common/preamble.js'; +const context = getContext('orchestrion:undici:fetch_decl'); +const result = await fetch('https://example.com'); +assert.strictEqual(result, 42); +assert.deepStrictEqual(context, { + start: true, + end: true, + asyncStart: 42, + asyncEnd: 42 +}); diff --git a/tests/instrumentor_test.rs b/tests/instrumentor_test.rs index ec5b9fd..6434e92 100644 --- a/tests/instrumentor_test.rs +++ b/tests/instrumentor_test.rs @@ -9,6 +9,7 @@ mod constructor_cjs; mod constructor_mjs; mod decl_cjs; mod decl_mjs; +mod decl_mjs_mismatched_type; mod expr_cjs; mod expr_mjs; mod index_cjs;