2

I would like to delete a function form javascript at a position in a js source file, and re-write the file without it, so...

I have used a code analysis tool to get the position of the target function for deletion as line 5, column 0, in myfile.js

1 function keepme(x) {
2   return x + ' is ok';
3 }
4 
5 function delme(y) {
6   return y + ' oh why';
7 }
8 
9 /* code continues here */

I want to now use some kind of parser I guess in order to remove the function delme from the file.

I have looked at UglifyJS's build in parser, and treewalker, but seems to report the position of function delme(y), not the whole function, so can't figure out how to find end point of function definition.


 For posterity...

var recast = require("recast");
var fs = require("fs");
var code = fs.readFileSync("billymoon.js").toString();

function stripByPosition(code, line, col) {
  
  var ast = recast.parse(code);

    recast.visit(ast, {
        visitFunctionDeclaration: function(path) {
            if (path.node.loc.start.line === line && path.node.loc.start.column === col) {
                path.prune();
            }
            return false;
        }
    });

  return recast.print(ast).code;

}

console.log(stripByPosition(code, 5, 0));
console.log(stripByPosition(code, 1, 0));
7
  • Just to clarify, do you want to premanently delete contents from the file, or simply during execution (temporary removal)? Commented Sep 14, 2015 at 20:16
  • 2
    Parse the file line by line and count the curly braces. Increment a counter on "{" and decrement on "}". When you reach 0, you will have parsed the whole function. Commented Sep 14, 2015 at 20:16
  • Closure Compiler can remove unused functions, if that what you mean Commented Sep 14, 2015 at 20:18
  • @JeffNoel I want to parse a js file on file system, then rewrite it with function removed - not during execution, but as static code analysis tool. Commented Sep 14, 2015 at 20:19
  • 1
    @JeffNoel I think it must be done in a parser, otherwise there could be oddly matched brackets in strings, regex, or comments. Commented Sep 14, 2015 at 20:47

1 Answer 1

3

Maybe you would like to use the recast tool for this. Here's an example.

var recast = require("recast");
var fs = require("fs");
var code = fs.readFileSync("billymoon.js").toString();
var ast = recast.parse(code);

recast.visit(ast, {
  visitFunctionDeclaration: function (path) {
    var start = path.node.loc.start;
    if (start.line === 5 && start.column === 0) {
      path.prune();
    }
    return false;
  }
});

console.log(recast.print(ast).code);
Sign up to request clarification or add additional context in comments.

3 Comments

looks interesting, but would need to identify function by line/col in source, as that is what I have from parser. Function might be anonymous function, or there could be two functions with the same name etc... I had a look through recast docs, and looks great - but did not see how to identify code from line/col
Having looked into it a bit more, looks like recast does report location of nodes in terms of line/col in it's loc property. Just need to use that to detect if it is correct function I guess...
Yeah, sorry. I misread your question. You can replace the condition with path.node.loc.start.line === 5. And I should do that too.

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.