Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: remove extra leading comments at node level (fixes #264) #269

Merged
merged 1 commit into from
Apr 21, 2016
Merged
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
60 changes: 19 additions & 41 deletions lib/comment-attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,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 @@ -67,6 +34,7 @@ module.exports = {
extra.trailingComments = [];
extra.leadingComments = [];
extra.bottomRightStack = [];
extra.previousNode = null;
},

addComment: function(comment) {
Expand All @@ -77,11 +45,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 @@ -142,10 +109,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 @@ -189,6 +165,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",
Copy link
Member Author

@kaicataldo kaicataldo Apr 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrectly attached leading comment now fixed

"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
}
}
}
]
}
Copy link
Member Author

@kaicataldo kaicataldo Apr 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrectly attached leading comment now fixed

}
]
}
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.