SlimIt 0.2 is out with a JavaScript parser, pretty printer and a node visitor

by Ruslan Spivak on May 7, 2011

SlimIt 0.2 (should be also accessible via http://slimit.org) got a JavaScript parser, pretty printer and a node visitor.

Let’s iterate over, modify a JavaScript AST and pretty print it:

>>> from slimit.parser import Parser
>>> from slimit.visitors import nodevisitor
>>> from slimit import ast
>>>
>>> parser = Parser()
>>> tree = parser.parse('for(var i=0; i<10; i++) {var x=5+i;}')
>>> for node in nodevisitor.visit(tree):
...     if isinstance(node, ast.Identifier) and node.value == 'i':
...         node.value = 'hello'
...
>>> print tree.to_ecma() # print awesome javascript :)
for (var hello = 0; hello < 10; hello++) {
  var x = 5 + hello;
}
>>>

SlimIt comes with a bunch of tests and I also used it to parse and pretty print jQuery 1.5.2 which I then tested in a browser and it worked fine. But, you know, software is not perfect :) If you find any issues, please, submit them at GitHub.

Enjoy.

If you enjoyed this post why not subscribe via email or my RSS feed and get the latest updates immediately. You can also follow me on GitHub or Twitter.

Speak your mind

Previous post:

Next post: