0

I'm looking for a way to get a function declaration body by name from a string of js code. I'm in Nodejs environment. Let's say I have some spaghetti.js file. I can read it into a string

const allJs = fs.readFileSync('spaghetti.js');

Now I need a function that receives that string and function name and returns a string with everything between { }.

Something like this

allJs = 'let a=1; const b=[2, 3]; function cook(items){return items}; cook(b)';
parseFunction(allJs, 'cook');//'return items'

The complexity of input js is not limited.

I tried to find an npm module for that, but no luck.

8
  • 1
    What is the second parameter ("cook") of your parseFunction for? Commented Apr 19, 2017 at 10:50
  • If the complexity of your input js is not limited, you should specify which function bodies you want. All, just the first, a special one, even nested ones, class methods, etc ...? Commented Apr 19, 2017 at 10:52
  • @Jamiec that's a function name to look for in the input code Commented Apr 19, 2017 at 10:52
  • right, but your string input already includes a call to cook. POssibly you meant for that to be return cook(b); no? Commented Apr 19, 2017 at 10:53
  • @dsuckau, let's say I only want top level function declarations. No classes, methods etc. Commented Apr 19, 2017 at 10:53

2 Answers 2

2

You should have a look at an AST parser for Javascript:

http://esprima.org/

https://github.com/ternjs/acorn

That should be more safe than using RegExp or something.

Sign up to request clarification or add additional context in comments.

Comments

0

A String can be evaluated locally with the native eval() method. But remember, eval is a form of evil!

If the parseFunction() above is relying on something like this then the global Function constructor is being used and the 'new' function is bound to the return value of that operation (and thus that return value itself needs to be called).

A simple way to achieve this might be to do something like this...

var funcString = 'var a = 1, b = 3;';
funcString += 'function summit(){return a + b;}';
funcString += 'return summit();';

function makeNewFunc(str) {
    return new Function(str);
}

var newFunc = makeNewFunc( funcString );

console.log('newFunc:',newFunc);
//-> newFunc: function anonymous()

console.log('newFunc():',newFunc());
//-> newFunc(): 4

This demonstrates how functions can be created and invoked from String snippets. (EDIT: Turning something like that into a Node module is a simple matter);

Hope that helped. :)

2 Comments

It seems you haven't understood my problem. I don't want to invoke anything. I just want to get a STRING representing a particular function's body. A simple text. You can think of it as some form of static code analysis.
@Andrey - I took your question, it's title, and the proposed parseFunction function to mean you were looking to, you know, parse a function from a string. If you want to extract a string representation of a function's body then you're after "function decompilation" - see this SO answer: stackoverflow.com/a/3179967/4746328

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.