Skip to content

Commit 09cdeca

Browse files
committed
Multiple line description support
1 parent e3aef97 commit 09cdeca

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

src/TsTypes.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { camelCase, upperFirst } from 'lodash'
22

3+
const multiLineCommentStart = '/** '
4+
const multiLineCommentIndent = ' * '
5+
const multiLineCommentEnd = ' */'
6+
const newLineRegex = /\\n|\n/
7+
38
export namespace TsType {
49

510
export interface TsTypeSettings {
@@ -36,7 +41,13 @@ export namespace TsType {
3641
return this.id && upperFirst(camelCase(this.id))
3742
}
3843
protected toBlockComment(settings: TsTypeSettings) {
39-
return this.description && settings.declarationDescription ? `/** ${this.description} */\n` : ''
44+
return this.description && settings.declarationDescription ?
45+
`${this.description
46+
.split(newLineRegex)
47+
.map((line, lineNum) => (lineNum > 0 ? multiLineCommentIndent : multiLineCommentStart) + line)
48+
.join('\n') + multiLineCommentEnd + '\n'
49+
}` :
50+
''
4051
}
4152
protected _toDeclaration(decl: string, settings: TsTypeSettings): string {
4253
return this.toBlockComment(settings) + decl + (settings.endTypeWithSemicolon ? ';' : '')
@@ -181,14 +192,26 @@ export namespace TsType {
181192
let id = this.safeId()
182193
return declaration || !id ? `{
183194
${this.props.map(_ => {
184-
let decl = ' ' + _.name
195+
const indentString = ' '
196+
let decl = indentString + _.name
197+
185198
if (!_.required)
186199
decl += '?'
187200
decl += ': ' + _.type.toType(settings)
188201
if (settings.endPropertyWithSemicolon)
189202
decl += ';'
203+
204+
//Single line description in comment to the right of declaration
205+
//Multiple line description in comment above
190206
if (settings.propertyDescription && _.type.description && !_.type.id)
191-
decl += ' // ' + _.type.description
207+
decl = newLineRegex.test(_.type.description) ?
208+
_.type.description
209+
.split(newLineRegex)
210+
.map((line, lineNum) => indentString + (lineNum > 0 ? multiLineCommentIndent : multiLineCommentStart) + line)
211+
.join('\n') +
212+
'\n' + indentString + multiLineCommentEnd + '\n' + decl :
213+
decl + ' // ' + _.type.description
214+
192215
return decl
193216
}).join('\n')}
194217
}` : id
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export var schema = {
2+
"title": "Example Schema",
3+
"description": "My cool schema",
4+
"type": "object",
5+
"properties": {
6+
"firstName": {
7+
"description": "first name single line description",
8+
"type": "string"
9+
},
10+
"lastName": {
11+
"type": "string"
12+
},
13+
"age": {
14+
"description": "Age description with\nmultiple lines",
15+
"type": "integer",
16+
"minimum": 0
17+
}
18+
},
19+
"required": ["firstName", "lastName"]
20+
}
21+
22+
export var types = `/** My cool schema */
23+
export interface ExampleSchema {
24+
firstName: string; // first name single line description
25+
lastName: string;
26+
/** Age description with
27+
* multiple lines
28+
*/
29+
age?: number;
30+
[k: string]: any;
31+
}`

0 commit comments

Comments
 (0)