|
1 | 1 | use crate::error::OrchestrionError; |
2 | | -use swc_core::ecma::ast::{ClassMethod, FnDecl, FnExpr, Function, MethodProp}; |
| 2 | +use swc_core::ecma::ast::{FnDecl, FnExpr, Function}; |
3 | 3 | use yaml_rust2::Yaml; |
4 | 4 |
|
5 | 5 | macro_rules! get_str { |
@@ -71,36 +71,38 @@ pub struct FunctionQuery { |
71 | 71 | } |
72 | 72 |
|
73 | 73 | 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 | + } |
81 | 85 | } |
82 | 86 |
|
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) |
87 | 89 | && 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) |
90 | 92 | } |
91 | 93 |
|
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) |
94 | 96 | && 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) |
97 | 99 | } |
98 | 100 |
|
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) |
104 | 106 | } |
105 | 107 | } |
106 | 108 |
|
|
0 commit comments