Skip to content

Commit 4293a0c

Browse files
authored
Merge pull request bcherny#26 from darcyparker/multiLineDesc
Multiple line description support
2 parents e3aef97 + aef14ff commit 4293a0c

File tree

6 files changed

+63
-8
lines changed

6 files changed

+63
-8
lines changed

src/TsTypes.ts

+23-3
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,23 @@ 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+
//All descriptions will be inside jsdoc-style comments to support hinting in editors
205+
//(ie intellisense)
190206
if (settings.propertyDescription && _.type.description && !_.type.id)
191-
decl += ' // ' + _.type.description
207+
decl = _.type.description
208+
.split(newLineRegex)
209+
.map((line, lineNum, lines) => (lineNum > 0 ? multiLineCommentIndent : indentString + multiLineCommentStart) + line)
210+
.join('\n' + indentString) + multiLineCommentEnd + '\n' + decl
211+
192212
return decl
193213
}).join('\n')}
194214
}` : id

test/cases/basics.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export var configurations = [
3434
types: `export interface ExampleSchema {
3535
firstName: string;
3636
lastName: string;
37-
age?: number; // Age in years
37+
/** Age in years */
38+
age?: number;
3839
height?: number;
3940
favoriteFoods?: any[];
4041
likesDogs?: boolean;

test/cases/not-extendable.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ export var schema = {
2121
export var types = `export interface ExampleSchema {
2222
firstName: string;
2323
lastName: string;
24-
age?: number; // Age in years
24+
/** Age in years */
25+
age?: number;
2526
}`

test/cases/ref.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export var configurations = [
1919
types: `export interface ExampleSchema {
2020
firstName: string;
2121
lastName: string;
22-
age?: number; // Age in years
22+
/** Age in years */
23+
age?: number;
2324
height?: number;
2425
favoriteFoods?: any[];
2526
likesDogs?: boolean;
@@ -37,4 +38,4 @@ export interface Referencing {
3738
foo: ExampleSchema;
3839
}`
3940
}
40-
]
41+
]
+31
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+
/** first name single line description */
25+
firstName: string;
26+
lastName: string;
27+
/** Age description with
28+
* multiple lines */
29+
age?: number;
30+
[k: string]: any;
31+
}`

test/cases/with-description.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export var types = `/** My cool schema */
2222
export interface ExampleSchema {
2323
firstName: string;
2424
lastName: string;
25-
age?: number; // Age in years
25+
/** Age in years */
26+
age?: number;
2627
[k: string]: any;
2728
}`

0 commit comments

Comments
 (0)