Skip to content

Commit 2043610

Browse files
committed
Merge pull request #330 from lavrton/empty-example-fail-fix
Fix throwing error when "example" tag is empty
2 parents 39b0ae4 + 9356ac5 commit 2043610

File tree

6 files changed

+137
-2
lines changed

6 files changed

+137
-2
lines changed

lib/lint.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ function lintComments(comment) {
3434
}
3535
}
3636

37+
function checkEmptyExample(tag) {
38+
if (!tag.description) {
39+
comment.errors.push({
40+
message: 'empty example is found',
41+
commentLineNumber: tag.lineNumber
42+
});
43+
}
44+
}
45+
3746
function checkCanonical(type) {
3847
if (type.type === 'NameExpression') {
3948
nameInvariant(type.name);
@@ -51,6 +60,10 @@ function lintComments(comment) {
5160
if (tag.title === 'param' && tag.type) {
5261
checkCanonical(tag.type);
5362
}
63+
64+
if (tag.title === 'example') {
65+
checkEmptyExample(tag);
66+
}
5467
});
5568
return comment;
5669
}

lib/output/html.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ var walk = require('../walk'),
1414
function highlightString(auto) {
1515
return function (example) {
1616
if (auto) {
17-
return hljs.highlightAuto(example).value;
17+
return hljs.highlightAuto(example || '').value;
1818
}
19-
return hljs.highlight('js', example).value;
19+
return hljs.highlight('js', example || '').value;
2020
};
2121
}
2222

test/fixture/empty-example.input.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* This function returns the number plus two.
3+
*
4+
* @example
5+
*/
6+
function returnTwo() {
7+
return a + 2;
8+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[
2+
{
3+
"description": "This function returns the number plus two.",
4+
"tags": [
5+
{
6+
"title": "example",
7+
"description": null,
8+
"lineNumber": 3
9+
}
10+
],
11+
"loc": {
12+
"start": {
13+
"line": 1,
14+
"column": 0
15+
},
16+
"end": {
17+
"line": 5,
18+
"column": 3
19+
}
20+
},
21+
"context": {
22+
"loc": {
23+
"start": {
24+
"line": 6,
25+
"column": 0
26+
},
27+
"end": {
28+
"line": 8,
29+
"column": 1
30+
}
31+
},
32+
"code": "/**\n * This function returns the number plus two.\n *\n * @example\n */\nfunction returnTwo() {\n return a + 2;\n}\n"
33+
},
34+
"errors": [],
35+
"examples": [null],
36+
"name": "returnTwo",
37+
"kind": "function",
38+
"members": {
39+
"instance": [],
40+
"static": []
41+
},
42+
"path": [
43+
"returnTwo"
44+
]
45+
}
46+
]

test/fixture/empty-example.output.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# returnTwo
2+
3+
This function returns the number plus two.
4+
5+
**Examples**
6+
7+
```javascript
8+
undefined
9+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"type": "root",
3+
"children": [
4+
{
5+
"depth": 1,
6+
"type": "heading",
7+
"children": [
8+
{
9+
"type": "text",
10+
"value": "returnTwo"
11+
}
12+
]
13+
},
14+
{
15+
"type": "paragraph",
16+
"children": [
17+
{
18+
"type": "text",
19+
"value": "This function returns the number plus two.",
20+
"position": {
21+
"start": {
22+
"line": 1,
23+
"column": 1
24+
},
25+
"end": {
26+
"line": 1,
27+
"column": 43
28+
},
29+
"indent": []
30+
}
31+
}
32+
],
33+
"position": {
34+
"start": {
35+
"line": 1,
36+
"column": 1
37+
},
38+
"end": {
39+
"line": 1,
40+
"column": 43
41+
},
42+
"indent": []
43+
}
44+
},
45+
{
46+
"type": "strong",
47+
"children": [
48+
{
49+
"type": "text",
50+
"value": "Examples"
51+
}
52+
]
53+
},
54+
{
55+
"lang": "javascript",
56+
"type": "code"
57+
}
58+
]
59+
}

0 commit comments

Comments
 (0)