From 3d8f67262318bb880b58b525f6f90587688d1fe8 Mon Sep 17 00:00:00 2001 From: gadisn Date: Sun, 5 Jun 2016 17:49:12 +0300 Subject: [PATCH 01/15] 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 02/15] 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 03/15] 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 04/15] 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 706090b41185c86fba4d5b3800e899151685bc4c Mon Sep 17 00:00:00 2001 From: Roi Date: Thu, 15 Sep 2016 11:10:38 +0300 Subject: [PATCH 05/15] Add files via upload Add tests to Gadi's inline comments implemetation --- test/comment/empty-block-with-multi-comment.expected.js | 5 +++++ test/comment/empty-block-with-multi-comment.js | 5 +++++ test/comment/empty-block-with-singlei-comment.expected.js | 3 +++ test/comment/empty-block-with-singlei-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-singlei-comment.expected.js create mode 100644 test/comment/empty-block-with-singlei-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-singlei-comment.expected.js b/test/comment/empty-block-with-singlei-comment.expected.js new file mode 100644 index 00000000..18c7f8b9 --- /dev/null +++ b/test/comment/empty-block-with-singlei-comment.expected.js @@ -0,0 +1,3 @@ +function test() { + // Trailing +} \ No newline at end of file diff --git a/test/comment/empty-block-with-singlei-comment.js b/test/comment/empty-block-with-singlei-comment.js new file mode 100644 index 00000000..18c7f8b9 --- /dev/null +++ b/test/comment/empty-block-with-singlei-comment.js @@ -0,0 +1,3 @@ +function test() { + // Trailing +} \ No newline at end of file From c4fb1741dc04a9f7baa5661a7d2d5478800857d6 Mon Sep 17 00:00:00 2001 From: Roi Date: Thu, 15 Sep 2016 11:29:13 +0300 Subject: [PATCH 06/15] Add files via upload Add tests to inline comments From a394bba1d519f8d2969295e959fb814c9e80fff5 Mon Sep 17 00:00:00 2001 From: Roi Date: Thu, 15 Sep 2016 11:40:50 +0300 Subject: [PATCH 07/15] Add files via upload Add tests to Gadi's inline comments implemintation --- test/empty-block-with-multi-comment.expected.js | 5 +++++ test/empty-block-with-multi-comment.js | 5 +++++ test/empty-block-with-singlei-comment.expected.js | 3 +++ test/empty-block-with-singlei-comment.js | 3 +++ 4 files changed, 16 insertions(+) create mode 100644 test/empty-block-with-multi-comment.expected.js create mode 100644 test/empty-block-with-multi-comment.js create mode 100644 test/empty-block-with-singlei-comment.expected.js create mode 100644 test/empty-block-with-singlei-comment.js diff --git a/test/empty-block-with-multi-comment.expected.js b/test/empty-block-with-multi-comment.expected.js new file mode 100644 index 00000000..3ea6b63e --- /dev/null +++ b/test/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/empty-block-with-multi-comment.js b/test/empty-block-with-multi-comment.js new file mode 100644 index 00000000..3ea6b63e --- /dev/null +++ b/test/empty-block-with-multi-comment.js @@ -0,0 +1,5 @@ +function test() { + /* + * Leading comment + */ +} \ No newline at end of file diff --git a/test/empty-block-with-singlei-comment.expected.js b/test/empty-block-with-singlei-comment.expected.js new file mode 100644 index 00000000..18c7f8b9 --- /dev/null +++ b/test/empty-block-with-singlei-comment.expected.js @@ -0,0 +1,3 @@ +function test() { + // Trailing +} \ No newline at end of file diff --git a/test/empty-block-with-singlei-comment.js b/test/empty-block-with-singlei-comment.js new file mode 100644 index 00000000..18c7f8b9 --- /dev/null +++ b/test/empty-block-with-singlei-comment.js @@ -0,0 +1,3 @@ +function test() { + // Trailing +} \ No newline at end of file From e1378af936dbad9a9a73cedd0d9c8e66a6853bd0 Mon Sep 17 00:00:00 2001 From: Roi Date: Thu, 15 Sep 2016 16:31:59 +0300 Subject: [PATCH 08/15] Rename empty-block-with-singlei-comment.expected.js to empty-block-with-single-comment.expected.js --- ....expected.js => empty-block-with-single-comment.expected.js} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename test/comment/{empty-block-with-singlei-comment.expected.js => empty-block-with-single-comment.expected.js} (86%) diff --git a/test/comment/empty-block-with-singlei-comment.expected.js b/test/comment/empty-block-with-single-comment.expected.js similarity index 86% rename from test/comment/empty-block-with-singlei-comment.expected.js rename to test/comment/empty-block-with-single-comment.expected.js index 18c7f8b9..4eca86a8 100644 --- a/test/comment/empty-block-with-singlei-comment.expected.js +++ b/test/comment/empty-block-with-single-comment.expected.js @@ -1,3 +1,3 @@ function test() { // Trailing -} \ No newline at end of file +} From aed6bb3a4fb0a506b61f4b83508c1b8868e030c8 Mon Sep 17 00:00:00 2001 From: Roi Date: Thu, 15 Sep 2016 16:32:47 +0300 Subject: [PATCH 09/15] Rename empty-block-with-singlei-comment.js to empty-block-with-single-comment.js --- ...th-singlei-comment.js => empty-block-with-single-comment.js} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename test/comment/{empty-block-with-singlei-comment.js => empty-block-with-single-comment.js} (86%) diff --git a/test/comment/empty-block-with-singlei-comment.js b/test/comment/empty-block-with-single-comment.js similarity index 86% rename from test/comment/empty-block-with-singlei-comment.js rename to test/comment/empty-block-with-single-comment.js index 18c7f8b9..4eca86a8 100644 --- a/test/comment/empty-block-with-singlei-comment.js +++ b/test/comment/empty-block-with-single-comment.js @@ -1,3 +1,3 @@ function test() { // Trailing -} \ No newline at end of file +} From 2d9ccc5f1198f3234255fea0281ff83287443d24 Mon Sep 17 00:00:00 2001 From: Roi Date: Thu, 15 Sep 2016 16:45:36 +0300 Subject: [PATCH 10/15] Delete empty-block-with-singlei-comment.js --- test/empty-block-with-singlei-comment.js | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 test/empty-block-with-singlei-comment.js diff --git a/test/empty-block-with-singlei-comment.js b/test/empty-block-with-singlei-comment.js deleted file mode 100644 index 18c7f8b9..00000000 --- a/test/empty-block-with-singlei-comment.js +++ /dev/null @@ -1,3 +0,0 @@ -function test() { - // Trailing -} \ No newline at end of file From 21b5947ca14809e1ef309b4d3a718da5dfa30703 Mon Sep 17 00:00:00 2001 From: Roi Date: Thu, 15 Sep 2016 16:45:50 +0300 Subject: [PATCH 11/15] Delete empty-block-with-singlei-comment.expected.js --- test/empty-block-with-singlei-comment.expected.js | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 test/empty-block-with-singlei-comment.expected.js diff --git a/test/empty-block-with-singlei-comment.expected.js b/test/empty-block-with-singlei-comment.expected.js deleted file mode 100644 index 18c7f8b9..00000000 --- a/test/empty-block-with-singlei-comment.expected.js +++ /dev/null @@ -1,3 +0,0 @@ -function test() { - // Trailing -} \ No newline at end of file From 5bf78eef7bdc7177b8ef85fc84164d0090cbe619 Mon Sep 17 00:00:00 2001 From: Roi Date: Thu, 15 Sep 2016 16:46:01 +0300 Subject: [PATCH 12/15] Delete empty-block-with-multi-comment.js --- test/empty-block-with-multi-comment.js | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 test/empty-block-with-multi-comment.js diff --git a/test/empty-block-with-multi-comment.js b/test/empty-block-with-multi-comment.js deleted file mode 100644 index 3ea6b63e..00000000 --- a/test/empty-block-with-multi-comment.js +++ /dev/null @@ -1,5 +0,0 @@ -function test() { - /* - * Leading comment - */ -} \ No newline at end of file From fc32a59887613cac1255d9b7113955525d2aa178 Mon Sep 17 00:00:00 2001 From: Roi Date: Thu, 15 Sep 2016 16:46:13 +0300 Subject: [PATCH 13/15] Delete empty-block-with-multi-comment.expected.js --- test/empty-block-with-multi-comment.expected.js | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 test/empty-block-with-multi-comment.expected.js diff --git a/test/empty-block-with-multi-comment.expected.js b/test/empty-block-with-multi-comment.expected.js deleted file mode 100644 index 3ea6b63e..00000000 --- a/test/empty-block-with-multi-comment.expected.js +++ /dev/null @@ -1,5 +0,0 @@ -function test() { - /* - * Leading comment - */ -} \ No newline at end of file From c630d38c83fd593be8b4db952e618dbe20f2b905 Mon Sep 17 00:00:00 2001 From: Roi Date: Sun, 9 Oct 2016 16:40:48 +0300 Subject: [PATCH 14/15] Update try-block-line-comment.expected.js Updated according to new traverese --- 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 { } } From ff36ad21b4b363f2f581c8e57f97dd4c4983ac0b Mon Sep 17 00:00:00 2001 From: Roi Date: Sun, 9 Oct 2016 17:01:09 +0300 Subject: [PATCH 15/15] Update package.json --- 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"