Skip to content

Commit

Permalink
Get methods as functions in JS
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Dec 9, 2019
1 parent 90ca554 commit d1e6398
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 16 deletions.
16 changes: 12 additions & 4 deletions src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ impl Checker for MozjsCode {
Function,
GeneratorFunction,
FunctionDeclaration,
GeneratorFunctionDeclaration
GeneratorFunctionDeclaration,
MethodDefinition
);
mk_checker!(
is_func_space,
Expand All @@ -163,6 +164,7 @@ impl Checker for MozjsCode {
Class,
GeneratorFunction,
FunctionDeclaration,
MethodDefinition,
GeneratorFunctionDeclaration,
ClassDeclaration
);
Expand All @@ -177,7 +179,8 @@ impl Checker for JavascriptCode {
Function,
GeneratorFunction,
FunctionDeclaration,
GeneratorFunctionDeclaration
GeneratorFunctionDeclaration,
MethodDefinition
);
mk_checker!(
is_func_space,
Expand All @@ -186,6 +189,7 @@ impl Checker for JavascriptCode {
GeneratorFunction,
Class,
FunctionDeclaration,
MethodDefinition,
GeneratorFunctionDeclaration,
ClassDeclaration
);
Expand All @@ -200,7 +204,8 @@ impl Checker for TypescriptCode {
Function,
GeneratorFunction,
FunctionDeclaration,
GeneratorFunctionDeclaration
GeneratorFunctionDeclaration,
MethodDefinition
);
mk_checker!(
is_func_space,
Expand All @@ -209,6 +214,7 @@ impl Checker for TypescriptCode {
Class,
GeneratorFunction,
FunctionDeclaration,
MethodDefinition,
GeneratorFunctionDeclaration,
ClassDeclaration
);
Expand All @@ -223,7 +229,8 @@ impl Checker for TsxCode {
Function,
GeneratorFunction,
FunctionDeclaration,
GeneratorFunctionDeclaration
GeneratorFunctionDeclaration,
MethodDefinition
);
mk_checker!(
is_func_space,
Expand All @@ -232,6 +239,7 @@ impl Checker for TsxCode {
GeneratorFunction,
Class,
FunctionDeclaration,
MethodDefinition,
GeneratorFunction,
GeneratorFunctionDeclaration,
ClassDeclaration
Expand Down
144 changes: 132 additions & 12 deletions src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,44 @@ impl Getter for MozjsCode {

let typ = node.kind_id();
match typ.into() {
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
NodeKind::Function
}
Function
| MethodDefinition
| GeneratorFunction
| FunctionDeclaration
| GeneratorFunctionDeclaration => NodeKind::Function,
Class | ClassDeclaration => NodeKind::Class,
Program => NodeKind::Unit,
_ => NodeKind::Unknown,
}
}

fn get_func_space_name<'a>(node: &Node, code: &'a [u8]) -> Option<&'a str> {
if let Some(name) = node.child_by_field_name("name") {
let code = &code[name.start_byte()..name.end_byte()];
std::str::from_utf8(code).ok()
} else {
// We can be in a pair: foo: function() {}
// Or in a variable declaration: var aFun = function() {}
if let Some(parent) = node.parent() {
match parent.kind_id().into() {
Mozjs::Pair => {
if let Some(name) = parent.child_by_field_name("key") {
let code = &code[name.start_byte()..name.end_byte()];
return std::str::from_utf8(code).ok();
}
}
Mozjs::VariableDeclarator => {
if let Some(name) = parent.child_by_field_name("name") {
let code = &code[name.start_byte()..name.end_byte()];
return std::str::from_utf8(code).ok();
}
}
_ => {}
}
}
Some("<anonymous>")
}
}
}

impl Getter for JavascriptCode {
Expand All @@ -59,14 +89,44 @@ impl Getter for JavascriptCode {

let typ = node.kind_id();
match typ.into() {
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
NodeKind::Function
}
Function
| MethodDefinition
| GeneratorFunction
| FunctionDeclaration
| GeneratorFunctionDeclaration => NodeKind::Function,
Class | ClassDeclaration => NodeKind::Class,
Program => NodeKind::Unit,
_ => NodeKind::Unknown,
}
}

fn get_func_space_name<'a>(node: &Node, code: &'a [u8]) -> Option<&'a str> {
if let Some(name) = node.child_by_field_name("name") {
let code = &code[name.start_byte()..name.end_byte()];
std::str::from_utf8(code).ok()
} else {
// We can be in a pair: foo: function() {}
// Or in a variable declaration: var aFun = function() {}
if let Some(parent) = node.parent() {
match parent.kind_id().into() {
Mozjs::Pair => {
if let Some(name) = parent.child_by_field_name("key") {
let code = &code[name.start_byte()..name.end_byte()];
return std::str::from_utf8(code).ok();
}
}
Mozjs::VariableDeclarator => {
if let Some(name) = parent.child_by_field_name("name") {
let code = &code[name.start_byte()..name.end_byte()];
return std::str::from_utf8(code).ok();
}
}
_ => {}
}
}
Some("<anonymous>")
}
}
}

impl Getter for TypescriptCode {
Expand All @@ -75,14 +135,44 @@ impl Getter for TypescriptCode {

let typ = node.kind_id();
match typ.into() {
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
NodeKind::Function
}
Function
| MethodDefinition
| GeneratorFunction
| FunctionDeclaration
| GeneratorFunctionDeclaration => NodeKind::Function,
Class | ClassDeclaration => NodeKind::Class,
Program => NodeKind::Unit,
_ => NodeKind::Unknown,
}
}

fn get_func_space_name<'a>(node: &Node, code: &'a [u8]) -> Option<&'a str> {
if let Some(name) = node.child_by_field_name("name") {
let code = &code[name.start_byte()..name.end_byte()];
std::str::from_utf8(code).ok()
} else {
// We can be in a pair: foo: function() {}
// Or in a variable declaration: var aFun = function() {}
if let Some(parent) = node.parent() {
match parent.kind_id().into() {
Mozjs::Pair => {
if let Some(name) = parent.child_by_field_name("key") {
let code = &code[name.start_byte()..name.end_byte()];
return std::str::from_utf8(code).ok();
}
}
Mozjs::VariableDeclarator => {
if let Some(name) = parent.child_by_field_name("name") {
let code = &code[name.start_byte()..name.end_byte()];
return std::str::from_utf8(code).ok();
}
}
_ => {}
}
}
Some("<anonymous>")
}
}
}

impl Getter for TsxCode {
Expand All @@ -91,14 +181,44 @@ impl Getter for TsxCode {

let typ = node.kind_id();
match typ.into() {
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
NodeKind::Function
}
Function
| MethodDefinition
| GeneratorFunction
| FunctionDeclaration
| GeneratorFunctionDeclaration => NodeKind::Function,
Class | ClassDeclaration => NodeKind::Class,
Program => NodeKind::Unit,
_ => NodeKind::Unknown,
}
}

fn get_func_space_name<'a>(node: &Node, code: &'a [u8]) -> Option<&'a str> {
if let Some(name) = node.child_by_field_name("name") {
let code = &code[name.start_byte()..name.end_byte()];
std::str::from_utf8(code).ok()
} else {
// We can be in a pair: foo: function() {}
// Or in a variable declaration: var aFun = function() {}
if let Some(parent) = node.parent() {
match parent.kind_id().into() {
Mozjs::Pair => {
if let Some(name) = parent.child_by_field_name("key") {
let code = &code[name.start_byte()..name.end_byte()];
return std::str::from_utf8(code).ok();
}
}
Mozjs::VariableDeclarator => {
if let Some(name) = parent.child_by_field_name("name") {
let code = &code[name.start_byte()..name.end_byte()];
return std::str::from_utf8(code).ok();
}
}
_ => {}
}
}
Some("<anonymous>")
}
}
}

impl Getter for RustCode {
Expand Down

0 comments on commit d1e6398

Please sign in to comment.