Skip to content

Commit 49a1cd9

Browse files
committed
Clean up @example parsing and expand tests - back to 100% coverage
1 parent e0591ca commit 49a1cd9

12 files changed

+56
-62
lines changed

lib/lint.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,6 @@ 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-
4637
function checkCanonical(type) {
4738
if (type.type === 'NameExpression') {
4839
nameInvariant(type.name);
@@ -60,10 +51,6 @@ function lintComments(comment) {
6051
if (tag.title === 'param' && tag.type) {
6152
checkCanonical(tag.type);
6253
}
63-
64-
if (tag.title === 'example') {
65-
checkEmptyExample(tag);
66-
}
6754
});
6855
return comment;
6956
}

lib/parse.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,27 @@ var flatteners = {
3030
'emits': synonym('fires'),
3131
'event': flattenDescription,
3232
'example': function (result, tag) {
33+
if (!tag.description) {
34+
result.errors.push({
35+
message: '@example without code',
36+
commentLineNumber: tag.lineNumber
37+
});
38+
return;
39+
}
40+
3341
if (!result.examples) {
3442
result.examples = [];
3543
}
36-
result.examples.push(tag);
44+
45+
var example = {
46+
description: tag.description
47+
};
48+
49+
if (tag.caption) {
50+
example.caption = tag.caption;
51+
}
52+
53+
result.examples.push(example);
3754
},
3855
'exception': synonym('throws'),
3956
'extends': synonym('augments'),

test/fixture/_multi-file-input.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@
7676
],
7777
"examples": [
7878
{
79-
"title": "example",
80-
"description": "var result = returnTwo(4);\n// result is 6",
81-
"lineNumber": 5
79+
"description": "var result = returnTwo(4);\n// result is 6"
8280
}
8381
],
8482
"name": "returnTwo",

test/fixture/empty-example.output.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@
3131
},
3232
"code": "/**\n * This function returns the number plus two.\n *\n * @example\n */\nfunction returnTwo() {\n return a + 2;\n}\n"
3333
},
34-
"errors": [],
35-
"examples": [
34+
"errors": [
3635
{
37-
"title": "example",
38-
"description": "",
39-
"lineNumber": 3
36+
"message": "@example without code",
37+
"commentLineNumber": 3
4038
}
4139
],
4240
"name": "returnTwo",
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
# returnTwo
22

33
This function returns the number plus two.
4-
5-
**Examples**
6-
7-
```javascript
8-
9-
```

test/fixture/empty-example.output.md.json

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,6 @@
4141
},
4242
"indent": []
4343
}
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-
"value": ""
5844
}
5945
]
6046
}

test/fixture/example-caption.output.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@
5555
],
5656
"examples": [
5757
{
58-
"title": "example",
5958
"description": "foo(1);",
60-
"lineNumber": 3,
6159
"caption": "demonstrates how to run foo"
6260
}
6361
],

test/fixture/multiexample.output.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,10 @@
9191
],
9292
"examples": [
9393
{
94-
"title": "example",
95-
"description": "foo(1);",
96-
"lineNumber": 3
94+
"description": "foo(1);"
9795
},
9896
{
99-
"title": "example",
100-
"description": "foo(2);",
101-
"lineNumber": 5
97+
"description": "foo(2);"
10298
}
10399
],
104100
"throws": [

test/fixture/params.output.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@
150150
],
151151
"examples": [
152152
{
153-
"title": "example",
154-
"description": "var address = new Address6('2001::/32');",
155-
"lineNumber": 9
153+
"description": "var address = new Address6('2001::/32');"
156154
}
157155
],
158156
"name": "Address6",

test/fixture/simple-two.output.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@
7676
],
7777
"examples": [
7878
{
79-
"title": "example",
80-
"description": "var result = returnTwo(4);\n// result is 6",
81-
"lineNumber": 5
79+
"description": "var result = returnTwo(4);\n// result is 6"
8280
}
8381
],
8482
"name": "returnTwo",

0 commit comments

Comments
 (0)