diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-03-17 05:51:52 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-17 13:51:52 +0800 |
commit | 7d595e2eac14022a66ad501707f3e820b87a617d (patch) | |
tree | fd3008216607ecafd555f20ccdb289d0c3e3646e /lib | |
parent | 9fc0ff5953ffaebffa179284f82bbb01fb9ada3e (diff) | |
download | tracifyjs-7d595e2eac14022a66ad501707f3e820b87a617d.tar.gz tracifyjs-7d595e2eac14022a66ad501707f3e820b87a617d.zip |
improve comment formatting logic (#4794)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/output.js | 81 |
1 files changed, 30 insertions, 51 deletions
diff --git a/lib/output.js b/lib/output.js index e6b49804..fef57bce 100644 --- a/lib/output.js +++ b/lib/output.js @@ -347,6 +347,7 @@ function OutputStream(options) { }; var indent = options.beautify ? function(half) { + if (need_newline_indented) print("\n"); print(repeat_string(" ", half ? indentation - (options.indent_level >> 1) : indentation)); } : noop; @@ -450,6 +451,27 @@ function OutputStream(options) { return /^ *$/.test(OUTPUT.slice(index + 1)); } + function pad_comment(token, force) { + if (need_newline_indented) return; + if (token.nlb && (force || !has_nlb())) { + need_newline_indented = true; + } else if (force) { + need_space = true; + } + } + + function print_comment(comment) { + var value = comment.value.replace(/[@#]__PURE__/g, " "); + if (/^\s*$/.test(value) && !/^\s*$/.test(comment.value)) return false; + if (/comment[134]/.test(comment.type)) { + print("//" + value); + need_newline_indented = true; + } else if (comment.type == "comment2") { + print("/*" + value + "*/"); + } + return true; + } + function prepend_comments(node) { var self = this; var scan = node instanceof AST_Exit && node.value; @@ -489,37 +511,12 @@ function OutputStream(options) { } comments = comments.filter(comment_filter, node); - if (comments.length == 0) return; - var last_nlb = has_nlb(); - comments.forEach(function(c, i) { - if (!last_nlb) { - if (c.nlb) { - print("\n"); - indent(); - last_nlb = true; - } else if (i > 0) { - space(); - } - } - var value = c.value.replace(/[@#]__PURE__/g, " "); - if (/^\s*$/.test(value)) return; - if (/comment[134]/.test(c.type)) { - print("//" + value + "\n"); - indent(); - last_nlb = true; - } else if (c.type == "comment2") { - print("/*" + value + "*/"); - last_nlb = false; - } + var printed = false; + comments.forEach(function(comment, index) { + pad_comment(comment, index); + if (print_comment(comment)) printed = true; }); - if (!last_nlb) { - if (node.start.nlb) { - print("\n"); - indent(); - } else { - space(); - } - } + if (printed) pad_comment(node.start, true); function dump(node) { var token = node.start; @@ -549,27 +546,9 @@ function OutputStream(options) { }))) return; comments._dumped = self; var insert = OUTPUT.length; - comments.filter(comment_filter, node).forEach(function(c, i) { - need_space = false; - if (need_newline_indented) { - print("\n"); - indent(); - need_newline_indented = false; - } else if (c.nlb && (i > 0 || !has_nlb())) { - print("\n"); - indent(); - } else if (i > 0 || !tail) { - space(); - } - var value = c.value.replace(/[@#]__PURE__/g, " "); - if (/^\s*$/.test(value)) return; - if (/comment[134]/.test(c.type)) { - print("//" + value); - need_newline_indented = true; - } else if (c.type == "comment2") { - print("/*" + value + "*/"); - need_space = true; - } + comments.filter(comment_filter, node).forEach(function(comment, index) { + pad_comment(comment, index || !tail); + print_comment(comment); }); if (OUTPUT.length > insert) newline_insert = insert; } |