From 3d8f67262318bb880b58b525f6f90587688d1fe8 Mon Sep 17 00:00:00 2001 From: gadisn Date: Sun, 5 Jun 2016 17:49:12 +0300 Subject: [PATCH 1/7] Add support for inline comments In regard to issue #206 ("Attach comment to an empty object"): I have an initial modification of attachComments() and addComments() which supports comments in empty object/block. I've called them inline comments, and they are attached to the node which represents the empty object/block (i.e. ObjectExpression/BlockStatement). --- escodegen.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/escodegen.js b/escodegen.js index 30132a80..988a8895 100644 --- a/escodegen.js +++ b/escodegen.js @@ -664,6 +664,31 @@ return '/*' + comment.value + '*/'; } + function addInlineComment(stmt, result) { + if (stmt.inlineComments) { + for (var i = 0; i < stmt.inlineComments.length; i++) { + var comment = stmt.inlineComments[i]; + // For object, replace '{}' in result with empty array + if (stmt.type === Syntax.ObjectExpression) { + if (!isArray(result)) { + result = []; + result.push("{"); + result.push("\n"); + result.push(generateComment(comment)); + result.push("}"); + } else { + // Add in index 2: index 0 is '{', index 1 is newline + result.splice(2 + i, 0, generateComment(comment)); + } + } else if (stmt.type === Syntax.BlockStatement) { + // Add in index 2: index 0 is '{', index 1 is newline (see definition of BlockStatement) + result.splice(2 + i, 0, generateComment(comment)); + } + } + } + return result; + } + function addComments(stmt, result) { var i, len, comment, save, tailingToStatement, specialBase, fragment, extRange, range, prevRange, prefix, infix, suffix, count; @@ -726,7 +751,12 @@ } } - result.push(addIndent(save)); + // Add inline comments after the leading comments were added + result.push(addInlineComment(stmt, save)); + } + else { + // Add inline comments + result = addInlineComment(stmt, result); } if (stmt.trailingComments) { @@ -2554,5 +2584,6 @@ exports.browser = false; exports.FORMAT_MINIFY = FORMAT_MINIFY; exports.FORMAT_DEFAULTS = FORMAT_DEFAULTS; + exports._addInlineComment = addInlineComment }()); /* vim: set sw=4 ts=4 et tw=80 : */ From 7f7ad83dbd0fbaafb6549e663a35997671942198 Mon Sep 17 00:00:00 2001 From: gadisn Date: Mon, 6 Jun 2016 09:14:22 +0300 Subject: [PATCH 2/7] Removed an obsolete export of addInlineComment --- escodegen.js | 1 - 1 file changed, 1 deletion(-) diff --git a/escodegen.js b/escodegen.js index 988a8895..2c021924 100644 --- a/escodegen.js +++ b/escodegen.js @@ -2584,6 +2584,5 @@ exports.browser = false; exports.FORMAT_MINIFY = FORMAT_MINIFY; exports.FORMAT_DEFAULTS = FORMAT_DEFAULTS; - exports._addInlineComment = addInlineComment }()); /* vim: set sw=4 ts=4 et tw=80 : */ From 034650add3d2570b87dd7da7bcaadbe76c2ca902 Mon Sep 17 00:00:00 2001 From: gadisn Date: Tue, 7 Jun 2016 08:56:04 +0300 Subject: [PATCH 3/7] Fix broken indentation Indentation got broken with the introduction of inline comments --- escodegen.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/escodegen.js b/escodegen.js index 2c021924..8f9b2a59 100644 --- a/escodegen.js +++ b/escodegen.js @@ -664,7 +664,7 @@ return '/*' + comment.value + '*/'; } - function addInlineComment(stmt, result) { + function addInlineComment(stmt, result, bAddIndent) { if (stmt.inlineComments) { for (var i = 0; i < stmt.inlineComments.length; i++) { var comment = stmt.inlineComments[i]; @@ -685,8 +685,14 @@ result.splice(2 + i, 0, generateComment(comment)); } } - } - return result; + return result; + } else { + if (bAddIndent) { + return addIndent(result); + } else { + return result; + } + } } function addComments(stmt, result) { @@ -752,11 +758,11 @@ } // Add inline comments after the leading comments were added - result.push(addInlineComment(stmt, save)); + result.push(addInlineComment(stmt, save, true)); } else { // Add inline comments - result = addInlineComment(stmt, result); + result = addInlineComment(stmt, result, false); } if (stmt.trailingComments) { From b28ea813afb8217409c12c200be2bbd40085d4b0 Mon Sep 17 00:00:00 2001 From: gadisn Date: Tue, 7 Jun 2016 10:18:32 +0300 Subject: [PATCH 4/7] Remove trailing space --- escodegen.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/escodegen.js b/escodegen.js index 8f9b2a59..6ef7ccee 100644 --- a/escodegen.js +++ b/escodegen.js @@ -692,7 +692,7 @@ } else { return result; } - } + } } function addComments(stmt, result) { From b2db53bc2658a413605433e3b6418e02b8ef43da Mon Sep 17 00:00:00 2001 From: Roi Date: Sun, 9 Oct 2016 18:04:45 +0300 Subject: [PATCH 5/7] Add tests for inline comments --- test/comment/empty-block-with-multi-comment.expected.js | 5 +++++ test/comment/empty-block-with-multi-comment.js | 5 +++++ test/comment/empty-block-with-single-comment.expected.js | 3 +++ test/comment/empty-block-with-single-comment.js | 3 +++ 4 files changed, 16 insertions(+) create mode 100644 test/comment/empty-block-with-multi-comment.expected.js create mode 100644 test/comment/empty-block-with-multi-comment.js create mode 100644 test/comment/empty-block-with-single-comment.expected.js create mode 100644 test/comment/empty-block-with-single-comment.js diff --git a/test/comment/empty-block-with-multi-comment.expected.js b/test/comment/empty-block-with-multi-comment.expected.js new file mode 100644 index 00000000..3ea6b63e --- /dev/null +++ b/test/comment/empty-block-with-multi-comment.expected.js @@ -0,0 +1,5 @@ +function test() { + /* + * Leading comment + */ +} \ No newline at end of file diff --git a/test/comment/empty-block-with-multi-comment.js b/test/comment/empty-block-with-multi-comment.js new file mode 100644 index 00000000..3ea6b63e --- /dev/null +++ b/test/comment/empty-block-with-multi-comment.js @@ -0,0 +1,5 @@ +function test() { + /* + * Leading comment + */ +} \ No newline at end of file diff --git a/test/comment/empty-block-with-single-comment.expected.js b/test/comment/empty-block-with-single-comment.expected.js new file mode 100644 index 00000000..4eca86a8 --- /dev/null +++ b/test/comment/empty-block-with-single-comment.expected.js @@ -0,0 +1,3 @@ +function test() { + // Trailing +} diff --git a/test/comment/empty-block-with-single-comment.js b/test/comment/empty-block-with-single-comment.js new file mode 100644 index 00000000..4eca86a8 --- /dev/null +++ b/test/comment/empty-block-with-single-comment.js @@ -0,0 +1,3 @@ +function test() { + // Trailing +} From ba68859c388ff752bce8521f1bf4cbafaf083e54 Mon Sep 17 00:00:00 2001 From: Roi Date: Sun, 9 Oct 2016 18:06:01 +0300 Subject: [PATCH 6/7] Update dependencies to the latets version --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 029d6049..3ba47c82 100644 --- a/package.json +++ b/package.json @@ -31,10 +31,10 @@ "url": "http://github.com/estools/escodegen.git" }, "dependencies": { - "estraverse": "^1.9.1", + "estraverse": "^4.2.0", "esutils": "^2.0.2", - "esprima": "^2.7.1", - "optionator": "^0.8.1" + "esprima": "^3.0.0", + "optionator": "^0.8.2" }, "optionalDependencies": { "source-map": "~0.2.0" From 870c168bc309b8fddac43b166f2664bf61fafd9e Mon Sep 17 00:00:00 2001 From: Roi Date: Sun, 9 Oct 2016 18:08:06 +0300 Subject: [PATCH 7/7] Update expected file according to latest traverse version --- test/comment/try-block-line-comment.expected.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/comment/try-block-line-comment.expected.js b/test/comment/try-block-line-comment.expected.js index 726ada5b..d46d7867 100644 --- a/test/comment/try-block-line-comment.expected.js +++ b/test/comment/try-block-line-comment.expected.js @@ -4,13 +4,11 @@ finally { } try { } catch (e) { -} // -finally { +} finally { } { try { } catch (e) { - } // - finally { + } finally { } }