diff --git a/src/instrumentation.rs b/src/instrumentation.rs index 58d5917..2748a86 100644 --- a/src/instrumentation.rs +++ b/src/instrumentation.rs @@ -287,6 +287,18 @@ impl Instrumentation { if let Pat::Ident(name) = &decl.name { traced = self.trace_expr_or_count(func_expr, &name.id.sym); } + } else if let Some(class_expr) = init.as_mut_class() { + if let Pat::Ident(name) = &decl.name { + if class_expr.ident.is_none() { + class_expr.ident = Some(name.id.clone()); + } else { + self.is_correct_class = self + .config + .function_query + .class_name() + .is_none_or(|class| name.id.sym.as_ref() == class); + } + } } } } @@ -303,11 +315,13 @@ impl Instrumentation { } pub fn visit_mut_class_expr(&mut self, node: &mut ClassExpr) -> bool { - self.is_correct_class = self.config.function_query.class_name().is_none_or(|class| { - node.ident - .as_ref() - .is_some_and(|ident| ident.sym.as_ref() == class) - }); + if !self.is_correct_class { + self.is_correct_class = self.config.function_query.class_name().is_none_or(|class| { + node.ident + .as_ref() + .is_some_and(|ident| ident.sym.as_ref() == class) + }); + } true } @@ -416,6 +430,18 @@ impl Instrumentation { _ => {} } } + } else if let Some(class_expr) = node.right.as_mut_class() { + if let AssignTarget::Simple(SimpleAssignTarget::Ident(name)) = &node.left { + if class_expr.ident.is_none() { + class_expr.ident = Some(name.id.clone()); + } else { + self.is_correct_class = self + .config + .function_query + .class_name() + .is_none_or(|class| name.id.sym.as_ref() == class); + } + } } !traced } diff --git a/tests/const_class_export_alias_mjs/mod.mjs b/tests/const_class_export_alias_mjs/mod.mjs new file mode 100644 index 0000000..0348c70 --- /dev/null +++ b/tests/const_class_export_alias_mjs/mod.mjs @@ -0,0 +1,7 @@ +const J = class { + async fetch(url) { + return 42; + } +}; + +export { J as Undici }; diff --git a/tests/const_class_export_alias_mjs/mod.rs b/tests/const_class_export_alias_mjs/mod.rs new file mode 100644 index 0000000..1be5651 --- /dev/null +++ b/tests/const_class_export_alias_mjs/mod.rs @@ -0,0 +1,15 @@ +use crate::common::*; +use orchestrion_js::*; + +#[test] +fn const_class_export_alias_mjs() { + transpile_and_test( + file!(), + true, + Config::new_single(InstrumentationConfig::new( + "Undici:fetch", + test_module_matcher(), + FunctionQuery::class_method("Undici", "fetch", FunctionKind::Async).as_export_alias(), + )), + ); +} diff --git a/tests/const_class_export_alias_mjs/test.mjs b/tests/const_class_export_alias_mjs/test.mjs new file mode 100644 index 0000000..3b4fdd4 --- /dev/null +++ b/tests/const_class_export_alias_mjs/test.mjs @@ -0,0 +1,12 @@ +import { Undici } from './instrumented.mjs'; +import { assert, getContext } from '../common/preamble.js'; +const context = getContext('orchestrion:undici:Undici:fetch'); +const undici = new Undici(); +const result = await undici.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 6c50772..eaaba3b 100644 --- a/tests/instrumentor_test.rs +++ b/tests/instrumentor_test.rs @@ -7,6 +7,7 @@ mod common; mod arguments_mutation; mod class_expression_cjs; mod class_method_cjs; +mod const_class_export_alias_mjs; mod constructor_cjs; mod constructor_mjs; mod decl_cjs; @@ -18,6 +19,7 @@ mod expr_cjs; mod expr_mjs; mod index_cjs; mod injection_failure; +mod let_class_export_alias_mjs; mod multiple_class_method_cjs; mod multiple_load_cjs; mod nested_functions; @@ -25,3 +27,5 @@ mod object_method_cjs; mod polyfill_cjs; mod polyfill_mjs; mod private_method_cjs; +mod var_class_export_alias_mjs; +mod var_named_class_export_alias_mjs; diff --git a/tests/let_class_export_alias_mjs/mod.mjs b/tests/let_class_export_alias_mjs/mod.mjs new file mode 100644 index 0000000..fb20b68 --- /dev/null +++ b/tests/let_class_export_alias_mjs/mod.mjs @@ -0,0 +1,7 @@ +let J = class { + async fetch(url) { + return 42; + } +}; + +export { J as Undici }; diff --git a/tests/let_class_export_alias_mjs/mod.rs b/tests/let_class_export_alias_mjs/mod.rs new file mode 100644 index 0000000..c9f5774 --- /dev/null +++ b/tests/let_class_export_alias_mjs/mod.rs @@ -0,0 +1,15 @@ +use crate::common::*; +use orchestrion_js::*; + +#[test] +fn let_class_export_alias_mjs() { + transpile_and_test( + file!(), + true, + Config::new_single(InstrumentationConfig::new( + "Undici:fetch", + test_module_matcher(), + FunctionQuery::class_method("Undici", "fetch", FunctionKind::Async).as_export_alias(), + )), + ); +} diff --git a/tests/let_class_export_alias_mjs/test.mjs b/tests/let_class_export_alias_mjs/test.mjs new file mode 100644 index 0000000..3b4fdd4 --- /dev/null +++ b/tests/let_class_export_alias_mjs/test.mjs @@ -0,0 +1,12 @@ +import { Undici } from './instrumented.mjs'; +import { assert, getContext } from '../common/preamble.js'; +const context = getContext('orchestrion:undici:Undici:fetch'); +const undici = new Undici(); +const result = await undici.fetch('https://example.com'); +assert.strictEqual(result, 42); +assert.deepStrictEqual(context, { + start: true, + end: true, + asyncStart: 42, + asyncEnd: 42 +}); diff --git a/tests/var_class_export_alias_mjs/mod.mjs b/tests/var_class_export_alias_mjs/mod.mjs new file mode 100644 index 0000000..cb5cb8f --- /dev/null +++ b/tests/var_class_export_alias_mjs/mod.mjs @@ -0,0 +1,7 @@ +var J = class { + async fetch(url) { + return 42; + } +}; + +export { J as Undici }; diff --git a/tests/var_class_export_alias_mjs/mod.rs b/tests/var_class_export_alias_mjs/mod.rs new file mode 100644 index 0000000..18245a3 --- /dev/null +++ b/tests/var_class_export_alias_mjs/mod.rs @@ -0,0 +1,15 @@ +use crate::common::*; +use orchestrion_js::*; + +#[test] +fn var_class_export_alias_mjs() { + transpile_and_test( + file!(), + true, + Config::new_single(InstrumentationConfig::new( + "Undici:fetch", + test_module_matcher(), + FunctionQuery::class_method("Undici", "fetch", FunctionKind::Async).as_export_alias(), + )), + ); +} diff --git a/tests/var_class_export_alias_mjs/test.mjs b/tests/var_class_export_alias_mjs/test.mjs new file mode 100644 index 0000000..3b4fdd4 --- /dev/null +++ b/tests/var_class_export_alias_mjs/test.mjs @@ -0,0 +1,12 @@ +import { Undici } from './instrumented.mjs'; +import { assert, getContext } from '../common/preamble.js'; +const context = getContext('orchestrion:undici:Undici:fetch'); +const undici = new Undici(); +const result = await undici.fetch('https://example.com'); +assert.strictEqual(result, 42); +assert.deepStrictEqual(context, { + start: true, + end: true, + asyncStart: 42, + asyncEnd: 42 +}); diff --git a/tests/var_named_class_export_alias_mjs/mod.mjs b/tests/var_named_class_export_alias_mjs/mod.mjs new file mode 100644 index 0000000..5ba3d3c --- /dev/null +++ b/tests/var_named_class_export_alias_mjs/mod.mjs @@ -0,0 +1,13 @@ +class Base { + async fetch(url) { + return 0; + } +} + +var J = class InternalName extends Base { + async fetch(url) { + return 42; + } +}; + +export { J as Undici }; diff --git a/tests/var_named_class_export_alias_mjs/mod.rs b/tests/var_named_class_export_alias_mjs/mod.rs new file mode 100644 index 0000000..fcb87d3 --- /dev/null +++ b/tests/var_named_class_export_alias_mjs/mod.rs @@ -0,0 +1,15 @@ +use crate::common::*; +use orchestrion_js::*; + +#[test] +fn var_named_class_export_alias_mjs() { + transpile_and_test( + file!(), + true, + Config::new_single(InstrumentationConfig::new( + "Undici:fetch", + test_module_matcher(), + FunctionQuery::class_method("Undici", "fetch", FunctionKind::Async).as_export_alias(), + )), + ); +} diff --git a/tests/var_named_class_export_alias_mjs/test.mjs b/tests/var_named_class_export_alias_mjs/test.mjs new file mode 100644 index 0000000..3b4fdd4 --- /dev/null +++ b/tests/var_named_class_export_alias_mjs/test.mjs @@ -0,0 +1,12 @@ +import { Undici } from './instrumented.mjs'; +import { assert, getContext } from '../common/preamble.js'; +const context = getContext('orchestrion:undici:Undici:fetch'); +const undici = new Undici(); +const result = await undici.fetch('https://example.com'); +assert.strictEqual(result, 42); +assert.deepStrictEqual(context, { + start: true, + end: true, + asyncStart: 42, + asyncEnd: 42 +});