Skip to content

Commit 4213aed

Browse files
committed
feat: support tsdoc
issue: microsoft/tsdoc#20 #46
1 parent b5d8c9f commit 4213aed

File tree

7 files changed

+185
-25
lines changed

7 files changed

+185
-25
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Description is formatting as Markdown, so you could use any features of Markdown
181181
| jsdocVerticalAlignment | Boolean | false |
182182
| jsdocKeepUnParseAbleExampleIndent | Boolean | false |
183183
| jsdocSingleLineComment | Boolean | true |
184+
| tsdoc | Boolean | false |
184185

185186
Full up to date list and description of options can be found in Prettier help. First install plugin then run Prettier with "--help" option.
186187

@@ -207,6 +208,16 @@ Then, in your .eslintrc.json:
207208
}
208209
```
209210

211+
## Tsdoc
212+
213+
We hope to support whole tsdoc, if we missed somethings please create an issue.
214+
215+
```json
216+
{
217+
"tsdoc": true
218+
};
219+
```
220+
210221
## Contribute
211222

212223
1- Get a clone/fork of repo

src/index.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,25 @@ const options: Record<keyof JsdocOptions, SupportOption> = {
5656
default: true,
5757
description: "Should compact single line comment",
5858
},
59+
tsdoc: {
60+
name: "tsdoc",
61+
type: "boolean",
62+
category: "jsdoc",
63+
default: false,
64+
description: "Should format as tsdoc",
65+
},
5966
};
6067

6168
const defaultOptions: JsdocOptions = {
62-
jsdocParser: true,
63-
jsdocSpaces: 1,
64-
jsdocDescriptionWithDot: false,
65-
jsdocDescriptionTag: false,
66-
jsdocVerticalAlignment: false,
67-
jsdocKeepUnParseAbleExampleIndent: false,
68-
jsdocSingleLineComment: true,
69+
jsdocParser: options.jsdocParser.default as boolean,
70+
jsdocSpaces: options.jsdocSpaces.default as number,
71+
jsdocDescriptionWithDot: options.jsdocDescriptionWithDot.default as boolean,
72+
jsdocDescriptionTag: options.jsdocDescriptionTag.default as boolean,
73+
jsdocVerticalAlignment: options.jsdocVerticalAlignment.default as boolean,
74+
jsdocKeepUnParseAbleExampleIndent: options.jsdocKeepUnParseAbleExampleIndent
75+
.default as boolean,
76+
jsdocSingleLineComment: options.jsdocSingleLineComment.default as boolean,
77+
tsdoc: options.tsdoc.default as boolean,
6978
};
7079

7180
const languages = prettier

src/stringify.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const stringify = (
2929
jsdocSpaces,
3030
jsdocVerticalAlignment,
3131
jsdocDescriptionTag,
32+
tsdoc,
3233
useTabs,
3334
tabWidth,
3435
} = options;
@@ -61,8 +62,22 @@ const stringify = (
6162
}
6263
if (name) tagString += `${gap}${name}${" ".repeat(tagNameGapAdj)}`;
6364

64-
// Add description (complicated because of text wrap)
65-
if (description && tag !== EXAMPLE) {
65+
// Try to use prettier on @example tag description
66+
if (tag === EXAMPLE && !tsdoc) {
67+
const exampleCaption = description.match(/<caption>([\s\S]*?)<\/caption>/i);
68+
69+
if (exampleCaption) {
70+
description = description.replace(exampleCaption[0], "");
71+
tagString = `${tagString} ${exampleCaption[0]}`;
72+
}
73+
74+
const beginningSpace = useTabs ? "\t" : " ".repeat(tabWidth);
75+
const formattedExample = formatCode(description, beginningSpace, options);
76+
tagString += formattedExample.startsWith("\n")
77+
? formattedExample.trimEnd()
78+
: "\n" + formattedExample;
79+
} // Add description (complicated because of text wrap)
80+
else if (description) {
6681
if (useTagTitle) tagString += gap + " ".repeat(descGapAdj);
6782
if (
6883
TAGS_PEV_FORMATE_DESCRIPTION.includes(tag) ||
@@ -85,22 +100,6 @@ const stringify = (
85100
}
86101
}
87102

88-
// Try to use prettier on @example tag description
89-
if (tag === EXAMPLE) {
90-
const exampleCaption = description.match(/<caption>([\s\S]*?)<\/caption>/i);
91-
92-
if (exampleCaption) {
93-
description = description.replace(exampleCaption[0], "");
94-
tagString = `${tagString} ${exampleCaption[0]}`;
95-
}
96-
97-
const beginningSpace = useTabs ? "\t" : " ".repeat(tabWidth);
98-
const formattedExample = formatCode(description, beginningSpace, options);
99-
tagString += formattedExample.startsWith("\n")
100-
? formattedExample.trimEnd()
101-
: "\n" + formattedExample;
102-
}
103-
104103
// Add empty line after some tags if there is something below
105104
tagString += descriptionEndLine({
106105
description: tagString,

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface JsdocOptions {
99
jsdocParser: boolean;
1010
/** default is true */
1111
jsdocSingleLineComment: boolean;
12+
tsdoc: boolean;
1213
}
1314

1415
export interface AllOptions extends ParserOptions, JsdocOptions {}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`File: tsdoc.ts Options: {"tsdoc":true} 1`] = `
4+
"/**
5+
* @example Adding one adds one
6+
*
7+
* \`\`\`typescript
8+
* import plusOne from \\"plus-one\\";
9+
* import expect from \\"test-lib\\";
10+
*
11+
* const actual = plusOne(3);
12+
* expect(actual).toEqual(4);
13+
* \`\`\`
14+
*/
15+
export function plusOne(input: number) {
16+
return input + 1;
17+
}
18+
19+
/**
20+
* Parses a JSON file.
21+
*
22+
* @example Parsing a basic JSON file
23+
*
24+
* # Contents of \`file.json\`
25+
*
26+
* \`\`\`json
27+
* {
28+
* \\"exampleItem\\": \\"text\\"
29+
* }
30+
* \`\`\`
31+
*
32+
* # Usage
33+
*
34+
* \`\`\`ts
35+
* const result = parseFile(\\"file.json\\");
36+
* \`\`\`
37+
*
38+
* # Result
39+
*
40+
* \`\`\`ts
41+
* {
42+
* exampleItem:'text',
43+
* }
44+
* \`\`\`
45+
*
46+
* @param path - Full path to the file.
47+
* @returns An object containing the JSON data.
48+
*/
49+
50+
/**
51+
* Adds two numbers together.
52+
*
53+
* @example Here's a simple example:
54+
*
55+
* \`\`\`js
56+
* // Prints \\"2\\":
57+
* console.log(add(1, 1));
58+
* \`\`\`
59+
*
60+
* @example Here's an example with negative numbers:
61+
*
62+
* \`\`\`
63+
* // Prints \\"0\\":
64+
* console.log(add(1,-1));
65+
* \`\`\`
66+
*/
67+
export function add(x: number, y: number): number {
68+
return x * y;
69+
}
70+
"
71+
`;

tests/files.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ const files: {
5454
jsdocKeepUnParseAbleExampleIndent: true,
5555
},
5656
},
57+
{
58+
name: "tsdoc.ts",
59+
options: {
60+
tsdoc: true,
61+
},
62+
},
5763
];
5864

5965
for (let i = 0; i < files.length; i++) {

tests/files/tsdoc.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @example
3+
* Adding one adds one
4+
* ```typescript
5+
* import plusOne from 'plus-one'
6+
* import expect from 'test-lib'
7+
*
8+
* const actual=plusOne(3);
9+
* expect(actual).toEqual(4);
10+
* ```
11+
*/
12+
export function plusOne(input: number) {
13+
return input + 1;
14+
}
15+
16+
17+
18+
/**
19+
* Parses a JSON file.
20+
*
21+
* @param path - Full path to the file.
22+
* @returns An object containing the JSON data.
23+
*
24+
* @example Parsing a basic JSON file
25+
*
26+
* # Contents of `file.json`
27+
* ```json
28+
* {
29+
* "exampleItem":"text"
30+
* }
31+
* ```
32+
*
33+
* # Usage
34+
* ```ts
35+
* const result = parseFile("file.json");
36+
* ```
37+
*
38+
* # Result
39+
* ```ts
40+
* {
41+
* exampleItem:'text',
42+
* }
43+
* ```
44+
*/
45+
46+
/**
47+
* Adds two numbers together.
48+
* @example
49+
* Here's a simple example:
50+
* ```js
51+
* // Prints "2":
52+
* console.log(add(1,1));
53+
* ```
54+
* @example
55+
* Here's an example with negative numbers:
56+
* ```
57+
* // Prints "0":
58+
* console.log(add(1,-1));
59+
* ```
60+
*/
61+
export function add(x: number, y: number): number {
62+
return x* y
63+
}

0 commit comments

Comments
 (0)