From 7d595e2eac14022a66ad501707f3e820b87a617d Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 17 Mar 2021 05:51:52 +0000 Subject: improve comment formatting logic (#4794) --- lib/output.js | 81 ++++++++++++++++++++++------------------------------------- 1 file changed, 30 insertions(+), 51 deletions(-) (limited to 'lib') 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; } -- cgit v1.2.3