Skip to content

Commit d1e6398

Browse files
committed
Get methods as functions in JS
1 parent 90ca554 commit d1e6398

File tree

2 files changed

+144
-16
lines changed

2 files changed

+144
-16
lines changed

src/checker.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ impl Checker for MozjsCode {
154154
Function,
155155
GeneratorFunction,
156156
FunctionDeclaration,
157-
GeneratorFunctionDeclaration
157+
GeneratorFunctionDeclaration,
158+
MethodDefinition
158159
);
159160
mk_checker!(
160161
is_func_space,
@@ -163,6 +164,7 @@ impl Checker for MozjsCode {
163164
Class,
164165
GeneratorFunction,
165166
FunctionDeclaration,
167+
MethodDefinition,
166168
GeneratorFunctionDeclaration,
167169
ClassDeclaration
168170
);
@@ -177,7 +179,8 @@ impl Checker for JavascriptCode {
177179
Function,
178180
GeneratorFunction,
179181
FunctionDeclaration,
180-
GeneratorFunctionDeclaration
182+
GeneratorFunctionDeclaration,
183+
MethodDefinition
181184
);
182185
mk_checker!(
183186
is_func_space,
@@ -186,6 +189,7 @@ impl Checker for JavascriptCode {
186189
GeneratorFunction,
187190
Class,
188191
FunctionDeclaration,
192+
MethodDefinition,
189193
GeneratorFunctionDeclaration,
190194
ClassDeclaration
191195
);
@@ -200,7 +204,8 @@ impl Checker for TypescriptCode {
200204
Function,
201205
GeneratorFunction,
202206
FunctionDeclaration,
203-
GeneratorFunctionDeclaration
207+
GeneratorFunctionDeclaration,
208+
MethodDefinition
204209
);
205210
mk_checker!(
206211
is_func_space,
@@ -209,6 +214,7 @@ impl Checker for TypescriptCode {
209214
Class,
210215
GeneratorFunction,
211216
FunctionDeclaration,
217+
MethodDefinition,
212218
GeneratorFunctionDeclaration,
213219
ClassDeclaration
214220
);
@@ -223,7 +229,8 @@ impl Checker for TsxCode {
223229
Function,
224230
GeneratorFunction,
225231
FunctionDeclaration,
226-
GeneratorFunctionDeclaration
232+
GeneratorFunctionDeclaration,
233+
MethodDefinition
227234
);
228235
mk_checker!(
229236
is_func_space,
@@ -232,6 +239,7 @@ impl Checker for TsxCode {
232239
GeneratorFunction,
233240
Class,
234241
FunctionDeclaration,
242+
MethodDefinition,
235243
GeneratorFunction,
236244
GeneratorFunctionDeclaration,
237245
ClassDeclaration

src/getter.rs

Lines changed: 132 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,44 @@ impl Getter for MozjsCode {
4343

4444
let typ = node.kind_id();
4545
match typ.into() {
46-
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
47-
NodeKind::Function
48-
}
46+
Function
47+
| MethodDefinition
48+
| GeneratorFunction
49+
| FunctionDeclaration
50+
| GeneratorFunctionDeclaration => NodeKind::Function,
4951
Class | ClassDeclaration => NodeKind::Class,
5052
Program => NodeKind::Unit,
5153
_ => NodeKind::Unknown,
5254
}
5355
}
56+
57+
fn get_func_space_name<'a>(node: &Node, code: &'a [u8]) -> Option<&'a str> {
58+
if let Some(name) = node.child_by_field_name("name") {
59+
let code = &code[name.start_byte()..name.end_byte()];
60+
std::str::from_utf8(code).ok()
61+
} else {
62+
// We can be in a pair: foo: function() {}
63+
// Or in a variable declaration: var aFun = function() {}
64+
if let Some(parent) = node.parent() {
65+
match parent.kind_id().into() {
66+
Mozjs::Pair => {
67+
if let Some(name) = parent.child_by_field_name("key") {
68+
let code = &code[name.start_byte()..name.end_byte()];
69+
return std::str::from_utf8(code).ok();
70+
}
71+
}
72+
Mozjs::VariableDeclarator => {
73+
if let Some(name) = parent.child_by_field_name("name") {
74+
let code = &code[name.start_byte()..name.end_byte()];
75+
return std::str::from_utf8(code).ok();
76+
}
77+
}
78+
_ => {}
79+
}
80+
}
81+
Some("<anonymous>")
82+
}
83+
}
5484
}
5585

5686
impl Getter for JavascriptCode {
@@ -59,14 +89,44 @@ impl Getter for JavascriptCode {
5989

6090
let typ = node.kind_id();
6191
match typ.into() {
62-
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
63-
NodeKind::Function
64-
}
92+
Function
93+
| MethodDefinition
94+
| GeneratorFunction
95+
| FunctionDeclaration
96+
| GeneratorFunctionDeclaration => NodeKind::Function,
6597
Class | ClassDeclaration => NodeKind::Class,
6698
Program => NodeKind::Unit,
6799
_ => NodeKind::Unknown,
68100
}
69101
}
102+
103+
fn get_func_space_name<'a>(node: &Node, code: &'a [u8]) -> Option<&'a str> {
104+
if let Some(name) = node.child_by_field_name("name") {
105+
let code = &code[name.start_byte()..name.end_byte()];
106+
std::str::from_utf8(code).ok()
107+
} else {
108+
// We can be in a pair: foo: function() {}
109+
// Or in a variable declaration: var aFun = function() {}
110+
if let Some(parent) = node.parent() {
111+
match parent.kind_id().into() {
112+
Mozjs::Pair => {
113+
if let Some(name) = parent.child_by_field_name("key") {
114+
let code = &code[name.start_byte()..name.end_byte()];
115+
return std::str::from_utf8(code).ok();
116+
}
117+
}
118+
Mozjs::VariableDeclarator => {
119+
if let Some(name) = parent.child_by_field_name("name") {
120+
let code = &code[name.start_byte()..name.end_byte()];
121+
return std::str::from_utf8(code).ok();
122+
}
123+
}
124+
_ => {}
125+
}
126+
}
127+
Some("<anonymous>")
128+
}
129+
}
70130
}
71131

72132
impl Getter for TypescriptCode {
@@ -75,14 +135,44 @@ impl Getter for TypescriptCode {
75135

76136
let typ = node.kind_id();
77137
match typ.into() {
78-
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
79-
NodeKind::Function
80-
}
138+
Function
139+
| MethodDefinition
140+
| GeneratorFunction
141+
| FunctionDeclaration
142+
| GeneratorFunctionDeclaration => NodeKind::Function,
81143
Class | ClassDeclaration => NodeKind::Class,
82144
Program => NodeKind::Unit,
83145
_ => NodeKind::Unknown,
84146
}
85147
}
148+
149+
fn get_func_space_name<'a>(node: &Node, code: &'a [u8]) -> Option<&'a str> {
150+
if let Some(name) = node.child_by_field_name("name") {
151+
let code = &code[name.start_byte()..name.end_byte()];
152+
std::str::from_utf8(code).ok()
153+
} else {
154+
// We can be in a pair: foo: function() {}
155+
// Or in a variable declaration: var aFun = function() {}
156+
if let Some(parent) = node.parent() {
157+
match parent.kind_id().into() {
158+
Mozjs::Pair => {
159+
if let Some(name) = parent.child_by_field_name("key") {
160+
let code = &code[name.start_byte()..name.end_byte()];
161+
return std::str::from_utf8(code).ok();
162+
}
163+
}
164+
Mozjs::VariableDeclarator => {
165+
if let Some(name) = parent.child_by_field_name("name") {
166+
let code = &code[name.start_byte()..name.end_byte()];
167+
return std::str::from_utf8(code).ok();
168+
}
169+
}
170+
_ => {}
171+
}
172+
}
173+
Some("<anonymous>")
174+
}
175+
}
86176
}
87177

88178
impl Getter for TsxCode {
@@ -91,14 +181,44 @@ impl Getter for TsxCode {
91181

92182
let typ = node.kind_id();
93183
match typ.into() {
94-
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
95-
NodeKind::Function
96-
}
184+
Function
185+
| MethodDefinition
186+
| GeneratorFunction
187+
| FunctionDeclaration
188+
| GeneratorFunctionDeclaration => NodeKind::Function,
97189
Class | ClassDeclaration => NodeKind::Class,
98190
Program => NodeKind::Unit,
99191
_ => NodeKind::Unknown,
100192
}
101193
}
194+
195+
fn get_func_space_name<'a>(node: &Node, code: &'a [u8]) -> Option<&'a str> {
196+
if let Some(name) = node.child_by_field_name("name") {
197+
let code = &code[name.start_byte()..name.end_byte()];
198+
std::str::from_utf8(code).ok()
199+
} else {
200+
// We can be in a pair: foo: function() {}
201+
// Or in a variable declaration: var aFun = function() {}
202+
if let Some(parent) = node.parent() {
203+
match parent.kind_id().into() {
204+
Mozjs::Pair => {
205+
if let Some(name) = parent.child_by_field_name("key") {
206+
let code = &code[name.start_byte()..name.end_byte()];
207+
return std::str::from_utf8(code).ok();
208+
}
209+
}
210+
Mozjs::VariableDeclarator => {
211+
if let Some(name) = parent.child_by_field_name("name") {
212+
let code = &code[name.start_byte()..name.end_byte()];
213+
return std::str::from_utf8(code).ok();
214+
}
215+
}
216+
_ => {}
217+
}
218+
}
219+
Some("<anonymous>")
220+
}
221+
}
102222
}
103223

104224
impl Getter for RustCode {

0 commit comments

Comments
 (0)