|
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,23 @@ 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 | + //All descriptions will be inside jsdoc-style comments to support hinting in editors |
| 205 | + //(ie intellisense) |
190 | 206 | 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 | +
|
192 | 212 | return decl
|
193 | 213 | }).join('\n')}
|
194 | 214 | }` : id
|
|
0 commit comments