Skip to content

Commit

Permalink
Fix: Fix behavior of ignoring comments within previous nodes (refs #256)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Mar 16, 2016
1 parent a516377 commit 98441cb
Show file tree
Hide file tree
Showing 3 changed files with 431 additions and 17 deletions.
53 changes: 36 additions & 17 deletions lib/comment-attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,40 @@ var extra = {
bottomRightStack: []
};

/** Recursively remove leading comments that are incorrectly added when no
* expression exists between comment and the current node
* @param {node} node to recursively check
* @returns {void}
*/
function removeExtraLeadingComments(node) {
var i, j;

if (!node.body) {
return;
}

if (Array.isArray(node.body)) {
// i must start at 0 so that we can check all indices recursively
for (i = 0; i < node.body.length; i++) {
// i must be greater than 0 to perform the check on the previous node
if (i > 0 && node.body[i].leadingComments) {
for (j = 0; j < node.body[i].leadingComments.length; j++) {
if (node.body[i].leadingComments[j].range[1] < node.body[i - 1].range[1]) {
node.body[i].leadingComments.splice(j, 1);
}
}

if (node.body[i].leadingComments.length === 0) {
delete node.body[i].leadingComments;
}
}
removeExtraLeadingComments(node.body[i]);
}
} else {
removeExtraLeadingComments(node.body);
}
}

//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------
Expand All @@ -63,25 +97,10 @@ module.exports = {
processComment: function(node) {
var lastChild,
trailingComments,
i,
j;
i;

if (node.type === astNodeTypes.Program) {
// Remove leading comments that are incorrectly added when no expression exists
// between comment and the current node
for (i = 1; i < node.body.length; i++) {
if (node.body[i].leadingComments) {
for (j = 0; j < node.body[i].leadingComments.length; j++) {
if (node.body[i].leadingComments[j].range[1] < node.body[i - 1].range[1]) {
node.body[i].leadingComments.splice(j, 1);
}
}

if (node.body[i].leadingComments.length === 0) {
delete node.body[i].leadingComments;
}
}
}
removeExtraLeadingComments(node);

if (node.body.length > 0) {
return;
Expand Down
Loading

0 comments on commit 98441cb

Please sign in to comment.