Permalink
Browse files

Fix the attachment of comments.

Both the proximity and the size of each syntax node need to be taken
into account when deciding which comment must be attached.

https://code.google.com/p/esprima/issues/detail?id=197
  • Loading branch information...
ariya committed Oct 5, 2013
1 parent 2f0cb7e commit 73fe9459e40f973af6b8dc605dd9f93c9309f610
Showing with 413 additions and 13 deletions.
  1. +14 −13 esprima.js
  2. +399 −0 test/test.js
View
@@ -1372,9 +1372,9 @@ parseStatement: true, parseSourceElement: true */
},
processComment: function (node) {
- var i, attacher, comment, pos, len;
+ var i, attacher, pos, len, candidate;
- if (typeof node.type === 'undefined') {
+ if (typeof node.type === 'undefined' || node.type === Syntax.Program) {
return;
}
@@ -1383,23 +1383,24 @@ parseStatement: true, parseSourceElement: true */
for (i = 0; i < extra.pendingComments.length; ++i) {
attacher = extra.pendingComments[i];
- comment = attacher.comment;
- pos = attacher.leading ? attacher.leading.range[0] : attacher.range[0];
- if (node.type !== Syntax.Program && node.range[0] >= pos) {
- if (attacher.leading) {
- len = attacher.leading.range[1] - attacher.leading.range[0];
- if ((node.range[1] - node.range[0]) >= len) {
+ if (node.range[0] >= attacher.comment.range[1]) {
+ candidate = attacher.leading;
+ if (candidate) {
+ pos = candidate.range[0];
+ len = candidate.range[1] - pos;
+ if (node.range[0] <= pos && (node.range[1] - node.range[0] >= len)) {
attacher.leading = node;
}
} else {
attacher.leading = node;
}
}
- pos = attacher.trailing ? attacher.trailing.range[0] : attacher.range[0];
- if (node.type !== Syntax.Program && node.range[1] <= pos) {
- if (attacher.trailing) {
- len = attacher.trailing.range[1] - attacher.trailing.range[0];
- if ((node.range[1] - node.range[0]) >= len) {
+ if (node.range[1] <= attacher.comment.range[0]) {
+ candidate = attacher.trailing;
+ if (candidate) {
+ pos = candidate.range[0];
+ len = candidate.range[1] - pos;
+ if (node.range[0] <= pos && (node.range[1] - node.range[0] >= len)) {
attacher.trailing = node;
}
} else {
Oops, something went wrong.

0 comments on commit 73fe945

Please sign in to comment.