Skip to content

Commit

Permalink
Add support for arrow funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
a-krikorian committed May 14, 2022
1 parent 92f33a0 commit 36fe79f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
5 changes: 4 additions & 1 deletion code-parser-module/constants/astObjectTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ const AST_OBJECT_TYPES = {
VARIABLE_DECLARATION: "VariableDeclaration",
RETURN_STATEMENT:"ReturnStatement",
BINARY_EXPRESSION:"BinaryExpression",
IDENTIFIER:"Identifier"
IDENTIFIER:"Identifier",
EXPRESSION_STATEMENT:"ExpressionStatement",
ARROW_FUNCTION_EXPRESSION:"ArrowFunctionExpression",

}

module.exports = Object.freeze(AST_OBJECT_TYPES)
32 changes: 21 additions & 11 deletions code-parser-module/domain/FunctionAst.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,43 @@ const AstObjectTypesParser = require("../helpers/AstObjectTypesParser");
class FunctionAst {
constructor(ast) {
this.ast = ast;
this.codeParsedObj = this.ast && this.ast.program && this.ast.program.body && this.ast.program.body[0];
this.codeParsedObj = (this.isRegularFunction())
? this.ast && this.ast.program && this.ast.program.body && this.ast.program.body[0]
: this.ast && this.ast.program && this.ast.program.body && this.ast.program.body[0] && this.ast.program.body[0].expression
}

isFunction() {
return this.codeParsedObj && this.codeParsedObj.type === AST_OBJECT_TYPES.FUNCTION_DECLARATION;
return this.isRegularFunction() || this.isArrowFunction();
}

isRegularFunction() {
return (this.codeParsedObj && this.codeParsedObj.type === AST_OBJECT_TYPES.FUNCTION_DECLARATION) ? true : false;
}

isArrowFunction() {
return (this.codeParsedObj && this.codeParsedObj.type === AST_OBJECT_TYPES.ARROW_FUNCTION_EXPRESSION) ? true : false;
}

getFunctionName() {
return this.codeParsedObj.id && this.codeParsedObj.id.name;
return (this.isRegularFunction()) ? this.codeParsedObj.id && this.codeParsedObj.id.name : "";
}

getFunctionArgs(){
const params = this.codeParsedObj && this.codeParsedObj.params || [];
return params.map(p => new FunctionParam(p.type, p.name));
getFunctionArgs() {
let args = this.codeParsedObj && this.codeParsedObj.params || [];;
return args.map(p => new FunctionParam(p.type, p.name));
}

getFunctionType(){
if(this.codeParsedObj && this.codeParsedObj.async === true) return FUNCTION_TYPES.ASYNC;
getFunctionType() {
if (this.codeParsedObj && this.codeParsedObj.async === true) return FUNCTION_TYPES.ASYNC;
return FUNCTION_TYPES.SYNC;
}

getFunctionBody(){
getFunctionBody() {
const bodyStatements = this.codeParsedObj && this.codeParsedObj.body && this.codeParsedObj.body.body || [];
return bodyStatements.map(statement => {
if(statement.type === AST_OBJECT_TYPES.VARIABLE_DECLARATION){
if (statement.type === AST_OBJECT_TYPES.VARIABLE_DECLARATION) {
return AstObjectTypesParser.variableDeclarationsParser(statement);
}else if(statement.type === AST_OBJECT_TYPES.RETURN_STATEMENT){
} else if (statement.type === AST_OBJECT_TYPES.RETURN_STATEMENT) {
return AstObjectTypesParser.returnStatementParser(statement);
}
});
Expand Down
2 changes: 1 addition & 1 deletion code-parser-module/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Parser {
}

let func = Parser.parse([
"function add(a, b) {",
"(a, b) => {",
"let x = a+b;",
" return x;",
"}"
Expand Down

0 comments on commit 36fe79f

Please sign in to comment.