6

With Crockford's JSLINT, after calling JSLINT() to parse some JavaScript source, it provides access to the parse tree via JSLINT.tree

It would be really powerful to be able to analyse the tree, make structural changes to it based on the analysis and then generate new JavaScript from the modified tree.

Is there an open source project that builds on JSLINT by providing a conversion from a parse tree back to JavaScript?

(Is this even possible, i.e. does JSLINT keep complete enough information from the parse to round-trip everything significant?)

pmaruszczyk
  • 2,157
  • 2
  • 24
  • 49
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
  • what should that be good for? Can you provide a possible usecase? – user123444555621 Feb 04 '12 at 10:41
  • 1
    The possibilities are limitless. Think about ways you might enhance the semantics without modifying the syntax (which is good because it doesn't break existing editing tools). Here's a random example: suppose you say that if a function's parameters are named with a suffix `_` then they must not be `null` or `undefined`, and you'd like this to be automatically checked at runtime. It would be trivial to search the parse tree, find functions that declare such parameters, and insert runtime checks at the start of those functions. – Daniel Earwicker Feb 04 '12 at 11:24
  • Okay, but why would you construct the whole syntax tree in the first place? I haven't looked at the source of compressors like UglifyJS or YUICompressor, but I think they would provide a more sensible basis. – user123444555621 Feb 04 '12 at 12:45
  • 1. So you can perform analysis and modification on the whole tree. Why wouldn't you construct the whole syntax tree? 2. A quick look at YUICompressor's source shows that it uses the Parser class from Rhino, which builds a complete syntax tree. Don't know to what extent `YUICompressor` uses the information, but it could. – Daniel Earwicker Feb 04 '12 at 17:23

2 Answers2

3

I dont know if JSLint can do it (looking at some of the forum postings, it doesnt look like maintaining the tree is their goal but its a by-product for doing the linting process).

The uglifyjs project may help with generating an AST from javascript and re-converting a AST to code. See this method for AST to javascript.

rajasaur
  • 5,340
  • 2
  • 27
  • 22
1

Not open source, but does exactly what OP requests: our JavaScript Front End.

This front end is based on our DMS Software Reengineering Toolkit, which is general purpose customizable compiler technology useful for building arbitrary code analyzers and transformers. DMS has many front ends available for many languages (COBOL, Java, C++, ...)

In particular, DMS-based parsers (including its JavaScript one), collect complete information for regenerating the source tree, including column-start and end information for tokens, radix of numeric literals, differences in string quoting conventions, etc. to enable it to do fidelity printing where no transformations have been applied. DMS also provides many other useful services for tree processing such as visitors, symbol table construction support, flow analysis support, pattern matching, as well as source-to-source transformations.

It can provide all these services because the cost of engineering it has been amortized across many, many languages and applications.

We did it because as Daniel said, "the possibilities are limitless". He gives an example of instrumenting the code to watch for runtime failures; this kind of instrumentation is a very useful idea and we build test coverage tools based on exactly this thought.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • +1 as it looks like a great toolkit, probably way more powerful than I'm looking for and for my tinkering I'm obviously going to lean towards free libs. – Daniel Earwicker Feb 04 '12 at 17:27
  • What we find happens is that people start with small ideas, and then as they gain confidence, they build larger and larger projects. There's lots of scripting langauges that were intended originally for "just a few commands" in which people have built giant systems. As the ability to change code becomes more familiar, people will do more and more of it, and demand the kind of support that DMS brings. To the extent the "free libs" solution works for you, more power to you! But I'll bet you reconsider this position after a good healthy taste of what you can do :-} – Ira Baxter Feb 04 '12 at 17:45