-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a method of dumping the AST #769
Conversation
@mishoo @vjeux @fabiosantoscode All comments are welcome, pretty sure I missed some stuff. |
Nice, this can be really useful for debugging :) |
I don't know what you might have missed, this patch touches stuff I'm not familiar with. I'm sure others will be able to better review this |
@rvanvelzen - Looks good. I personally would prefer if the AST prop order is significant. For example, it is easier to debug large Binary expressions if the operator type is seen first in the tree. It would avoid a lot of scrolling and parentheses matching to see what's going on: diff --git a/lib/ast.js b/lib/ast.js
index 203beef..3a1b344 100644
--- a/lib/ast.js
+++ b/lib/ast.js
@@ -79,7 +79,7 @@ function DEFNODE(type, props, methods, base) {
if (base) base.SUBCLASSES.push(ctor);
ctor.prototype.CTOR = ctor;
ctor.PROPS = props || null;
- ctor.DUMP_PROPS = dump_props.sort();
+ ctor.DUMP_PROPS = dump_props;
ctor.SELF_PROPS = self_props;
ctor.SUBCLASSES = [];
if (type) {
@@ -706,7 +706,7 @@ var AST_UnaryPostfix = DEFNODE("UnaryPostfix", null, {
$documentation: "Unary postfix expression, i.e. `i++`"
}, AST_Unary);
-var AST_Binary = DEFNODE("Binary", "left operator right", {
+var AST_Binary = DEFNODE("Binary", "operator left right", {
$documentation: "Binary expression, i.e. `a + b`",
$propdoc: {
left: "[AST_Node] left-hand side expression", |
I suspect it wouldn't be very much work to import and export UglifyJS2 trees in this "native" JSON AST form. |
Sorry for chiming in so late, but why not using |
My comment about import/export aside, this feature is to more to debug UglifyJS2 itself or learn about its internal structure in order to create new optimizations. A different command line flag |
You're right, for using it in tests that's way more appropriate. I'm not entirely sure what the right direction would be to dump the internal AST. |
No reason not to support both internal AST and mozilla-style AST with different command line flags as they each serve a different purpose. |
When I cherry picked this pull request and applied it to my local latest UglifyJS2 master and ran But once I reverted this part of the patch,
|
What did the failure in test/compress/new.js look like? |
It output the AST instead of the code. I'm assuming that the test code wasn't exercised since reverting test/run-tests.js seemed to resolve the issue. |
It would be nice to get this PR merged to master as it's super useful for debugging uglify issues. |
+1 for that. I've been adding lots of syntax, and it's been quite hard to see if I'm doing it right, I have to throw sand and parentheses at stuff. |
Tests using |
d7ef0fc
to
e217729
Compare
I have maintained my own version here: https://github.com/avdg/UglifyJS2/blob/ast-dump/bin/uglifyjs |
e217729
to
51e944b
Compare
I've just rebased this to be pushed to master. It probably needs some work to work with harmony properly. @avdg are there any modifications in your tree that would be worthwhile to have? |
It was a bit cleaned up and updated to the new branch (there were some conflicts at the time). Though it has been a long time ago (and maybe the mess is gone by now, but I didn't check the new patch just yet). |
And yes, there needs to be some fixing for harmony, bit tricky to maintain (need to keep track what happens on a ast-tree structure that isn't stable yet, also the code is currently maintained separately as this pr isn't merged) but the patch should be fairly obvious once the patch mechanics are understood. |
closes mishoo#769
Re-order `AST_Binary` properties to make dump more readable. closes #769
This also includes using the dumped AST to verify test results. This should be slightly safer.