0

Let's say, for example, I have a file like this:

function foo() {
    const a = "hello";
    return a;
}

function bar() {
    console.log("there");
}

I want to write something that could print the output like this:

functions:
foo
bar

consts/variables:
foo.a // format here doesn't matter really, just illustrating

Is there any way I could do this without modifying the original file in pure js? Or is text parsing the only real way to go here?

  • 1
    You're absolutely going to need to something like an AST parser to do this efficiently. You don't need to modify the original file though. – JaredMcAteer May 15 '22 at 14:13
  • 1
    You can refer this similar thread https://stackoverflow.com/questions/11279441/return-all-of-the-functions-that-are-defined-in-a-javascript-file – Kaashan May 15 '22 at 14:26
  • Well you get the original file as text, right? What else would you do with the file other than reading it? So yes, you must parse the source code into an AST. – Bergi May 15 '22 at 15:03
  • I get the original file as a regular .js file if that makes a difference. – brandonknewman May 15 '22 at 15:57

0 Answers0