Skip to content

Commit 1e4dd63

Browse files
committed
Never attach an AST to a trailing comment
1 parent 0a122b5 commit 1e4dd63

File tree

7 files changed

+249
-23
lines changed

7 files changed

+249
-23
lines changed

lib/parsers/javascript.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ module.exports = function (data) {
8686

8787
var visited = {};
8888

89-
function walkComments(ast, type) {
89+
function walkComments(ast, type, includeContext) {
9090
types.visit(ast, {
9191
visitNode: function (path) {
9292
/**
@@ -100,27 +100,33 @@ module.exports = function (data) {
100100
loc: extend({}, path.value.loc),
101101
file: data.file
102102
};
103+
// Avoid visiting the same comment twice as a leading
104+
// and trailing node
103105
var key = JSON.stringify(comment.loc);
106+
if (!visited[key]) {
107+
visited[key] = true;
108+
if (includeContext) {
109+
// This is non-enumerable so that it doesn't get stringified in
110+
// output; e.g. by the documentation binary.
111+
Object.defineProperty(context, 'ast', {
112+
enumerable: false,
113+
value: path
114+
});
104115

105-
if (visited[key]) {
106-
return;
116+
if (path.parent && path.parent.node) {
117+
context.code = code.substring
118+
.apply(code, path.parent.node.range);
119+
}
120+
} else {
121+
// Avoid the invariant of a comment with no AST by providing
122+
// an empty one.
123+
Object.defineProperty(context, 'ast', {
124+
enumerable: false,
125+
value: {}
126+
});
127+
}
128+
results.push(parse(comment.value, comment.loc, context));
107129
}
108-
109-
visited[key] = true;
110-
111-
// This is non-enumerable so that it doesn't get stringified in output; e.g. by the
112-
// documentation binary.
113-
Object.defineProperty(context, 'ast', {
114-
enumerable: false,
115-
value: path
116-
});
117-
118-
if (path.parent && path.parent.node) {
119-
context.code = code.substring
120-
.apply(code, path.parent.node.range);
121-
}
122-
123-
results.push(parse(comment.value, comment.loc, context));
124130
}
125131

126132
(path.value[type] || [])
@@ -132,8 +138,8 @@ module.exports = function (data) {
132138
});
133139
}
134140

135-
walkComments(ast, 'leadingComments');
136-
walkComments(ast, 'trailingComments');
141+
walkComments(ast, 'leadingComments', true);
142+
walkComments(ast, 'trailingComments', false);
137143

138144
return results;
139145
};

test/fixture/trailing-only.input.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function fooBaz() {
2+
return 2;
3+
}
4+
/**
5+
* this is a type
6+
* @return {number} nothing
7+
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
3+
this is a type
4+
5+
6+
Returns **number** nothing
7+
8+
9+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[
2+
{
3+
"description": "this is a type",
4+
"tags": [
5+
{
6+
"title": "returns",
7+
"description": "nothing",
8+
"lineNumber": 2,
9+
"type": {
10+
"type": "NameExpression",
11+
"name": "number"
12+
}
13+
}
14+
],
15+
"loc": {
16+
"start": {
17+
"line": 4,
18+
"column": 0
19+
},
20+
"end": {
21+
"line": 7,
22+
"column": 3
23+
}
24+
},
25+
"context": {
26+
"loc": {
27+
"start": {
28+
"line": 1,
29+
"column": 0
30+
},
31+
"end": {
32+
"line": 3,
33+
"column": 1
34+
}
35+
}
36+
},
37+
"errors": [],
38+
"returns": [
39+
{
40+
"title": "returns",
41+
"description": "nothing",
42+
"lineNumber": 2,
43+
"type": {
44+
"type": "NameExpression",
45+
"name": "number"
46+
}
47+
}
48+
],
49+
"members": {
50+
"instance": [],
51+
"static": []
52+
},
53+
"events": [],
54+
"path": [
55+
null
56+
]
57+
}
58+
]

test/fixture/trailing-only.output.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
3+
this is a type
4+
5+
6+
Returns **number** nothing
7+
8+
9+
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
{
2+
"type": "root",
3+
"children": [
4+
{
5+
"type": "root",
6+
"children": [
7+
{
8+
"depth": 1,
9+
"type": "heading",
10+
"children": [
11+
{
12+
"type": "text"
13+
}
14+
]
15+
},
16+
{
17+
"type": "root",
18+
"children": [
19+
{
20+
"type": "paragraph",
21+
"children": [
22+
{
23+
"type": "text",
24+
"value": "this is a type",
25+
"position": {
26+
"start": {
27+
"line": 1,
28+
"column": 1
29+
},
30+
"end": {
31+
"line": 1,
32+
"column": 15
33+
},
34+
"indent": []
35+
}
36+
}
37+
],
38+
"position": {
39+
"start": {
40+
"line": 1,
41+
"column": 1
42+
},
43+
"end": {
44+
"line": 1,
45+
"column": 15
46+
},
47+
"indent": []
48+
}
49+
}
50+
],
51+
"position": {
52+
"start": {
53+
"line": 1,
54+
"column": 1
55+
},
56+
"end": {
57+
"line": 1,
58+
"column": 15
59+
}
60+
}
61+
},
62+
{
63+
"type": "root",
64+
"children": [
65+
{
66+
"type": "paragraph",
67+
"children": [
68+
{
69+
"type": "text",
70+
"value": "Returns "
71+
},
72+
{
73+
"type": "strong",
74+
"children": [
75+
{
76+
"type": "text",
77+
"value": "number"
78+
}
79+
]
80+
},
81+
{
82+
"type": "text",
83+
"value": " "
84+
},
85+
{
86+
"type": "root",
87+
"children": [
88+
{
89+
"type": "paragraph",
90+
"children": [
91+
{
92+
"type": "text",
93+
"value": "nothing",
94+
"position": {
95+
"start": {
96+
"line": 1,
97+
"column": 1
98+
},
99+
"end": {
100+
"line": 1,
101+
"column": 8
102+
},
103+
"indent": []
104+
}
105+
}
106+
],
107+
"position": {
108+
"start": {
109+
"line": 1,
110+
"column": 1
111+
},
112+
"end": {
113+
"line": 1,
114+
"column": 8
115+
},
116+
"indent": []
117+
}
118+
}
119+
],
120+
"position": {
121+
"start": {
122+
"line": 1,
123+
"column": 1
124+
},
125+
"end": {
126+
"line": 1,
127+
"column": 8
128+
}
129+
}
130+
}
131+
]
132+
}
133+
]
134+
}
135+
]
136+
}
137+
]
138+
}

test/fixture/trailing.output.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
"line": 14,
3131
"column": 1
3232
}
33-
},
34-
"code": "function fooBar() {\n return 1;\n}\n/**\n * TWO\n * @return {number} something\n */\nfunction fooBaz() {\n return 2;\n}"
33+
}
3534
},
3635
"errors": [],
3736
"class": {

0 commit comments

Comments
 (0)