|
1 | 1 | import { camelCase, upperFirst } from 'lodash'
|
2 | 2 |
|
| 3 | +const multiLineCommentStart = '/** ' |
| 4 | +const multiLineCommentIndent = ' * ' |
| 5 | +const multiLineCommentEnd = ' */' |
| 6 | +const newLineRegex = /\\n|\n/ |
| 7 | + |
3 | 8 | export namespace TsType {
|
4 | 9 |
|
5 | 10 | export interface TsTypeSettings {
|
@@ -36,7 +41,13 @@ export namespace TsType {
|
36 | 41 | return this.id && upperFirst(camelCase(this.id))
|
37 | 42 | }
|
38 | 43 | 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 | + '' |
40 | 51 | }
|
41 | 52 | protected _toDeclaration(decl: string, settings: TsTypeSettings): string {
|
42 | 53 | return this.toBlockComment(settings) + decl + (settings.endTypeWithSemicolon ? ';' : '')
|
@@ -181,14 +192,26 @@ export namespace TsType {
|
181 | 192 | let id = this.safeId()
|
182 | 193 | return declaration || !id ? `{
|
183 | 194 | ${this.props.map(_ => {
|
184 |
| - let decl = ' ' + _.name |
| 195 | + const indentString = ' ' |
| 196 | + let decl = indentString + _.name |
| 197 | +
|
185 | 198 | if (!_.required)
|
186 | 199 | decl += '?'
|
187 | 200 | decl += ': ' + _.type.toType(settings)
|
188 | 201 | if (settings.endPropertyWithSemicolon)
|
189 | 202 | decl += ';'
|
| 203 | +
|
| 204 | + //Single line description in comment to the right of declaration |
| 205 | + //Multiple line description in comment above |
190 | 206 | 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 | +
|
192 | 215 | return decl
|
193 | 216 | }).join('\n')}
|
194 | 217 | }` : id
|
|
0 commit comments