Allow option of raw AST output
authorNick Downing <nick@ndcode.org>
Fri, 14 Jan 2022 10:54:46 +0000 (21:54 +1100)
committerNick Downing <nick@ndcode.org>
Fri, 14 Jan 2022 10:54:46 +0000 (21:54 +1100)
cli.js
jst.js

diff --git a/cli.js b/cli.js
index 0e2c3ce..86e2f25 100755 (executable)
--- a/cli.js
+++ b/cli.js
@@ -8,8 +8,8 @@ commander
   .version('0.1.0', '-v, --version')
   .option('-s, --signature <str>', 'function signature for --wrap', 'async (_require, _pathname, _root)')
   .option('-b, --bare-returns', 'allow return in top level code', false)
-  .option('-o, --output <astring|uglify>', 'use astring or UglifyJS for output', 'astring')
-  .option('-i, --indent <n>', 'indent per level (astring only)', '2')
+  .option('-o, --output <ast|astring|uglify>', 'output AST, or via astring/UglifyJS', 'astring')
+  .option('-i, --indent <n>', 'indent per level (ast/astring only)', '2')
   .option('-j, --initial-indent <n>', 'initial indent (astring only)', '0')
   .option('-c, --compress', 'compress the logic (UglifyJS only)', true)
   .option('-m, --mangle', 'mangle symbol names (UglifyJS only)', true)
@@ -26,6 +26,9 @@ fs.writeSync(
     {
       bare_returns: commander.bareReturns,
       output: commander.output,
+      ast_options: {
+        indent: parseInt(commander.indent)
+      },
       astring_options: {
         indent: ' '.repeat(parseInt(commander.indent)),
         startingIndentLevel: parseInt(commander.initialIndent)
diff --git a/jst.js b/jst.js
index 156dfe5..da88339 100644 (file)
--- a/jst.js
+++ b/jst.js
@@ -33,6 +33,7 @@ let jst = (text, options) => {
       bare_returns: true,
       ecma_version: 'latest', // or 11, 12, 13, etc
       output: 'astring',
+      ast_options: {indent: 2},
       astring_options: {indent: '  ', startingIndentLevel: 0},
       uglify_options: {compress: true, mangle: true}
     },
@@ -48,6 +49,8 @@ let jst = (text, options) => {
       }
     )
   )
+  if (options.output === 'ast')
+    return JSON.stringify(ast, null, options.ast_options.indent)
   if (options.output === 'astring')
     return astring.generate(ast, options.astring_options)
   let render = uglify_js.minify(