Skip to content

Commit

Permalink
Merge pull request #269 from kaicataldo/fixes264
Browse files Browse the repository at this point in the history
Fix: remove extra leading comments at node level (fixes #264)
  • Loading branch information
nzakas committed Apr 21, 2016
2 parents 88446f3 + e044705 commit 78f3b56
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 91 deletions.
60 changes: 19 additions & 41 deletions lib/comment-attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,10 @@ var astNodeTypes = require("./ast-node-types");
var extra = {
trailingComments: [],
leadingComments: [],
bottomRightStack: []
bottomRightStack: [],
previousNode: null
};

/** 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 @@ -65,6 +32,7 @@ module.exports = {
extra.trailingComments = [];
extra.leadingComments = [];
extra.bottomRightStack = [];
extra.previousNode = null;
},

addComment: function(comment) {
Expand All @@ -75,11 +43,10 @@ module.exports = {
processComment: function(node) {
var lastChild,
trailingComments,
i;
i,
j;

if (node.type === astNodeTypes.Program) {
removeExtraLeadingComments(node);

if (node.body.length > 0) {
return;
}
Expand Down Expand Up @@ -140,10 +107,19 @@ module.exports = {
}
}
} else if (extra.leadingComments.length > 0) {

if (extra.leadingComments[extra.leadingComments.length - 1].range[1] <= node.range[0]) {
node.leadingComments = extra.leadingComments;
extra.leadingComments = [];
if (extra.previousNode) {
for (j = 0; j < extra.leadingComments.length; j++) {
if (extra.leadingComments[j].end < extra.previousNode.end) {
extra.leadingComments.splice(j, 1);
j--;
}
}
}
if (extra.leadingComments.length > 0) {
node.leadingComments = extra.leadingComments;
extra.leadingComments = [];
}
} else {

// https://github.com/eslint/espree/issues/2
Expand Down Expand Up @@ -187,6 +163,8 @@ module.exports = {
}
}

extra.previousNode = node;

if (trailingComments) {
node.trailingComments = trailingComments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,6 @@ module.exports = {
"value": 777,
"raw": "777",
"leadingComments": [
{
"type": "Line",
"value": "foo",
"range": [
0,
5
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 5
}
}
},
{
"type": "Block",
"value": "aaa",
Expand Down Expand Up @@ -337,4 +319,4 @@ module.exports = {
]
}
]
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -649,27 +649,7 @@ module.exports = {
],
"value": false,
"raw": "false"
},
"leadingComments": [
{
"type": "Line",
"value": " no default",
"range": [
232,
245
],
"loc": {
"start": {
"line": 7,
"column": 12
},
"end": {
"line": 7,
"column": 25
}
}
}
]
}
}
]
}
Expand Down Expand Up @@ -1585,4 +1565,4 @@ module.exports = {
]
}
]
};
};
2 changes: 1 addition & 1 deletion tests/fixtures/libraries/XMLHttpRequest.js.result.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/fixtures/libraries/angular-1.2.5.js.result.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/fixtures/libraries/backbone-1.1.0.js.result.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/fixtures/libraries/benchmark.js.result.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/fixtures/libraries/escodegen.browser.js.result.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/fixtures/libraries/esmangle.browser.js.result.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/fixtures/libraries/jquery-1.9.1.js.result.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/fixtures/libraries/mootools-1.4.5.js.result.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/fixtures/libraries/yui-3.12.0.js.result.json

Large diffs are not rendered by default.

0 comments on commit 78f3b56

Please sign in to comment.