Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Attach every comment to the possible previous syntax node.

The comment is placed in the trailingComments 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...
commit 2f0cb7e368085686f2cf1553f8fdf45f2c415d28 1 parent 73f70cd
@ariya authored
Showing with 341 additions and 15 deletions.
  1. +39 −13 esprima.js
  2. +1 −1  test/runner.js
  3. +301 −1 test/test.js
View
52 esprima.js
@@ -364,7 +364,8 @@ parseStatement: true, parseSourceElement: true */
if (extra.attachComment) {
attacher = {
comment: comment,
- candidate: null,
+ leading: null,
+ trailing: null,
range: [start, end]
};
extra.pendingComments.push(attacher);
@@ -1376,18 +1377,33 @@ parseStatement: true, parseSourceElement: true */
if (typeof node.type === 'undefined') {
return;
}
+
+ // Check for possible additional trailing comments.
+ peek();
+
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];
+ pos = attacher.leading ? attacher.leading.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 (attacher.leading) {
+ len = attacher.leading.range[1] - attacher.leading.range[0];
+ if ((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) {
- attacher.candidate = node;
+ attacher.trailing = node;
}
} else {
- attacher.candidate = node;
+ attacher.trailing = node;
}
}
}
@@ -3522,17 +3538,24 @@ parseStatement: true, parseSourceElement: true */
}
function attachComments() {
- var i, attacher, comment, node;
+ var i, attacher, comment, leading, trailing;
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 = [];
+ leading = attacher.leading;
+ if (leading) {
+ if (typeof leading.leadingComments === 'undefined') {
+ leading.leadingComments = [];
+ }
+ leading.leadingComments.push(attacher.comment);
+ }
+ trailing = attacher.trailing;
+ if (trailing) {
+ if (typeof trailing.trailingComments === 'undefined') {
+ trailing.trailingComments = [];
}
- node.leadingComments.push(attacher.comment);
+ trailing.trailingComments.push(attacher.comment);
}
}
extra.pendingComments = [];
@@ -3587,8 +3610,11 @@ parseStatement: true, parseSourceElement: true */
column: this.marker[5]
}
};
+ node = delegate.postProcess(node);
+ }
+ if (extra.attachComment) {
+ delegate.processComment(node);
}
- node = delegate.postProcess(node);
}
};
View
2  test/runner.js
@@ -74,7 +74,7 @@ function errorToObject(e) {
function hasAttachedComment(syntax) {
var key;
for (key in syntax) {
- if (key === 'leadingComments') {
+ if (key === 'leadingComments' || key === 'trailingComments') {
return true;
}
if (typeof syntax[key] === 'object' && syntax[key] !== null) {
View
302 test/test.js
@@ -3708,7 +3708,16 @@ var testFixture = {
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 2 }
- }
+ },
+ trailingComments: [{
+ type: 'Line',
+ value: ' line comment',
+ range: [3, 18],
+ loc: {
+ start: { line: 1, column: 3 },
+ end: { line: 1, column: 18 }
+ }
+ }]
},
range: [0, 18],
loc: {
@@ -3933,6 +3942,51 @@ var testFixture = {
}]
},
+ '42/**/': {
+ type: 'Program',
+ body: [{
+ type: 'ExpressionStatement',
+ expression: {
+ type: 'Literal',
+ value: 42,
+ raw: '42',
+ range: [0, 2],
+ loc: {
+ start: { line: 1, column: 0 },
+ end: { line: 1, column: 2 }
+ },
+ trailingComments: [{
+ type: 'Block',
+ value: '',
+ range: [2, 6],
+ loc: {
+ start: { line: 1, column: 2 },
+ end: { line: 1, column: 6 }
+ }
+ }]
+ },
+ range: [0, 6],
+ loc: {
+ start: { line: 1, column: 0 },
+ end: { line: 1, column: 6 }
+ }
+ }],
+ range: [0, 6],
+ loc: {
+ start: { line: 1, column: 0 },
+ end: { line: 1, column: 6 }
+ },
+ comments: [{
+ type: 'Block',
+ value: '',
+ range: [2, 6],
+ loc: {
+ start: { line: 1, column: 2 },
+ end: { line: 1, column: 6 }
+ }
+ }]
+ },
+
'// Hello, world!\n\n// Another hello\n42': {
type: 'Program',
body: [{
@@ -3994,6 +4048,85 @@ var testFixture = {
}]
},
+ 'if (x) { doThat() // Some comment\n }': {
+ type: 'Program',
+ body: [{
+ type: 'IfStatement',
+ test: {
+ type: 'Identifier',
+ name: 'x',
+ range: [4, 5],
+ loc: {
+ start: { line: 1, column: 4 },
+ end: { line: 1, column: 5 }
+ }
+ },
+ consequent: {
+ type: 'BlockStatement',
+ body: [{
+ type: 'ExpressionStatement',
+ expression: {
+ type: 'CallExpression',
+ callee: {
+ type: 'Identifier',
+ name: 'doThat',
+ range: [9, 15],
+ loc: {
+ start: { line: 1, column: 9 },
+ end: { line: 1, column: 15 }
+ }
+ },
+ 'arguments': [],
+ range: [9, 17],
+ loc: {
+ start: { line: 1, column: 9 },
+ end: { line: 1, column: 17 }
+ },
+ trailingComments: [{
+ type: 'Line',
+ value: ' Some comment',
+ range: [18, 33],
+ loc: {
+ start: { line: 1, column: 18 },
+ end: { line: 1, column: 33 }
+ }
+ }]
+ },
+ range: [9, 35],
+ loc: {
+ start: { line: 1, column: 9 },
+ end: { line: 2, column: 1 }
+ }
+ }],
+ range: [7, 36],
+ loc: {
+ start: { line: 1, column: 7 },
+ end: { line: 2, column: 2 }
+ }
+ },
+ alternate: null,
+ range: [0, 36],
+ loc: {
+ start: { line: 1, column: 0 },
+ end: { line: 2, column: 2 }
+ }
+ }],
+ range: [0, 36],
+ loc: {
+ start: { line: 1, column: 0 },
+ end: { line: 2, column: 2 }
+ },
+ comments: [{
+ type: 'Line',
+ value: ' Some comment',
+ range: [18, 33],
+ loc: {
+ start: { line: 1, column: 18 },
+ end: { line: 1, column: 33 }
+ }
+ }]
+ },
+
'if (x) { // Some comment\ndoThat(); }': {
type: 'Program',
body: [{
@@ -4152,6 +4285,85 @@ var testFixture = {
}]
},
+ 'if (x) { doThat() /* Some comment */ }': {
+ type: 'Program',
+ body: [{
+ type: 'IfStatement',
+ test: {
+ type: 'Identifier',
+ name: 'x',
+ range: [4, 5],
+ loc: {
+ start: { line: 1, column: 4 },
+ end: { line: 1, column: 5 }
+ }
+ },
+ consequent: {
+ type: 'BlockStatement',
+ body: [{
+ type: 'ExpressionStatement',
+ expression: {
+ type: 'CallExpression',
+ callee: {
+ type: 'Identifier',
+ name: 'doThat',
+ range: [9, 15],
+ loc: {
+ start: { line: 1, column: 9 },
+ end: { line: 1, column: 15 }
+ }
+ },
+ 'arguments': [],
+ range: [9, 17],
+ loc: {
+ start: { line: 1, column: 9 },
+ end: { line: 1, column: 17 }
+ },
+ trailingComments: [{
+ type: 'Block',
+ value: ' Some comment ',
+ range: [18, 36],
+ loc: {
+ start: { line: 1, column: 18 },
+ end: { line: 1, column: 36 }
+ }
+ }]
+ },
+ range: [9, 37],
+ loc: {
+ start: { line: 1, column: 9 },
+ end: { line: 1, column: 37 }
+ }
+ }],
+ range: [7, 38],
+ loc: {
+ start: { line: 1, column: 7 },
+ end: { line: 1, column: 38 }
+ }
+ },
+ alternate: null,
+ range: [0, 38],
+ loc: {
+ start: { line: 1, column: 0 },
+ end: { line: 1, column: 38 }
+ }
+ }],
+ range: [0, 38],
+ loc: {
+ start: { line: 1, column: 0 },
+ end: { line: 1, column: 38 }
+ },
+ comments: [{
+ type: 'Block',
+ value: ' Some comment ',
+ range: [18, 36],
+ loc: {
+ start: { line: 1, column: 18 },
+ end: { line: 1, column: 36 }
+ }
+ }]
+ },
+
'switch (answer) { case 42: /* perfect */ bingo() }': {
type: 'Program',
body: [{
@@ -4240,6 +4452,94 @@ var testFixture = {
}]
},
+ 'switch (answer) { case 42: bingo() /* perfect */ }': {
+ type: 'Program',
+ body: [{
+ type: 'SwitchStatement',
+ discriminant: {
+ type: 'Identifier',
+ name: 'answer',
+ range: [8, 14],
+ loc: {
+ start: { line: 1, column: 8 },
+ end: { line: 1, column: 14 }
+ }
+ },
+ cases: [{
+ type: 'SwitchCase',
+ test: {
+ type: 'Literal',
+ value: 42,
+ raw: '42',
+ range: [23, 25],
+ loc: {
+ start: { line: 1, column: 23 },
+ end: { line: 1, column: 25 }
+ }
+ },
+ consequent: [{
+ type: 'ExpressionStatement',
+ expression: {
+ type: 'CallExpression',
+ callee: {
+ type: 'Identifier',
+ name: 'bingo',
+ range: [27, 32],
+ loc: {
+ start: { line: 1, column: 27 },
+ end: { line: 1, column: 32 }
+ }
+ },
+ 'arguments': [],
+ range: [27, 34],
+ loc: {
+ start: { line: 1, column: 27 },
+ end: { line: 1, column: 34 }
+ },
+ trailingComments: [{
+ type: 'Block',
+ value: ' perfect ',
+ range: [35, 48],
+ loc: {
+ start: { line: 1, column: 35 },
+ end: { line: 1, column: 48 }
+ }
+ }]
+ },
+ range: [27, 49],
+ loc: {
+ start: { line: 1, column: 27 },
+ end: { line: 1, column: 49 }
+ }
+ }],
+ range: [18, 49],
+ loc: {
+ start: { line: 1, column: 18 },
+ end: { line: 1, column: 49 }
+ }
+ }],
+ range: [0, 50],
+ loc: {
+ start: { line: 1, column: 0 },
+ end: { line: 1, column: 50 }
+ }
+ }],
+ range: [0, 50],
+ loc: {
+ start: { line: 1, column: 0 },
+ end: { line: 1, column: 50 }
+ },
+ comments: [{
+ type: 'Block',
+ value: ' perfect ',
+ range: [35, 48],
+ loc: {
+ start: { line: 1, column: 35 },
+ end: { line: 1, column: 48 }
+ }
+ }]
+ },
+
'<!-- foo': {
type: 'Program',
body: [],
Please sign in to comment.
Something went wrong with that request. Please try again.