support JSON dump beyond `AST_Toplevel` (#4173)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 3 Oct 2020 14:53:06 +0000 (15:53 +0100)
committerGitHub <noreply@github.com>
Sat, 3 Oct 2020 14:53:06 +0000 (22:53 +0800)
bin/uglifyjs

index da2528d..c606265 100755 (executable)
@@ -342,7 +342,18 @@ function run() {
         }
         fatal(ex);
     } else if (output == "ast") {
-        if (!options.compress && !options.mangle) result.ast.figure_out_scope({});
+        if (!options.compress && !options.mangle) {
+            var toplevel = result.ast;
+            if (!(toplevel instanceof UglifyJS.AST_Toplevel)) {
+                if (!(toplevel instanceof UglifyJS.AST_Statement)) toplevel = new UglifyJS.AST_SimpleStatement({
+                    body: toplevel,
+                });
+                toplevel = new UglifyJS.AST_Toplevel({
+                    body: [ toplevel ],
+                });
+            }
+            toplevel.figure_out_scope({});
+        }
         print(JSON.stringify(result.ast, function(key, value) {
             if (value) switch (key) {
               case "thedef":