escodegen
escodegen copied to clipboard
Add option to generate code that respects 'loc' info
I currently use esprima + some traversal code that I wrote to transform JS syntax from A -> B. I'd much prefer to simply modify the AST from esprima and feed it to escodegen (rather than mutate a string buffer of the original code as I traverse the tree and find nodes to be transformed), however I really need line numbers to be preserved post-transformation (for purposes of making stack traces sane in prod). Source maps work great in a dev environment, but they're too large to ship in prod for error logging.
Currently escodegen does its own formatting and doesn't attempt to follow line numbers output from esprima when the 'loc' option is turned on, so I decided to see what it would take to add an option do this and I have a [partially complete] branch with several types of statements supported here:
https://github.com/jeffmo/escodegen/tree/preserveLocInfo
I haven't finished it yet because I wanted to check in here to see if this is a change you'd be willing to take if I went through with finishing it out? It adds quite a bit of complexity to the code (byte size will likely increase significantly), but it's toggle-able via an option so perf should only be impacted when the option is on.
Looks great! Controlling the format of output code with loc information is very interesting :)
This would be great for ariya/esrefactor, which currently has to do what @jeffmo describes.
Awesome. Hopefully (time-permitting) I can have something more complete in the next week or two.
Hey folks I have started using this feature to add support source maps support for my wisp language. I'll probably submit individual bugs, but I'd like to share main roadblocks I've run into so far:
-
loc.end
does not seems to have much effect on generated maps. In my case this problem even more visible because I have location info on all nodes except primitive types like string and numbers, and since end location is ignored generated map often considers identifier followed by string as a one node. I really wish it was smarter about it. - I'm not sure yet, but I'm under impression that if several different nodes map to the same source location only first occurrence is taken into account. For example
(defn identity [x] x)
compiles tovar identity = function identity(x) return x };
and while I'd expectidentity
to be mapped to both locations, it only does to the first one.
That being said, I'm very graceful to this project & keep up a good work!
I guess I commented on wrong thread, which brings a question of how that formatting feature behave if node locations are based of non JS file but rather some other language.
+1
This is exactly what I need for the code transformer built into https://github.com/scriby/asyncblock
see also: https://github.com/benjamn/recast
It looks like this project isn't maintained anymore 😞 . Maybe I should switch to recast as mentioned above....
This is the exact behavior I need, are there any plans to merge this PR
It's not a PR, someone needs to implement it 😆