Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
Attach every comment to the possible following syntax node.
Browse files Browse the repository at this point in the history
The comment is placed in the leadingComments array of the said node.
This is based on the original patch from Yusuke Suzuki.

https://code.google.com/p/esprima/issues/detail?id=197
  • Loading branch information
ariya committed Sep 23, 2013
1 parent 2fec5d0 commit 7c35150
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 11 deletions.
63 changes: 62 additions & 1 deletion esprima.js
Expand Up @@ -336,7 +336,7 @@ parseStatement: true, parseSourceElement: true */
// 7.4 Comments

function addComment(type, value, start, end, loc) {
var comment;
var comment, attacher;

assert(typeof start === 'number', 'Comment must have valid position');

Expand All @@ -360,6 +360,15 @@ parseStatement: true, parseSourceElement: true */
comment.loc = loc;
}
extra.comments.push(comment);

if (extra.attachComment) {
attacher = {
comment: comment,
candidate: null,
range: [start, end]
};
extra.pendingComments.push(attacher);
}
}

function skipSingleLineComment() {
Expand Down Expand Up @@ -1349,6 +1358,29 @@ parseStatement: true, parseSourceElement: true */
}
},

processComment: function (node) {
var i, attacher, comment, pos, len;

if (typeof node.type === 'undefined') {
return;
}
for (i = 0; i < extra.pendingComments.length; ++i) {
attacher = extra.pendingComments[i];
comment = attacher.comment;
pos = attacher.candidate ? attacher.candidate.range[0] : attacher.range[0];
if (node.type !== Syntax.Program && node.range[0] >= pos) {
if (attacher.candidate) {
len = attacher.candidate.range[1] - attacher.candidate.range[0];
if ((node.range[1] - node.range[0]) >= len) {
attacher.candidate = node;
}
} else {
attacher.candidate = node;
}
}
}
},

markEnd: function (node) {
if (extra.range) {
node.range = [state.markerStack.pop(), index];
Expand All @@ -1366,6 +1398,9 @@ parseStatement: true, parseSourceElement: true */
};
this.postProcess(node);
}
if (extra.attachComment) {
this.processComment(node);
}
return node;
},

Expand Down Expand Up @@ -3474,6 +3509,23 @@ parseStatement: true, parseSourceElement: true */
return delegate.markEnd(delegate.createProgram(body));
}

function attachComments() {
var i, attacher, comment, node;

for (i = 0; i < extra.pendingComments.length; ++i) {
attacher = extra.pendingComments[i];
comment = attacher.comment;
node = attacher.candidate;
if (node) {
if (typeof node.leadingComments === 'undefined') {
node.leadingComments = [];
}
node.leadingComments.push(attacher.comment);
}
}
extra.pendingComments = [];
}

function filterTokenLocation() {
var i, entry, token, tokens = [];

Expand Down Expand Up @@ -3666,6 +3718,7 @@ parseStatement: true, parseSourceElement: true */
if (typeof options !== 'undefined') {
extra.range = (typeof options.range === 'boolean') && options.range;
extra.loc = (typeof options.loc === 'boolean') && options.loc;
extra.attachComment = (typeof options.attachComment === 'boolean') && options.attachComment;

if (extra.loc && options.source !== null && options.source !== undefined) {
extra.source = toString(options.source);
Expand All @@ -3680,6 +3733,11 @@ parseStatement: true, parseSourceElement: true */
if (typeof options.tolerant === 'boolean' && options.tolerant) {
extra.errors = [];
}
if (extra.attachComment) {
extra.range = true;
extra.pendingComments = [];
extra.comments = [];
}
}

if (length > 0) {
Expand All @@ -3705,6 +3763,9 @@ parseStatement: true, parseSourceElement: true */
if (typeof extra.errors !== 'undefined') {
program.errors = extra.errors;
}
if (extra.attachComment) {
attachComments();
}
} catch (e) {
throw e;
} finally {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -41,7 +41,7 @@
"lint": "node_modules/jslint/bin/jslint.js esprima.js",
"coverage": "npm run-script analyze-coverage && npm run-script check-coverage",
"analyze-coverage": "node node_modules/istanbul/lib/cli.js cover test/runner.js",
"check-coverage": "node node_modules/istanbul/lib/cli.js check-coverage --statement -8 --branch -17 --function 100",
"check-coverage": "node node_modules/istanbul/lib/cli.js check-coverage --statement -8 --branch -19 --function 100",

"complexity": "npm run-script analyze-complexity && npm run-script check-complexity",
"analyze-complexity": "node tools/list-complexity.js",
Expand Down
19 changes: 19 additions & 0 deletions test/runner.js
Expand Up @@ -71,6 +71,21 @@ function errorToObject(e) {
};
}

function hasAttachedComment(syntax) {
var key;
for (key in syntax) {
if (key === 'leadingComments') {
return true;
}
if (typeof syntax[key] === 'object' && syntax[key] !== null) {
if (hasAttachedComment(syntax[key])) {
return true;
}
}
}
return false;
}

function testParse(esprima, code, syntax) {
'use strict';
var expected, tree, actual, options, StringObject, i, len, err;
Expand All @@ -88,6 +103,10 @@ function testParse(esprima, code, syntax) {
source: null
};

if (options.comment) {
options.attachComment = hasAttachedComment(syntax);
}

if (typeof syntax.tokens !== 'undefined') {
if (syntax.tokens.length > 0) {
options.range = (typeof syntax.tokens[0].range !== 'undefined');
Expand Down

0 comments on commit 7c35150

Please sign in to comment.