diff --git a/escodegen.js b/escodegen.js index 42c05aed..79408a1a 100644 --- a/escodegen.js +++ b/escodegen.js @@ -664,6 +664,37 @@ return '/*' + comment.value + '*/'; } + function addInlineComment(stmt, result, bAddIndent) { + 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; + } else { + if (bAddIndent) { + return addIndent(result); + } else { + return result; + } + } + } + function addComments(stmt, result) { var i, len, comment, save, tailingToStatement, specialBase, fragment, extRange, range, prevRange, prefix, infix, suffix, count; @@ -726,7 +757,12 @@ } } - result.push(addIndent(save)); + // Add inline comments after the leading comments were added + result.push(addInlineComment(stmt, save, true)); + } + else { + // Add inline comments + result = addInlineComment(stmt, result, false); } if (stmt.trailingComments) { 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" 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 +} 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 { } }