Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion escodegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions test/comment/empty-block-with-multi-comment.expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function test() {
/*
* Leading comment
*/
}
5 changes: 5 additions & 0 deletions test/comment/empty-block-with-multi-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function test() {
/*
* Leading comment
*/
}
3 changes: 3 additions & 0 deletions test/comment/empty-block-with-single-comment.expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function test() {
// Trailing
}
3 changes: 3 additions & 0 deletions test/comment/empty-block-with-single-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function test() {
// Trailing
}
6 changes: 2 additions & 4 deletions test/comment/try-block-line-comment.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ finally {
}
try {
} catch (e) {
} //
finally {
} finally {
}
{
try {
} catch (e) {
} //
finally {
} finally {
}
}