Skip to content

Commit 509b9ad

Browse files
Complete to single line jsdoc comment if no params
1 parent 976c25c commit 509b9ad

13 files changed

Lines changed: 99 additions & 127 deletions

src/services/jsDoc.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ namespace ts.JsDoc {
212212
return emptyDocComment;
213213
}
214214

215+
if (!parameters || parameters.length === 0) {
216+
// if there are no parameters, just complete to a single line JSDoc comment
217+
const singleLineResult = "/** */";
218+
return { newText: singleLineResult, caretOffset: 3 };
219+
}
220+
215221
const posLineAndChar = sourceFile.getLineAndCharacterOfPosition(position);
216222
const lineStart = sourceFile.getLineStarts()[posLineAndChar.line];
217223

@@ -220,18 +226,16 @@ namespace ts.JsDoc {
220226
const isJavaScriptFile = hasJavaScriptFileExtension(sourceFile.fileName);
221227

222228
let docParams = "";
223-
if (parameters) {
224-
for (let i = 0; i < parameters.length; i++) {
225-
const currentName = parameters[i].name;
226-
const paramName = currentName.kind === SyntaxKind.Identifier ?
227-
(<Identifier>currentName).escapedText :
228-
"param" + i;
229-
if (isJavaScriptFile) {
230-
docParams += `${indentationStr} * @param {any} ${paramName}${newLine}`;
231-
}
232-
else {
233-
docParams += `${indentationStr} * @param ${paramName}${newLine}`;
234-
}
229+
for (let i = 0; i < parameters.length; i++) {
230+
const currentName = parameters[i].name;
231+
const paramName = currentName.kind === SyntaxKind.Identifier ?
232+
(<Identifier>currentName).escapedText :
233+
"param" + i;
234+
if (isJavaScriptFile) {
235+
docParams += `${indentationStr} * @param {any} ${paramName}${newLine}`;
236+
}
237+
else {
238+
docParams += `${indentationStr} * @param ${paramName}${newLine}`;
235239
}
236240
}
237241

@@ -258,8 +262,6 @@ namespace ts.JsDoc {
258262
readonly parameters?: ReadonlyArray<ParameterDeclaration>;
259263
}
260264
function getCommentOwnerInfo(tokenAtPos: Node): CommentOwnerInfo | undefined {
261-
// TODO: add support for:
262-
// - potentially property assignments
263265
for (let commentOwner = tokenAtPos; commentOwner; commentOwner = commentOwner.parent) {
264266
switch (commentOwner.kind) {
265267
case SyntaxKind.FunctionDeclaration:
@@ -274,6 +276,7 @@ namespace ts.JsDoc {
274276
case SyntaxKind.PropertySignature:
275277
case SyntaxKind.EnumDeclaration:
276278
case SyntaxKind.EnumMember:
279+
case SyntaxKind.TypeAliasDeclaration:
277280
return { commentOwner };
278281

279282
case SyntaxKind.VariableStatement: {

tests/cases/fourslash/docCommentTemplateClassDecl01.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,5 @@
1111
//// }
1212
////}
1313

14-
verify.docCommentTemplateAt("decl", /*newTextOffset*/ 8,
15-
`/**
16-
*
17-
*/
18-
`);
14+
verify.docCommentTemplateAt("decl", /*newTextOffset*/ 3,
15+
"/** */");

tests/cases/fourslash/docCommentTemplateClassDeclMethods01.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
const enum Indentation {
4-
Standard = 8,
4+
Standard = 3,
55
Indented = 12,
66
}
77

@@ -17,15 +17,11 @@ const enum Indentation {
1717
////}
1818

1919
verify.docCommentTemplateAt("0", Indentation.Standard,
20-
`/**
21-
*
22-
*/`);
20+
"/** */");
2321

2422

25-
verify.docCommentTemplateAt("1", Indentation.Indented,
26-
`/**
27-
*
28-
*/`);
23+
verify.docCommentTemplateAt("1", Indentation.Standard,
24+
"/** */");
2925

3026

3127
verify.docCommentTemplateAt("2", Indentation.Indented,
@@ -51,7 +47,7 @@ verify.docCommentTemplateAt("4", Indentation.Indented,
5147
* @param param2
5248
*/`);
5349

54-
verify.docCommentTemplateAt("5", Indentation.Indented,
50+
verify.docCommentTemplateAt("5", Indentation.Indented,
5551
`/**
5652
*
5753
* @param a

tests/cases/fourslash/docCommentTemplateClassDeclMethods02.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
const enum Indentation {
4+
Standard = 3,
45
Indented = 12,
56
}
67

@@ -13,10 +14,8 @@ const enum Indentation {
1314
//// [1 + 2 + 3 + Math.rand()](x: number, y: string, z = true) { }
1415
////}
1516

16-
verify.docCommentTemplateAt("0", Indentation.Indented,
17-
`/**
18-
*
19-
*/`);
17+
verify.docCommentTemplateAt("0", Indentation.Standard,
18+
"/** */");
2019

2120
verify.docCommentTemplateAt("1", Indentation.Indented,
2221
`/**

tests/cases/fourslash/docCommentTemplateIndentation.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@
55
//// /*1*/
66
/////*0*/ function foo() { }
77

8-
const noIndentEmptyScaffolding = "/**\r\n * \r\n */";
9-
const oneIndentEmptyScaffolding = "/**\r\n * \r\n */";
10-
const twoIndentEmptyScaffolding = "/**\r\n * \r\n */";
11-
const noIndentOffset = 8;
12-
const oneIndentOffset = noIndentOffset + 4;
13-
const twoIndentOffset = oneIndentOffset + 4;
8+
const singleLineComment = "/** */";
149

15-
verify.docCommentTemplateAt("0", noIndentOffset, noIndentEmptyScaffolding);
16-
verify.docCommentTemplateAt("1", oneIndentOffset, oneIndentEmptyScaffolding);
17-
verify.docCommentTemplateAt("2", twoIndentOffset, twoIndentEmptyScaffolding);
10+
verify.docCommentTemplateAt("0", 3, singleLineComment);
11+
verify.docCommentTemplateAt("1", 3, singleLineComment);
12+
verify.docCommentTemplateAt("2", 3, singleLineComment);

tests/cases/fourslash/docCommentTemplateInterfacesAndEnums.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
/////*interfaceFoo*/
4+
////interface Foo {
5+
//// /*propertybar*/
6+
//// bar: any;
7+
////
8+
//// /*methodbaz*/
9+
//// baz(message: any): void;
10+
////
11+
//// /*methodUnit*/
12+
//// unit(): void;
13+
////}
14+
////
15+
/////*enumStatus*/
16+
////const enum Status {
17+
//// /*memberOpen*/
18+
//// Open,
19+
////
20+
//// /*memberClosed*/
21+
//// Closed
22+
////}
23+
////
24+
/////*aliasBar*/
25+
////type Bar = Foo & any;
26+
27+
verify.docCommentTemplateAt("interfaceFoo", /*expectedOffset*/ 3,
28+
"/** */");
29+
30+
verify.docCommentTemplateAt("propertybar", /*expectedOffset*/ 3,
31+
"/** */");
32+
33+
verify.docCommentTemplateAt("methodbaz", /*expectedOffset*/ 12,
34+
`/**
35+
*
36+
* @param message
37+
*/`);
38+
39+
verify.docCommentTemplateAt("methodUnit", /*expectedOffset*/ 3,
40+
"/** */");
41+
42+
verify.docCommentTemplateAt("enumStatus", /*expectedOffset*/ 3,
43+
"/** */");
44+
45+
verify.docCommentTemplateAt("memberOpen", /*expectedOffset*/ 3,
46+
"/** */");
47+
48+
verify.docCommentTemplateAt("memberClosed", /*expectedOffset*/ 3,
49+
"/** */");

tests/cases/fourslash/docCommentTemplateNamespacesAndModules01.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,11 @@
1212
////module "ambientModule" {
1313
////}
1414

15-
verify.docCommentTemplateAt("namespaceN", /*indentation*/ 8,
16-
`/**
17-
*
18-
*/`);
15+
verify.docCommentTemplateAt("namespaceN", /*indentation*/ 3,
16+
"/** */");
1917

20-
verify.docCommentTemplateAt("namespaceM", /*indentation*/ 8,
21-
`/**
22-
*
23-
*/`);
18+
verify.docCommentTemplateAt("namespaceM", /*indentation*/ 3,
19+
"/** */");
2420

25-
verify.docCommentTemplateAt("namespaceM", /*indentation*/ 8,
26-
`/**
27-
*
28-
*/`);
21+
verify.docCommentTemplateAt("namespaceM", /*indentation*/ 3,
22+
"/** */");

tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
//// /*n3*/ n3 {
77
////}
88

9-
verify.docCommentTemplateAt("top", /*indentation*/ 8,
10-
`/**
11-
*
12-
*/`);
9+
verify.docCommentTemplateAt("top", /*indentation*/ 3,
10+
"/** */");
1311

1412
verify.emptyDocCommentTemplateAt("n2");
1513

tests/cases/fourslash/docCommentTemplateObjectLiteralMethods01.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
const enum Indentation {
4+
Standard = 3,
45
Indented = 12,
56
}
67

@@ -13,10 +14,8 @@ const enum Indentation {
1314
//// [1 + 2 + 3 + Math.rand()](x: number, y: string, z = true) { }
1415
////}
1516

16-
verify.docCommentTemplateAt("0", Indentation.Indented,
17-
`/**
18-
*
19-
*/`);
17+
verify.docCommentTemplateAt("0", Indentation.Standard,
18+
"/** */");
2019

2120
verify.docCommentTemplateAt("1", Indentation.Indented,
2221
`/**

0 commit comments

Comments
 (0)