Skip to content

Commit 2518fcb

Browse files
committed
fix: Handle module.exports = class Foo when locating classes
1 parent 65fbb26 commit 2518fcb

File tree

6 files changed

+75
-5
lines changed

6 files changed

+75
-5
lines changed

src/instrumentation.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::path::PathBuf;
77
use swc_core::common::{Span, SyntaxContext};
88
use swc_core::ecma::{
99
ast::{
10-
ArrowExpr, AssignExpr, AssignTarget, BlockStmt, ClassDecl, ClassMethod, Constructor, Expr,
10+
ArrowExpr, AssignExpr, AssignTarget, BlockStmt, ClassDecl, ClassExpr, ClassMethod, Constructor, Expr,
1111
FnDecl, FnExpr, Ident, Lit, MemberProp, MethodProp, Module, ModuleItem, Pat, PropName,
1212
Script, SimpleAssignTarget, Stmt, Str, VarDecl,
1313
},
@@ -236,6 +236,19 @@ impl Instrumentation {
236236
.is_none_or(|class| node.ident.sym.as_ref() == class);
237237
true
238238
}
239+
240+
pub fn visit_mut_class_expr(&mut self, node: &mut ClassExpr) -> bool {
241+
self.is_correct_class = self
242+
.config
243+
.function_query
244+
.class_name()
245+
.is_none_or(|class| {
246+
node.ident
247+
.as_ref()
248+
.map_or(false, |ident| ident.sym.as_ref() == class)
249+
});
250+
true
251+
}
239252

240253
pub fn visit_mut_class_method(&mut self, node: &mut ClassMethod) -> bool {
241254
let name = match &node.key {

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use swc_core::{
2727
common::{comments::Comments, errors::ColorConfig, FileName, FilePathMapping},
2828
ecma::{
2929
ast::{
30-
AssignExpr, ClassDecl, ClassMethod, Constructor, EsVersion, FnDecl, MethodProp, Module,
30+
AssignExpr, ClassDecl, ClassMethod, ClassExpr, Constructor, EsVersion, FnDecl, MethodProp, Module,
3131
Script, Str, VarDecl,
3232
},
3333
visit::{VisitMut, VisitMutWith},
@@ -227,6 +227,7 @@ impl VisitMut for InstrumentationVisitor {
227227
visit_with_all_fn!(visit_mut_method_prop, MethodProp);
228228
visit_with_all_fn!(visit_mut_assign_expr, AssignExpr);
229229
visit_with_all_fn!(visit_mut_class_decl, ClassDecl);
230+
visit_with_all_fn!(visit_mut_class_expr, ClassExpr);
230231
visit_with_all_fn!(visit_mut_class_method, ClassMethod);
231232
visit_with_all_fn!(visit_mut_constructor, Constructor);
232233
}

tests/class_method_cjs/mod.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ class UndiciBase {
77
return 42;
88
}
99
}
10-
class Undici extends UndiciBase {
10+
module.exports = class Undici extends UndiciBase {
1111
async fetch (url) {
1212
return super.fetch(url);
1313
}
14-
}
14+
};
1515

16-
module.exports = Undici;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const { tracingChannel: tr_ch_apm_tracingChannel } = require("diagnostics_channel");
2+
const tr_ch_apm$up_fetch = tr_ch_apm_tracingChannel("orchestrion:one:up:fetch");
3+
const tr_ch_apm$up_constructor = tr_ch_apm_tracingChannel("orchestrion:one:up:constructor");
4+
module.exports = class Up {
5+
constructor(){
6+
const tr_ch_apm_ctx$up_constructor = {
7+
arguments
8+
};
9+
try {
10+
if (tr_ch_apm$up_constructor.hasSubscribers) {
11+
tr_ch_apm$up_constructor.start.publish(tr_ch_apm_ctx$up_constructor);
12+
}
13+
console.log('constructor');
14+
} catch (tr_ch_err) {
15+
if (tr_ch_apm$up_constructor.hasSubscribers) {
16+
tr_ch_apm_ctx$up_constructor.error = tr_ch_err;
17+
try {
18+
tr_ch_apm_ctx$up_constructor.self = this;
19+
} catch (refErr) {}
20+
tr_ch_apm$up_constructor.error.publish(tr_ch_apm_ctx$up_constructor);
21+
}
22+
throw tr_ch_err;
23+
} finally{
24+
if (tr_ch_apm$up_constructor.hasSubscribers) {
25+
tr_ch_apm_ctx$up_constructor.self = this;
26+
tr_ch_apm$up_constructor.end.publish(tr_ch_apm_ctx$up_constructor);
27+
}
28+
}
29+
}
30+
fetch() {
31+
const traced = ()=>{
32+
console.log('fetch');
33+
};
34+
if (!tr_ch_apm$up_fetch.hasSubscribers) return traced();
35+
return tr_ch_apm$up_fetch.traceSync(traced, {
36+
arguments,
37+
self: this
38+
});
39+
}
40+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = class Up {
2+
constructor() {
3+
console.log('constructor')
4+
}
5+
6+
fetch() {
7+
console.log('fetch')
8+
}
9+
}
10+

tests/wasm/tests.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ const output = matchedTransforms.transform(original.toString('utf8'), true);
3333

3434
const expected = await fs.readFile(path.join(import.meta.dirname, './testdata/expected.mjs'))
3535
assert.strictEqual(output, expected.toString('utf8'));
36+
37+
const originalCjs = await fs.readFile(path.join(import.meta.dirname, './testdata/original-cjs.js'))
38+
const outputCjs = matchedTransforms.transform(originalCjs.toString('utf8'), false);
39+
40+
41+
const expectedCjs = await fs.readFile(path.join(import.meta.dirname, './testdata/expected-cjs.js'))
42+
assert.strictEqual(outputCjs, expectedCjs.toString('utf8'));

0 commit comments

Comments
 (0)