From 509b9ad087c7368d5c27d3fdd4d6feefeae9e8af Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Thu, 2 Nov 2017 09:55:56 -0700 Subject: [PATCH] Complete to single line jsdoc comment if no params --- src/services/jsDoc.ts | 31 ++++++------ .../docCommentTemplateClassDecl01.ts | 7 +-- .../docCommentTemplateClassDeclMethods01.ts | 14 ++---- .../docCommentTemplateClassDeclMethods02.ts | 7 ++- .../docCommentTemplateIndentation.ts | 13 ++--- .../docCommentTemplateInterfacesAndEnums.ts | 50 ------------------- ...ntTemplateInterfacesEnumsAndTypeAliases.ts | 49 ++++++++++++++++++ ...ocCommentTemplateNamespacesAndModules01.ts | 18 +++---- ...ocCommentTemplateNamespacesAndModules02.ts | 6 +-- ...ocCommentTemplateObjectLiteralMethods01.ts | 7 ++- .../docCommentTemplateVariableStatements01.ts | 6 +-- .../docCommentTemplateVariableStatements02.ts | 6 +-- .../docCommentTemplateVariableStatements03.ts | 12 ++--- 13 files changed, 99 insertions(+), 127 deletions(-) delete mode 100644 tests/cases/fourslash/docCommentTemplateInterfacesAndEnums.ts create mode 100644 tests/cases/fourslash/docCommentTemplateInterfacesEnumsAndTypeAliases.ts diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index 622463d96fa..f1e06a0ffe4 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -212,6 +212,12 @@ namespace ts.JsDoc { return emptyDocComment; } + if (!parameters || parameters.length === 0) { + // if there are no parameters, just complete to a single line JSDoc comment + const singleLineResult = "/** */"; + return { newText: singleLineResult, caretOffset: 3 }; + } + const posLineAndChar = sourceFile.getLineAndCharacterOfPosition(position); const lineStart = sourceFile.getLineStarts()[posLineAndChar.line]; @@ -220,18 +226,16 @@ namespace ts.JsDoc { const isJavaScriptFile = hasJavaScriptFileExtension(sourceFile.fileName); let docParams = ""; - if (parameters) { - for (let i = 0; i < parameters.length; i++) { - const currentName = parameters[i].name; - const paramName = currentName.kind === SyntaxKind.Identifier ? - (currentName).escapedText : - "param" + i; - if (isJavaScriptFile) { - docParams += `${indentationStr} * @param {any} ${paramName}${newLine}`; - } - else { - docParams += `${indentationStr} * @param ${paramName}${newLine}`; - } + for (let i = 0; i < parameters.length; i++) { + const currentName = parameters[i].name; + const paramName = currentName.kind === SyntaxKind.Identifier ? + (currentName).escapedText : + "param" + i; + if (isJavaScriptFile) { + docParams += `${indentationStr} * @param {any} ${paramName}${newLine}`; + } + else { + docParams += `${indentationStr} * @param ${paramName}${newLine}`; } } @@ -258,8 +262,6 @@ namespace ts.JsDoc { readonly parameters?: ReadonlyArray; } function getCommentOwnerInfo(tokenAtPos: Node): CommentOwnerInfo | undefined { - // TODO: add support for: - // - potentially property assignments for (let commentOwner = tokenAtPos; commentOwner; commentOwner = commentOwner.parent) { switch (commentOwner.kind) { case SyntaxKind.FunctionDeclaration: @@ -274,6 +276,7 @@ namespace ts.JsDoc { case SyntaxKind.PropertySignature: case SyntaxKind.EnumDeclaration: case SyntaxKind.EnumMember: + case SyntaxKind.TypeAliasDeclaration: return { commentOwner }; case SyntaxKind.VariableStatement: { diff --git a/tests/cases/fourslash/docCommentTemplateClassDecl01.ts b/tests/cases/fourslash/docCommentTemplateClassDecl01.ts index 5a96f20d2e2..342d35a3b4a 100644 --- a/tests/cases/fourslash/docCommentTemplateClassDecl01.ts +++ b/tests/cases/fourslash/docCommentTemplateClassDecl01.ts @@ -11,8 +11,5 @@ //// } ////} -verify.docCommentTemplateAt("decl", /*newTextOffset*/ 8, -`/** - * - */ -`); +verify.docCommentTemplateAt("decl", /*newTextOffset*/ 3, +"/** */"); diff --git a/tests/cases/fourslash/docCommentTemplateClassDeclMethods01.ts b/tests/cases/fourslash/docCommentTemplateClassDeclMethods01.ts index ef4c82e7df7..34e55875676 100644 --- a/tests/cases/fourslash/docCommentTemplateClassDeclMethods01.ts +++ b/tests/cases/fourslash/docCommentTemplateClassDeclMethods01.ts @@ -1,7 +1,7 @@ /// const enum Indentation { - Standard = 8, + Standard = 3, Indented = 12, } @@ -17,15 +17,11 @@ const enum Indentation { ////} verify.docCommentTemplateAt("0", Indentation.Standard, -`/** - * - */`); +"/** */"); -verify.docCommentTemplateAt("1", Indentation.Indented, - `/** - * - */`); +verify.docCommentTemplateAt("1", Indentation.Standard, +"/** */"); verify.docCommentTemplateAt("2", Indentation.Indented, @@ -51,7 +47,7 @@ verify.docCommentTemplateAt("4", Indentation.Indented, * @param param2 */`); -verify.docCommentTemplateAt("5", Indentation.Indented, +verify.docCommentTemplateAt("5", Indentation.Indented, `/** * * @param a diff --git a/tests/cases/fourslash/docCommentTemplateClassDeclMethods02.ts b/tests/cases/fourslash/docCommentTemplateClassDeclMethods02.ts index 28da24d381a..a16fbd86064 100644 --- a/tests/cases/fourslash/docCommentTemplateClassDeclMethods02.ts +++ b/tests/cases/fourslash/docCommentTemplateClassDeclMethods02.ts @@ -1,6 +1,7 @@ /// const enum Indentation { + Standard = 3, Indented = 12, } @@ -13,10 +14,8 @@ const enum Indentation { //// [1 + 2 + 3 + Math.rand()](x: number, y: string, z = true) { } ////} -verify.docCommentTemplateAt("0", Indentation.Indented, - `/** - * - */`); +verify.docCommentTemplateAt("0", Indentation.Standard, +"/** */"); verify.docCommentTemplateAt("1", Indentation.Indented, `/** diff --git a/tests/cases/fourslash/docCommentTemplateIndentation.ts b/tests/cases/fourslash/docCommentTemplateIndentation.ts index c3015a6d9dd..bc909aa0265 100644 --- a/tests/cases/fourslash/docCommentTemplateIndentation.ts +++ b/tests/cases/fourslash/docCommentTemplateIndentation.ts @@ -5,13 +5,8 @@ //// /*1*/ /////*0*/ function foo() { } -const noIndentEmptyScaffolding = "/**\r\n * \r\n */"; -const oneIndentEmptyScaffolding = "/**\r\n * \r\n */"; -const twoIndentEmptyScaffolding = "/**\r\n * \r\n */"; -const noIndentOffset = 8; -const oneIndentOffset = noIndentOffset + 4; -const twoIndentOffset = oneIndentOffset + 4; +const singleLineComment = "/** */"; -verify.docCommentTemplateAt("0", noIndentOffset, noIndentEmptyScaffolding); -verify.docCommentTemplateAt("1", oneIndentOffset, oneIndentEmptyScaffolding); -verify.docCommentTemplateAt("2", twoIndentOffset, twoIndentEmptyScaffolding); +verify.docCommentTemplateAt("0", 3, singleLineComment); +verify.docCommentTemplateAt("1", 3, singleLineComment); +verify.docCommentTemplateAt("2", 3, singleLineComment); diff --git a/tests/cases/fourslash/docCommentTemplateInterfacesAndEnums.ts b/tests/cases/fourslash/docCommentTemplateInterfacesAndEnums.ts deleted file mode 100644 index ed10ba86d98..00000000000 --- a/tests/cases/fourslash/docCommentTemplateInterfacesAndEnums.ts +++ /dev/null @@ -1,50 +0,0 @@ -/// - -/////*interfaceFoo*/ -////interface Foo { -//// /*propertybar*/ -//// bar: any; -//// -//// /*methodbaz*/ -//// baz(message: any): void; -////} -//// -/////*enumStatus*/ -////const enum Status { -//// /*memberOpen*/ -//// Open, -//// -//// /*memberClosed*/ -//// Closed -////} - -verify.docCommentTemplateAt("interfaceFoo", /*expectedOffset*/ 8, -`/** - * - */`); - -verify.docCommentTemplateAt("propertybar", /*expectedOffset*/ 12, - `/** - * - */`); - -verify.docCommentTemplateAt("methodbaz", /*expectedOffset*/ 12, - `/** - * - * @param message - */`); - -verify.docCommentTemplateAt("enumStatus", /*expectedOffset*/ 8, -`/** - * - */`); - -verify.docCommentTemplateAt("memberOpen", /*expectedOffset*/ 12, - `/** - * - */`); - -verify.docCommentTemplateAt("memberClosed", /*expectedOffset*/ 12, - `/** - * - */`); \ No newline at end of file diff --git a/tests/cases/fourslash/docCommentTemplateInterfacesEnumsAndTypeAliases.ts b/tests/cases/fourslash/docCommentTemplateInterfacesEnumsAndTypeAliases.ts new file mode 100644 index 00000000000..d0805d53255 --- /dev/null +++ b/tests/cases/fourslash/docCommentTemplateInterfacesEnumsAndTypeAliases.ts @@ -0,0 +1,49 @@ +/// + +/////*interfaceFoo*/ +////interface Foo { +//// /*propertybar*/ +//// bar: any; +//// +//// /*methodbaz*/ +//// baz(message: any): void; +//// +//// /*methodUnit*/ +//// unit(): void; +////} +//// +/////*enumStatus*/ +////const enum Status { +//// /*memberOpen*/ +//// Open, +//// +//// /*memberClosed*/ +//// Closed +////} +//// +/////*aliasBar*/ +////type Bar = Foo & any; + +verify.docCommentTemplateAt("interfaceFoo", /*expectedOffset*/ 3, + "/** */"); + +verify.docCommentTemplateAt("propertybar", /*expectedOffset*/ 3, + "/** */"); + +verify.docCommentTemplateAt("methodbaz", /*expectedOffset*/ 12, + `/** + * + * @param message + */`); + +verify.docCommentTemplateAt("methodUnit", /*expectedOffset*/ 3, + "/** */"); + +verify.docCommentTemplateAt("enumStatus", /*expectedOffset*/ 3, + "/** */"); + +verify.docCommentTemplateAt("memberOpen", /*expectedOffset*/ 3, + "/** */"); + +verify.docCommentTemplateAt("memberClosed", /*expectedOffset*/ 3, + "/** */"); \ No newline at end of file diff --git a/tests/cases/fourslash/docCommentTemplateNamespacesAndModules01.ts b/tests/cases/fourslash/docCommentTemplateNamespacesAndModules01.ts index e7e52fd5e94..f3ba46605c4 100644 --- a/tests/cases/fourslash/docCommentTemplateNamespacesAndModules01.ts +++ b/tests/cases/fourslash/docCommentTemplateNamespacesAndModules01.ts @@ -12,17 +12,11 @@ ////module "ambientModule" { ////} -verify.docCommentTemplateAt("namespaceN", /*indentation*/ 8, -`/** - * - */`); +verify.docCommentTemplateAt("namespaceN", /*indentation*/ 3, + "/** */"); -verify.docCommentTemplateAt("namespaceM", /*indentation*/ 8, -`/** - * - */`); +verify.docCommentTemplateAt("namespaceM", /*indentation*/ 3, + "/** */"); -verify.docCommentTemplateAt("namespaceM", /*indentation*/ 8, -`/** - * - */`); +verify.docCommentTemplateAt("namespaceM", /*indentation*/ 3, + "/** */"); \ No newline at end of file diff --git a/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts b/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts index 8bb14bef5df..c1b9ed23ad6 100644 --- a/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts +++ b/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts @@ -6,10 +6,8 @@ //// /*n3*/ n3 { ////} -verify.docCommentTemplateAt("top", /*indentation*/ 8, -`/** - * - */`); +verify.docCommentTemplateAt("top", /*indentation*/ 3, +"/** */"); verify.emptyDocCommentTemplateAt("n2"); diff --git a/tests/cases/fourslash/docCommentTemplateObjectLiteralMethods01.ts b/tests/cases/fourslash/docCommentTemplateObjectLiteralMethods01.ts index 2ae77d4afac..7fb6156be17 100644 --- a/tests/cases/fourslash/docCommentTemplateObjectLiteralMethods01.ts +++ b/tests/cases/fourslash/docCommentTemplateObjectLiteralMethods01.ts @@ -1,6 +1,7 @@ /// const enum Indentation { + Standard = 3, Indented = 12, } @@ -13,10 +14,8 @@ const enum Indentation { //// [1 + 2 + 3 + Math.rand()](x: number, y: string, z = true) { } ////} -verify.docCommentTemplateAt("0", Indentation.Indented, - `/** - * - */`); +verify.docCommentTemplateAt("0", Indentation.Standard, + "/** */"); verify.docCommentTemplateAt("1", Indentation.Indented, `/** diff --git a/tests/cases/fourslash/docCommentTemplateVariableStatements01.ts b/tests/cases/fourslash/docCommentTemplateVariableStatements01.ts index b6243652167..9112c9a8cab 100644 --- a/tests/cases/fourslash/docCommentTemplateVariableStatements01.ts +++ b/tests/cases/fourslash/docCommentTemplateVariableStatements01.ts @@ -29,10 +29,8 @@ ////} for (const varName of ["a", "b", "c", "d"]) { - verify.docCommentTemplateAt(varName, /*newTextOffset*/ 8, -`/** - * - */`); + verify.docCommentTemplateAt(varName, /*newTextOffset*/ 3, + "/** */"); } verify.docCommentTemplateAt("e", /*newTextOffset*/ 8, diff --git a/tests/cases/fourslash/docCommentTemplateVariableStatements02.ts b/tests/cases/fourslash/docCommentTemplateVariableStatements02.ts index f22e361f63f..8e513780aad 100644 --- a/tests/cases/fourslash/docCommentTemplateVariableStatements02.ts +++ b/tests/cases/fourslash/docCommentTemplateVariableStatements02.ts @@ -29,8 +29,6 @@ ////}, f2 = null; for (const varName of ["a", "b", "c", "d", "e", "f"]) { - verify.docCommentTemplateAt(varName, /*newTextOffset*/ 8, -`/** - * - */`); + verify.docCommentTemplateAt(varName, /*newTextOffset*/ 3, + "/** */"); } diff --git a/tests/cases/fourslash/docCommentTemplateVariableStatements03.ts b/tests/cases/fourslash/docCommentTemplateVariableStatements03.ts index 195553098f0..6971b86a312 100644 --- a/tests/cases/fourslash/docCommentTemplateVariableStatements03.ts +++ b/tests/cases/fourslash/docCommentTemplateVariableStatements03.ts @@ -49,10 +49,8 @@ verify.docCommentTemplateAt("c", /*newTextOffset*/ 8, * @param x */`); -verify.docCommentTemplateAt("d", /*newTextOffset*/ 8, -`/** - * - */`); +verify.docCommentTemplateAt("d", /*newTextOffset*/ 3, +"/** */"); verify.docCommentTemplateAt("e", /*newTextOffset*/ 8, `/** @@ -60,10 +58,8 @@ verify.docCommentTemplateAt("e", /*newTextOffset*/ 8, * @param param0 */`); -verify.docCommentTemplateAt("f", /*newTextOffset*/ 8, -`/** - * - */`); +verify.docCommentTemplateAt("f", /*newTextOffset*/ 3, +"/** */"); verify.docCommentTemplateAt("g", /*newTextOffset*/ 8, `/**