Tools like acorn[1] and Esprima[2] can parse the JavaScript source and output an ESTree compliant syntax tree.
Then you can traverse it and modify it like any other AST.
[1] https://github.com/ternjs/acorn [2] http://esprima.org/ [3] https://github.com/estree/estree