Skip to content

Commit 1a25a4e

Browse files
fix(doclint): support lists in comments (#1492)
Adds logging comments to the doclint tests, and adds a new one with a bulleted list in a comment. Lists can only be used in comments where extra properties would be unexpected.
1 parent 6390645 commit 1a25a4e

File tree

5 files changed

+70
-8
lines changed

5 files changed

+70
-8
lines changed

utils/doclint/check_public_api/MDBuilder.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ class MDOutline {
3030
const writer = new commonmark.HtmlRenderer();
3131
const html = writer.render(parsed);
3232

33-
page.on('console', msg => {
34-
console.log(msg.text());
35-
});
33+
const logConsole = msg => console.log(msg.text());
34+
page.on('console', logConsole);
3635
// Extract headings.
3736
await page.setContent(html);
3837
const {classes, errors} = await page.evaluate(() => {
@@ -56,9 +55,11 @@ class MDOutline {
5655
const name = str.substring(0, str.indexOf('<')).replace(/\`/g, '').trim();
5756
const type = findType(str);
5857
const properties = [];
59-
const comment = str.substring(str.indexOf('<') + type.length + 2).trim();
58+
let comment = str.substring(str.indexOf('<') + type.length + 2).trim();
6059
const hasNonEnumProperties = type.split('|').some(part => {
61-
return part !== 'string' && part !== 'number' && part !== 'Array<string>' && !(part[0] === '"' && part[part.length - 1] === '"');
60+
const basicTypes = new Set(['string', 'number', 'boolean']);
61+
const arrayTypes = new Set([...basicTypes].map(type => `Array<${type}>`));
62+
return !basicTypes.has(part) && !arrayTypes.has(part) && !(part.startsWith('"') && part.endsWith('"'));
6263
});
6364
if (hasNonEnumProperties) {
6465
for (const childElement of element.querySelectorAll(':scope > ul > li')) {
@@ -77,6 +78,8 @@ class MDOutline {
7778
property.required = true;
7879
properties.push(property);
7980
}
81+
} else if (ul) {
82+
comment += '\n' + parseComment(ul).split('\n').map(l => ` - ${l}`).join('\n');
8083
}
8184
return {
8285
name,
@@ -214,6 +217,7 @@ class MDOutline {
214217
return fragment;
215218
}
216219
});
220+
page.off('console', logConsole);
217221
return new MDOutline(classes, errors);
218222
}
219223

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### class: Foo
2+
3+
#### foo.method(arg1, arg2)
4+
- `arg1` <[string]> A single line argument comment
5+
- `arg2` <[string]> A multiline argument comment:
6+
- it could be this
7+
- or it could be that
8+
- returns: <[Promise]<[ElementHandle]>>
9+
10+
The method does something.
11+
12+
[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String"
13+
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise"
14+
[ElementHandle]: # "ElementHandle"
15+
[ElementHandle]: # "Frame"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"classes": [
3+
{
4+
"name": "Foo",
5+
"members": [
6+
{
7+
"name": "method",
8+
"type": {
9+
"name": "Promise<ElementHandle>"
10+
},
11+
"kind": "method",
12+
"comment": "The method does something.",
13+
"args": [
14+
{
15+
"name": "arg1",
16+
"type": {
17+
"name": "string"
18+
},
19+
"kind": "property",
20+
"comment": "A single line argument comment"
21+
},
22+
{
23+
"name": "arg2",
24+
"type": {
25+
"name": "string"
26+
},
27+
"kind": "property",
28+
"comment": "A multiline argument comment:\n - it could be this\n - or it could be that"
29+
}
30+
]
31+
}
32+
]
33+
}
34+
]
35+
}

utils/doclint/check_public_api/test/md-builder-common/result.txt

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,31 @@
22
"classes": [
33
{
44
"name": "Foo",
5+
"comment": "This is a class.",
56
"members": [
67
{
78
"name": "frame",
89
"type": {
910
"name": "[Frame]"
1011
},
11-
"kind": "event"
12+
"kind": "event",
13+
"comment": "This event is dispatched."
1214
},
1315
{
1416
"name": "$",
1517
"type": {
1618
"name": "Promise<ElementHandle>"
1719
},
1820
"kind": "method",
21+
"comment": "The method runs document.querySelector.",
1922
"args": [
2023
{
2124
"name": "selector",
2225
"type": {
2326
"name": "string"
2427
},
25-
"kind": "property"
28+
"kind": "property",
29+
"comment": "A selector to query page for"
2630
}
2731
]
2832
},
@@ -31,7 +35,8 @@
3135
"type": {
3236
"name": "string"
3337
},
34-
"kind": "property"
38+
"kind": "property",
39+
"comment": "Contains the URL of the request."
3540
}
3641
]
3742
}

utils/doclint/check_public_api/test/test.js

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe('checkPublicAPI', function() {
5555
it('js-builder-common', testJSBuilder);
5656
it('js-builder-inheritance', testJSBuilder);
5757
it('md-builder-common', testMDBuilder);
58+
it('md-builder-comments', testMDBuilder);
5859
});
5960

6061
runner.run();
@@ -101,6 +102,7 @@ function serialize(doc) {
101102
const result = {
102103
classes: doc.classesArray.map(cls => ({
103104
name: cls.name,
105+
comment: cls.comment || undefined,
104106
members: cls.membersArray.map(serializeMember)
105107
}))
106108
};
@@ -114,6 +116,7 @@ function serializeMember(member) {
114116
name: member.name,
115117
type: serializeType(member.type),
116118
kind: member.kind,
119+
comment: member.comment || undefined,
117120
args: member.argsArray.length ? member.argsArray.map(serializeMember) : undefined
118121
}
119122
}

0 commit comments

Comments
 (0)