Skip to content

Commit dad509d

Browse files
shlomiassafaciccarello
authored andcommitted
dont parse comment for tags inside a code block (TypeStrong#633)
1 parent 34377dc commit dad509d

File tree

3 files changed

+150
-5
lines changed

3 files changed

+150
-5
lines changed

src/lib/converter/factories/comment.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,23 @@ export function parseComment(text: string, comment: Comment = new Comment()): Co
160160
comment.tags.push(currentTag);
161161
}
162162

163+
const CODE_FENCE = /^\s*```(?!.*```)/;
164+
let inCode = false;
163165
function readLine(line: string) {
164166
line = line.replace(/^\s*\*? ?/, '');
165167
line = line.replace(/\s*$/, '');
166168

167-
const tag = /^@(\S+)/.exec(line);
168-
if (tag) {
169-
readTagLine(line, tag);
170-
} else {
171-
readBareLine(line);
169+
if (CODE_FENCE.test(line) ) {
170+
inCode = !inCode;
171+
}
172+
173+
if (!inCode) {
174+
const tag = /^@(\S+)/.exec(line);
175+
if (tag) {
176+
return readTagLine(line, tag);
177+
}
172178
}
179+
readBareLine(line);
173180
}
174181

175182
// text = text.replace(/^\s*\/\*+\s*(\r\n?|\n)/, '');

src/test/converter/comment/comment.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* A Comment for a class
3+
*
4+
* ## Some Markup
5+
* **with more markup**
6+
*
7+
* A example with decorators that should not parse to tag
8+
* ```
9+
* @myDecorator
10+
* @FactoryDecorator('a', 'b', 'c')
11+
* export class CommentedClass {
12+
* myProp: string = 'myProp';
13+
*
14+
* @PropDecorator() decoratedProp: string;
15+
*
16+
* constructor(@ParamDecorator public param: string) { }
17+
*
18+
* myMethod() { }
19+
* }
20+
* ```
21+
* @deprecated
22+
* @todo something
23+
*/
24+
export class CommentedClass {
25+
/**
26+
* The main prop
27+
*/
28+
prop: string;
29+
}

src/test/converter/comment/specs.json

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"id": 0,
3+
"name": "typedoc",
4+
"kind": 0,
5+
"flags": {},
6+
"children": [
7+
{
8+
"id": 1,
9+
"name": "\"comment\"",
10+
"kind": 1,
11+
"kindString": "External module",
12+
"flags": {
13+
"isExported": true
14+
},
15+
"originalName": "%BASE%/comment/comment.ts",
16+
"children": [
17+
{
18+
"id": 2,
19+
"name": "CommentedClass",
20+
"kind": 128,
21+
"kindString": "Class",
22+
"flags": {
23+
"isExported": true
24+
},
25+
"comment": {
26+
"shortText": "A Comment for a class",
27+
"text": "## Some Markup\n**with more markup**\n\nA example with decorators that should not parse to tag\n```\n@myDecorator\n@FactoryDecorator('a', 'b', 'c')\nexport class CommentedClass {\n myProp: string = 'myProp';\n\n @PropDecorator() decoratedProp: string;\n\n constructor(@ParamDecorator public param: string) { }\n\n myMethod() { }\n}\n```",
28+
"tags": [
29+
{
30+
"tag": "deprecated",
31+
"text": ""
32+
},
33+
{
34+
"tag": "todo",
35+
"text": "something\n"
36+
}
37+
]
38+
},
39+
"children": [
40+
{
41+
"id": 3,
42+
"name": "prop",
43+
"kind": 1024,
44+
"kindString": "Property",
45+
"flags": {
46+
"isExported": true
47+
},
48+
"comment": {
49+
"shortText": "The main prop"
50+
},
51+
"sources": [
52+
{
53+
"fileName": "comment.ts",
54+
"line": 28,
55+
"character": 6
56+
}
57+
],
58+
"type": {
59+
"type": "intrinsic",
60+
"name": "string"
61+
}
62+
}
63+
],
64+
"groups": [
65+
{
66+
"title": "Properties",
67+
"kind": 1024,
68+
"children": [
69+
3
70+
]
71+
}
72+
],
73+
"sources": [
74+
{
75+
"fileName": "comment.ts",
76+
"line": 24,
77+
"character": 27
78+
}
79+
]
80+
}
81+
],
82+
"groups": [
83+
{
84+
"title": "Classes",
85+
"kind": 128,
86+
"children": [
87+
2
88+
]
89+
}
90+
],
91+
"sources": [
92+
{
93+
"fileName": "comment.ts",
94+
"line": 1,
95+
"character": 0
96+
}
97+
]
98+
}
99+
],
100+
"groups": [
101+
{
102+
"title": "External modules",
103+
"kind": 1,
104+
"children": [
105+
1
106+
]
107+
}
108+
]
109+
}

0 commit comments

Comments
 (0)