From e50babe916f99ee36d0a2fa602195596985dd9f2 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 14 Jul 2015 20:55:56 +0100 Subject: [PATCH] add shouldPrintComment option and output comments when compact option is set to true - fixes #1810 --- packages/babel/src/generation/index.js | 21 +++++++++++++++++-- .../transformation/file/options/config.json | 5 +++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/babel/src/generation/index.js b/packages/babel/src/generation/index.js index e2fd0db831..56ceedde7b 100644 --- a/packages/babel/src/generation/index.js +++ b/packages/babel/src/generation/index.js @@ -47,6 +47,7 @@ class CodeGenerator { } var format = { + shouldPrintComment: opts.shouldPrintComment, retainLines: opts.retainLines, comments: opts.comments == null || opts.comments, compact: opts.compact, @@ -377,16 +378,32 @@ class CodeGenerator { return (node && node[key]) || []; } + /** + * [Please add a description.] + */ + + shouldPrintComment(comment) { + if (this.format.shouldPrintComment) { + return this.format.shouldPrintComment(comment.value); + } else { + if (comment.value.indexOf("@license") >= 0 || comment.value.indexOf("@preserve") >= 0) { + return true; + } else { + return this.format.comments; + } + } + } + /** * [Please add a description.] */ _printComments(comments) { - if (this.format.compact) return; - if (!this.format.comments) return; if (!comments || !comments.length) return; for (var comment of (comments: Array)) { + if (!this.shouldPrintComment(comment)) continue; + var skip = false; if (this.ast.comments) { diff --git a/packages/babel/src/transformation/file/options/config.json b/packages/babel/src/transformation/file/options/config.json index ad38102420..58ee9b24e2 100644 --- a/packages/babel/src/transformation/file/options/config.json +++ b/packages/babel/src/transformation/file/options/config.json @@ -163,6 +163,11 @@ "description": "strip/output comments in generated output (on by default)" }, + "shouldPrintComment": { + "hidden": true, + "description": "optional callback to control whether a comment should be inserted, when this is used the comments option is ignored" + }, + "compact": { "type": "booleanString", "default": "auto",