Skip to content

Commit 378f9b7

Browse files
authored
fix: Don't check for matching sync/async function type (#5)
1 parent 75f6d81 commit 378f9b7

File tree

6 files changed

+51
-21
lines changed

6 files changed

+51
-21
lines changed

src/function_query.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
33
* This product includes software developed at Datadog (<https://www.datadoghq.com>/). Copyright 2025 Datadog, Inc.
44
**/
5-
use swc_core::ecma::ast::{FnDecl, FnExpr, Function};
5+
use swc_core::ecma::ast::FnDecl;
66

77
#[derive(Debug, Clone)]
88
pub(crate) enum FunctionType {
@@ -25,14 +25,6 @@ impl FunctionKind {
2525
matches!(self, FunctionKind::Async)
2626
}
2727

28-
#[must_use]
29-
pub fn matches(&self, func: &Function) -> bool {
30-
match self {
31-
FunctionKind::Sync => !func.is_async && !func.is_generator,
32-
FunctionKind::Async => func.is_async && !func.is_generator,
33-
}
34-
}
35-
3628
#[must_use]
3729
pub fn tracing_operator(&self) -> &'static str {
3830
match self {
@@ -202,22 +194,19 @@ impl FunctionQuery {
202194

203195
pub fn matches_decl(&self, func: &FnDecl, count: &mut usize) -> bool {
204196
let matches_except_count = matches!(self.typ(), FunctionType::FunctionDeclaration)
205-
&& self.kind().matches(&func.function)
206197
&& func.ident.sym == self.name();
207198
self.maybe_increment_count(matches_except_count, count)
208199
}
209200

210-
pub fn matches_expr(&self, func: &FnExpr, count: &mut usize, name: &str) -> bool {
211-
let matches_except_count = matches!(self.typ(), FunctionType::FunctionExpression)
212-
&& self.kind().matches(&func.function)
213-
&& name == self.name();
201+
pub fn matches_expr(&self, count: &mut usize, name: &str) -> bool {
202+
let matches_except_count =
203+
matches!(self.typ(), FunctionType::FunctionExpression) && name == self.name();
214204
self.maybe_increment_count(matches_except_count, count)
215205
}
216206

217-
pub fn matches_method(&self, func: &Function, count: &mut usize, name: &str) -> bool {
218-
let matches_except_count = matches!(self.typ(), FunctionType::Method)
219-
&& self.kind().matches(func)
220-
&& name == self.name();
207+
pub fn matches_method(&self, count: &mut usize, name: &str) -> bool {
208+
let matches_except_count =
209+
matches!(self.typ(), FunctionType::Method) && name == self.name();
221210
self.maybe_increment_count(matches_except_count, count)
222211
}
223212
}

src/instrumentation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl Instrumentation {
166166
if self
167167
.config
168168
.function_query
169-
.matches_expr(func_expr, &mut self.count, name.as_ref())
169+
.matches_expr(&mut self.count, name.as_ref())
170170
&& func_expr.function.body.is_some()
171171
{
172172
if let Some(body) = func_expr.function.body.as_mut() {
@@ -251,7 +251,7 @@ impl Instrumentation {
251251
if self
252252
.config
253253
.function_query
254-
.matches_method(&node.function, &mut self.count, name.as_ref())
254+
.matches_method(&mut self.count, name.as_ref())
255255
&& node.function.body.is_some()
256256
{
257257
if let Some(body) = node.function.body.as_mut() {
@@ -284,7 +284,7 @@ impl Instrumentation {
284284
if self
285285
.config
286286
.function_query
287-
.matches_method(&node.function, &mut self.count, name.as_ref())
287+
.matches_method(&mut self.count, name.as_ref())
288288
&& node.function.body.is_some()
289289
{
290290
if let Some(body) = node.function.body.as_mut() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
/**
3+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
4+
* This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2025 Datadog, Inc.
5+
**/
6+
function fetch (url) {
7+
return Promise.resolve(42);
8+
}
9+
10+
export { fetch };

tests/decl_mjs_mismatched_type/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use crate::common::*;
2+
use orchestrion_js::*;
3+
4+
#[test]
5+
fn decl_mjs_mismatched_type() {
6+
transpile_and_test(
7+
file!(),
8+
true,
9+
Config::new_single(InstrumentationConfig::new(
10+
"fetch_decl",
11+
test_module_matcher(),
12+
FunctionQuery::function_declaration("fetch", FunctionKind::Async),
13+
)),
14+
);
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2025 Datadog, Inc.
4+
**/
5+
import { fetch } from './instrumented.mjs';
6+
import { assert, getContext } from '../common/preamble.js';
7+
const context = getContext('orchestrion:undici:fetch_decl');
8+
const result = await fetch('https://example.com');
9+
assert.strictEqual(result, 42);
10+
assert.deepStrictEqual(context, {
11+
start: true,
12+
end: true,
13+
asyncStart: 42,
14+
asyncEnd: 42
15+
});

tests/instrumentor_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod constructor_cjs;
99
mod constructor_mjs;
1010
mod decl_cjs;
1111
mod decl_mjs;
12+
mod decl_mjs_mismatched_type;
1213
mod expr_cjs;
1314
mod expr_mjs;
1415
mod index_cjs;

0 commit comments

Comments
 (0)