Skip to content

Commit b461e7a

Browse files
authored
fix multiple functions (#2)
Previously, the index/count would refer to all unmatched functions, since the count was being incremented on any non-match. Now it's restricted to incrementing only when it would otherwise match. This makes it much less likely that index is necessary when selecting a function.
1 parent b757e86 commit b461e7a

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

src/function_query.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::error::OrchestrionError;
2-
use swc_core::ecma::ast::{ClassMethod, FnDecl, FnExpr, Function, MethodProp};
2+
use swc_core::ecma::ast::{FnDecl, FnExpr, Function};
33
use yaml_rust2::Yaml;
44

55
macro_rules! get_str {
@@ -71,36 +71,38 @@ pub struct FunctionQuery {
7171
}
7272

7373
impl FunctionQuery {
74-
pub fn matches_decl(&self, func: &FnDecl, count: usize) -> bool {
75-
// TODO(bengl) check if it's only the count that's wrong, and somehow
76-
// signal that so we can update the counter.
77-
matches!(self.typ, FunctionType::FunctionDeclaration)
78-
&& self.kind.matches(&func.function)
79-
&& func.ident.sym == self.name
80-
&& count == self.index
74+
fn maybe_increment_count(&self, matches_except_count: bool, count: &mut usize) -> bool {
75+
if matches_except_count {
76+
if self.index == *count {
77+
true
78+
} else {
79+
*count += 1;
80+
false
81+
}
82+
} else {
83+
false
84+
}
8185
}
8286

83-
pub fn matches_expr(&self, func: &FnExpr, count: usize, name: &str) -> bool {
84-
// TODO(bengl) check if it's only the count that's wrong, and somehow
85-
// signal that so we can update the counter.
86-
matches!(self.typ, FunctionType::FunctionExpression)
87+
pub fn matches_decl(&self, func: &FnDecl, count: &mut usize) -> bool {
88+
let matches_except_count = matches!(self.typ, FunctionType::FunctionDeclaration)
8789
&& self.kind.matches(&func.function)
88-
&& name == self.name
89-
&& count == self.index
90+
&& func.ident.sym == self.name;
91+
self.maybe_increment_count(matches_except_count, count)
9092
}
9193

92-
pub fn matches_class_method(&self, func: &ClassMethod, count: usize, name: &str) -> bool {
93-
matches!(self.typ, FunctionType::Method)
94+
pub fn matches_expr(&self, func: &FnExpr, count: &mut usize, name: &str) -> bool {
95+
let matches_except_count = matches!(self.typ, FunctionType::FunctionExpression)
9496
&& self.kind.matches(&func.function)
95-
&& name == self.name
96-
&& count == self.index
97+
&& name == self.name;
98+
self.maybe_increment_count(matches_except_count, count)
9799
}
98100

99-
pub fn matches_method_prop(&self, func: &MethodProp, count: usize, name: &str) -> bool {
100-
matches!(self.typ, FunctionType::Method)
101-
&& self.kind.matches(&func.function)
102-
&& name == self.name
103-
&& count == self.index
101+
pub fn matches_method(&self, func: &Function, count: &mut usize, name: &str) -> bool {
102+
let matches_except_count = matches!(self.typ, FunctionType::Method)
103+
&& self.kind.matches(func)
104+
&& name == self.name;
105+
self.maybe_increment_count(matches_except_count, count)
104106
}
105107
}
106108

src/instrumentation.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,14 @@ impl Instrumentation {
101101
if self
102102
.config
103103
.function_query
104-
.matches_expr(func_expr, self.count, name.as_ref())
104+
.matches_expr(func_expr, &mut self.count, name.as_ref())
105105
&& func_expr.function.body.is_some()
106106
{
107107
if let Some(body) = func_expr.function.body.as_mut() {
108108
self.insert_tracing(body);
109109
}
110110
true
111111
} else {
112-
self.count += 1;
113112
false
114113
}
115114
}
@@ -137,13 +136,15 @@ impl Instrumentation {
137136
}
138137

139138
pub fn visit_mut_fn_decl(&mut self, node: &mut FnDecl) -> bool {
140-
if self.config.function_query.matches_decl(node, self.count) && node.function.body.is_some()
139+
if self
140+
.config
141+
.function_query
142+
.matches_decl(node, &mut self.count)
143+
&& node.function.body.is_some()
141144
{
142145
if let Some(body) = node.function.body.as_mut() {
143146
self.insert_tracing(body);
144147
}
145-
} else {
146-
self.count += 1;
147148
}
148149
false
149150
}
@@ -170,14 +171,12 @@ impl Instrumentation {
170171
if self
171172
.config
172173
.function_query
173-
.matches_class_method(node, self.count, name.as_ref())
174+
.matches_method(&node.function, &mut self.count, name.as_ref())
174175
&& node.function.body.is_some()
175176
{
176177
if let Some(body) = node.function.body.as_mut() {
177178
self.insert_tracing(body);
178179
}
179-
} else {
180-
self.count += 1;
181180
}
182181
false
183182
}
@@ -190,14 +189,12 @@ impl Instrumentation {
190189
if self
191190
.config
192191
.function_query
193-
.matches_method_prop(node, self.count, name.as_ref())
192+
.matches_method(&node.function, &mut self.count, name.as_ref())
194193
&& node.function.body.is_some()
195194
{
196195
if let Some(body) = node.function.body.as_mut() {
197196
self.insert_tracing(body);
198197
}
199-
} else {
200-
self.count += 1;
201198
}
202199
false
203200
}

tests/expr_cjs/mod.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
'use strict';
2+
const foo = function () {}
3+
24
exports.fetch = async function (url) {
35
return 42;
46
}

0 commit comments

Comments
 (0)