Skip to content

Commit

Permalink
Attaching leading comments fixed
Browse files Browse the repository at this point in the history
Last saved leading comment of lastChild can be inside of current node range, but previous comments can be suitable for it

Closes jquerygh-1071
Closes jquerygh-1205
  • Loading branch information
jifeon authored and ariya committed Jun 28, 2015
1 parent 03ba6f7 commit b8eeaf2
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 3 deletions.
16 changes: 13 additions & 3 deletions esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -1742,9 +1742,19 @@
}

if (lastChild) {
if (lastChild.leadingComments && lastChild.leadingComments[lastChild.leadingComments.length - 1].range[1] <= this.range[0]) {
this.leadingComments = lastChild.leadingComments;
lastChild.leadingComments = undefined;
if (lastChild.leadingComments) {
leadingComments = [];
for (i = lastChild.leadingComments.length - 1; i >= 0; --i) {
comment = lastChild.leadingComments[i];
if (comment.range[1] <= this.range[0]) {
leadingComments.unshift(comment);
lastChild.leadingComments.splice(i, 1);
}
}

if (!lastChild.leadingComments.length) {
lastChild.leadingComments = undefined;
}
}
} else if (extra.leadingComments.length > 0) {
leadingComments = [];
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/comment/migrated_0051.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(/* comment */{
/* comment 2 */
p1: null
})
91 changes: 91 additions & 0 deletions test/fixtures/comment/migrated_0051.tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"range": [
0,
51
],
"type": "Program",
"body": [
{
"range": [
0,
51
],
"type": "ExpressionStatement",
"expression": {
"range": [
14,
50
],
"type": "ObjectExpression",
"properties": [
{
"range": [
40,
48
],
"type": "Property",
"key": {
"range": [
40,
42
],
"type": "Identifier",
"name": "p1"
},
"computed": false,
"value": {
"range": [
44,
48
],
"type": "Literal",
"value": null,
"raw": "null"
},
"kind": "init",
"method": false,
"shorthand": false,
"leadingComments": [
{
"type": "Block",
"value": " comment 2 ",
"range": [
20,
35
]
}
]
}
],
"leadingComments": [
{
"type": "Block",
"value": " comment ",
"range": [
1,
14
]
}
]
}
}
],
"comments": [
{
"type": "Block",
"value": " comment ",
"range": [
1,
14
]
},
{
"type": "Block",
"value": " comment 2 ",
"range": [
20,
35
]
}
]
}
1 change: 1 addition & 0 deletions test/fixtures/comment/migrated_0052.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/**/ function a() {}
56 changes: 56 additions & 0 deletions test/fixtures/comment/migrated_0052.tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"range": [
5,
20
],
"type": "Program",
"body": [
{
"range": [
5,
20
],
"type": "FunctionDeclaration",
"id": {
"range": [
14,
15
],
"type": "Identifier",
"name": "a"
},
"params": [],
"defaults": [],
"body": {
"range": [
18,
20
],
"type": "BlockStatement",
"body": []
},
"generator": false,
"expression": false,
"leadingComments": [
{
"type": "Block",
"value": "",
"range": [
0,
4
]
}
]
}
],
"comments": [
{
"type": "Block",
"value": "",
"range": [
0,
4
]
}
]
}
1 change: 1 addition & 0 deletions test/fixtures/comment/migrated_0053.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/**/ function a() {function o() {}}
84 changes: 84 additions & 0 deletions test/fixtures/comment/migrated_0053.tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"range": [
5,
35
],
"type": "Program",
"body": [
{
"range": [
5,
35
],
"type": "FunctionDeclaration",
"id": {
"range": [
14,
15
],
"type": "Identifier",
"name": "a"
},
"params": [],
"defaults": [],
"body": {
"range": [
18,
35
],
"type": "BlockStatement",
"body": [
{
"range": [
19,
34
],
"type": "FunctionDeclaration",
"id": {
"range": [
28,
29
],
"type": "Identifier",
"name": "o"
},
"params": [],
"defaults": [],
"body": {
"range": [
32,
34
],
"type": "BlockStatement",
"body": []
},
"generator": false,
"expression": false
}
]
},
"generator": false,
"expression": false,
"leadingComments": [
{
"type": "Block",
"value": "",
"range": [
0,
4
]
}
]
}
],
"comments": [
{
"type": "Block",
"value": "",
"range": [
0,
4
]
}
]
}
1 change: 1 addition & 0 deletions test/fixtures/comment/migrated_0054.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/**/ function a() {/**/function o() {}}
102 changes: 102 additions & 0 deletions test/fixtures/comment/migrated_0054.tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"range": [
5,
39
],
"type": "Program",
"body": [
{
"range": [
5,
39
],
"type": "FunctionDeclaration",
"id": {
"range": [
14,
15
],
"type": "Identifier",
"name": "a"
},
"params": [],
"defaults": [],
"body": {
"range": [
18,
39
],
"type": "BlockStatement",
"body": [
{
"range": [
23,
38
],
"type": "FunctionDeclaration",
"id": {
"range": [
32,
33
],
"type": "Identifier",
"name": "o"
},
"params": [],
"defaults": [],
"body": {
"range": [
36,
38
],
"type": "BlockStatement",
"body": []
},
"generator": false,
"expression": false,
"leadingComments": [
{
"type": "Block",
"value": "",
"range": [
19,
23
]
}
]
}
]
},
"generator": false,
"expression": false,
"leadingComments": [
{
"type": "Block",
"value": "",
"range": [
0,
4
]
}
]
}
],
"comments": [
{
"type": "Block",
"value": "",
"range": [
0,
4
]
},
{
"type": "Block",
"value": "",
"range": [
19,
23
]
}
]
}

0 comments on commit b8eeaf2

Please sign in to comment.