From 22eb519b0f6c4c06c71a7c2dd351bbec530f5dd9 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Fri, 27 Oct 2017 15:33:30 -0700 Subject: [PATCH 01/12] Return empty doc comment instead of undefined --- src/services/jsDoc.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index 3e9913fc6c726..49f13d0b4c335 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -188,24 +188,26 @@ namespace ts.JsDoc { * be performed. */ export function getDocCommentTemplateAtPosition(newLine: string, sourceFile: SourceFile, position: number): TextInsertion { + const emptyDocComment = { newText: "", caretOffset: 0 }; + // Check if in a context where we don't want to perform any insertion if (isInString(sourceFile, position) || isInComment(sourceFile, position) || hasDocComment(sourceFile, position)) { - return undefined; + return emptyDocComment; } const tokenAtPos = getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ false); const tokenStart = tokenAtPos.getStart(); if (!tokenAtPos || tokenStart < position) { - return undefined; + return emptyDocComment; } const commentOwnerInfo = getCommentOwnerInfo(tokenAtPos); if (!commentOwnerInfo) { - return undefined; + return emptyDocComment; } const { commentOwner, parameters } = commentOwnerInfo; if (commentOwner.getStart() < position) { - return undefined; + return emptyDocComment; } const posLineAndChar = sourceFile.getLineAndCharacterOfPosition(position); From b566480aaaf92460b37eb0977b5c07c1c0729c85 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Fri, 27 Oct 2017 16:39:33 -0700 Subject: [PATCH 02/12] Update tests to expect empty doc comment template --- src/harness/fourslash.ts | 4 ++-- tests/cases/fourslash/docCommentTemplateEmptyFile.ts | 2 +- tests/cases/fourslash/docCommentTemplateInMultiLineComment.ts | 2 +- .../cases/fourslash/docCommentTemplateInSingleLineComment.ts | 2 +- .../fourslash/docCommentTemplateInsideFunctionDeclaration.ts | 2 +- .../fourslash/docCommentTemplateNamespacesAndModules02.ts | 4 ++-- tests/cases/fourslash/docCommentTemplateRegex.ts | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 7ba4e94902db8..79cd18da8397c 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -4050,9 +4050,9 @@ namespace FourSlashInterface { this.state.verifyDocCommentTemplate({ newText: expectedText.replace(/\r?\n/g, "\r\n"), caretOffset: expectedOffset }); } - public noDocCommentTemplateAt(marker: string | FourSlash.Marker) { + public emptyDocCommentTemplateAt(marker: string | FourSlash.Marker) { this.state.goToMarker(marker); - this.state.verifyDocCommentTemplate(/*expected*/ undefined); + this.state.verifyDocCommentTemplate({ newText: "", caretOffset: 0 }); } public rangeAfterCodeFix(expectedText: string, includeWhiteSpace?: boolean, errorCode?: number, index?: number): void { diff --git a/tests/cases/fourslash/docCommentTemplateEmptyFile.ts b/tests/cases/fourslash/docCommentTemplateEmptyFile.ts index f04653dc32838..6dcb5ef832b56 100644 --- a/tests/cases/fourslash/docCommentTemplateEmptyFile.ts +++ b/tests/cases/fourslash/docCommentTemplateEmptyFile.ts @@ -3,4 +3,4 @@ // @Filename: emptyFile.ts /////*0*/ -verify.noDocCommentTemplateAt("0"); +verify.emptyDocCommentTemplateAt("0"); diff --git a/tests/cases/fourslash/docCommentTemplateInMultiLineComment.ts b/tests/cases/fourslash/docCommentTemplateInMultiLineComment.ts index 6e749782c7dcc..dc3da4e7599db 100644 --- a/tests/cases/fourslash/docCommentTemplateInMultiLineComment.ts +++ b/tests/cases/fourslash/docCommentTemplateInMultiLineComment.ts @@ -3,4 +3,4 @@ // @Filename: justAComment.ts //// /* /*0*/ */ -verify.noDocCommentTemplateAt("0"); +verify.emptyDocCommentTemplateAt("0"); diff --git a/tests/cases/fourslash/docCommentTemplateInSingleLineComment.ts b/tests/cases/fourslash/docCommentTemplateInSingleLineComment.ts index b60fff2d59022..472d417a9ff97 100644 --- a/tests/cases/fourslash/docCommentTemplateInSingleLineComment.ts +++ b/tests/cases/fourslash/docCommentTemplateInSingleLineComment.ts @@ -9,5 +9,5 @@ //// // /*2*/ for (const marker of test.markers()) { - verify.noDocCommentTemplateAt(marker); + verify.emptyDocCommentTemplateAt(marker); } diff --git a/tests/cases/fourslash/docCommentTemplateInsideFunctionDeclaration.ts b/tests/cases/fourslash/docCommentTemplateInsideFunctionDeclaration.ts index e0ebc00dc392a..13b6ebc0df64d 100644 --- a/tests/cases/fourslash/docCommentTemplateInsideFunctionDeclaration.ts +++ b/tests/cases/fourslash/docCommentTemplateInsideFunctionDeclaration.ts @@ -4,5 +4,5 @@ ////f/*0*/unction /*1*/foo/*2*/(/*3*/) /*4*/{ /*5*/} for (const marker of test.markers()) { - verify.noDocCommentTemplateAt(marker); + verify.emptyDocCommentTemplateAt(marker); } diff --git a/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts b/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts index dad2e9745a9be..8bb14bef5dff0 100644 --- a/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts +++ b/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts @@ -11,6 +11,6 @@ verify.docCommentTemplateAt("top", /*indentation*/ 8, * */`); -verify.noDocCommentTemplateAt("n2"); +verify.emptyDocCommentTemplateAt("n2"); -verify.noDocCommentTemplateAt("n3"); +verify.emptyDocCommentTemplateAt("n3"); diff --git a/tests/cases/fourslash/docCommentTemplateRegex.ts b/tests/cases/fourslash/docCommentTemplateRegex.ts index 685c1ca5aef79..7a6af09aeb562 100644 --- a/tests/cases/fourslash/docCommentTemplateRegex.ts +++ b/tests/cases/fourslash/docCommentTemplateRegex.ts @@ -4,5 +4,5 @@ ////var regex = /*0*///*1*/asdf/*2*/ /*3*///*4*/; for (const marker of test.markers()) { - verify.noDocCommentTemplateAt(marker); + verify.emptyDocCommentTemplateAt(marker); } From 7aeb11b41ea6cdec2fe5994b4a32454876dba8b6 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Fri, 27 Oct 2017 16:46:39 -0700 Subject: [PATCH 03/12] Return doc comment template for interfaces and method signatures --- src/services/jsDoc.ts | 4 +++- .../fourslash/docCommentTemplateInterfaces.ts | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/docCommentTemplateInterfaces.ts diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index 49f13d0b4c335..0443a950e9482 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -266,10 +266,12 @@ namespace ts.JsDoc { case SyntaxKind.FunctionDeclaration: case SyntaxKind.MethodDeclaration: case SyntaxKind.Constructor: - const { parameters } = commentOwner as FunctionDeclaration | MethodDeclaration | ConstructorDeclaration; + case SyntaxKind.MethodSignature: + const { parameters } = commentOwner as FunctionDeclaration | MethodDeclaration | ConstructorDeclaration | MethodSignature; return { commentOwner, parameters }; case SyntaxKind.ClassDeclaration: + case SyntaxKind.InterfaceDeclaration: return { commentOwner }; case SyntaxKind.VariableStatement: { diff --git a/tests/cases/fourslash/docCommentTemplateInterfaces.ts b/tests/cases/fourslash/docCommentTemplateInterfaces.ts new file mode 100644 index 0000000000000..2faf49351f23b --- /dev/null +++ b/tests/cases/fourslash/docCommentTemplateInterfaces.ts @@ -0,0 +1,23 @@ +/// + +/////*interfaceFoo*/ +////interface Foo { +//// /*propertybar*/ +//// bar: any; +//// +//// /*methodbaz*/ +//// baz(message: any): void; +////} + +verify.docCommentTemplateAt("interfaceFoo", /*expectedOffset*/ 8, +`/** + * + */`); + +verify.emptyDocCommentTemplateAt("propertybar"); + +verify.docCommentTemplateAt("methodbaz", /*expectedOffset*/ 12, + `/** + * + * @param message + */`); \ No newline at end of file From 49772187e51060d0516c0b7644684c13fc05afc7 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Fri, 27 Oct 2017 16:53:24 -0700 Subject: [PATCH 04/12] Update comments --- src/services/jsDoc.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index 0443a950e9482..07db4402cbf3f 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -177,6 +177,8 @@ namespace ts.JsDoc { * - class declarations * - variable statements * - namespace declarations + * - interface declarations + * - method signatures * * Hosts should ideally check that: * - The line is all whitespace up to 'position' before performing the insertion. @@ -258,7 +260,6 @@ namespace ts.JsDoc { function getCommentOwnerInfo(tokenAtPos: Node): CommentOwnerInfo | undefined { // TODO: add support for: // - enums/enum members - // - interfaces // - property declarations // - potentially property assignments for (let commentOwner = tokenAtPos; commentOwner; commentOwner = commentOwner.parent) { From 976c25c672b9f86129ad1ee549fec7f0b3f0b9fa Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Mon, 30 Oct 2017 15:05:55 -0700 Subject: [PATCH 05/12] Add support for enums and property signatures --- src/services/jsDoc.ts | 5 +- .../fourslash/docCommentTemplateInterfaces.ts | 23 --------- .../docCommentTemplateInterfacesAndEnums.ts | 50 +++++++++++++++++++ 3 files changed, 53 insertions(+), 25 deletions(-) delete mode 100644 tests/cases/fourslash/docCommentTemplateInterfaces.ts create mode 100644 tests/cases/fourslash/docCommentTemplateInterfacesAndEnums.ts diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index 07db4402cbf3f..622463d96fa17 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -259,8 +259,6 @@ namespace ts.JsDoc { } function getCommentOwnerInfo(tokenAtPos: Node): CommentOwnerInfo | undefined { // TODO: add support for: - // - enums/enum members - // - property declarations // - potentially property assignments for (let commentOwner = tokenAtPos; commentOwner; commentOwner = commentOwner.parent) { switch (commentOwner.kind) { @@ -273,6 +271,9 @@ namespace ts.JsDoc { case SyntaxKind.ClassDeclaration: case SyntaxKind.InterfaceDeclaration: + case SyntaxKind.PropertySignature: + case SyntaxKind.EnumDeclaration: + case SyntaxKind.EnumMember: return { commentOwner }; case SyntaxKind.VariableStatement: { diff --git a/tests/cases/fourslash/docCommentTemplateInterfaces.ts b/tests/cases/fourslash/docCommentTemplateInterfaces.ts deleted file mode 100644 index 2faf49351f23b..0000000000000 --- a/tests/cases/fourslash/docCommentTemplateInterfaces.ts +++ /dev/null @@ -1,23 +0,0 @@ -/// - -/////*interfaceFoo*/ -////interface Foo { -//// /*propertybar*/ -//// bar: any; -//// -//// /*methodbaz*/ -//// baz(message: any): void; -////} - -verify.docCommentTemplateAt("interfaceFoo", /*expectedOffset*/ 8, -`/** - * - */`); - -verify.emptyDocCommentTemplateAt("propertybar"); - -verify.docCommentTemplateAt("methodbaz", /*expectedOffset*/ 12, - `/** - * - * @param message - */`); \ No newline at end of file diff --git a/tests/cases/fourslash/docCommentTemplateInterfacesAndEnums.ts b/tests/cases/fourslash/docCommentTemplateInterfacesAndEnums.ts new file mode 100644 index 0000000000000..ed10ba86d98ef --- /dev/null +++ b/tests/cases/fourslash/docCommentTemplateInterfacesAndEnums.ts @@ -0,0 +1,50 @@ +/// + +/////*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 From 509b9ad087c7368d5c27d3fdd4d6feefeae9e8af Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Thu, 2 Nov 2017 09:55:56 -0700 Subject: [PATCH 06/12] 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 622463d96fa17..f1e06a0ffe4ab 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 5a96f20d2e25d..342d35a3b4a9e 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 ef4c82e7df74f..34e5587567683 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 28da24d381a7f..a16fbd860647a 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 c3015a6d9dd0b..bc909aa0265cb 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 ed10ba86d98ef..0000000000000 --- 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 0000000000000..d0805d5325570 --- /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 e7e52fd5e9485..f3ba46605c41f 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 8bb14bef5dff0..c1b9ed23ad6d6 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 2ae77d4afac38..7fb6156be17e0 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 b624365216711..9112c9a8cab6f 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 f22e361f63f87..8e513780aadaf 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 195553098f023..6971b86a312cd 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, `/** From 12baae6c843044dd03478d04ea862d62368500c2 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Thu, 2 Nov 2017 10:59:58 -0700 Subject: [PATCH 07/12] Revert "Return empty doc comment instead of undefined" This reverts commit 22eb519b0f6c4c06c71a7c2dd351bbec530f5dd9. --- src/services/jsDoc.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index f1e06a0ffe4ab..c88fe26161325 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -190,26 +190,24 @@ namespace ts.JsDoc { * be performed. */ export function getDocCommentTemplateAtPosition(newLine: string, sourceFile: SourceFile, position: number): TextInsertion { - const emptyDocComment = { newText: "", caretOffset: 0 }; - // Check if in a context where we don't want to perform any insertion if (isInString(sourceFile, position) || isInComment(sourceFile, position) || hasDocComment(sourceFile, position)) { - return emptyDocComment; + return undefined; } const tokenAtPos = getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ false); const tokenStart = tokenAtPos.getStart(); if (!tokenAtPos || tokenStart < position) { - return emptyDocComment; + return undefined; } const commentOwnerInfo = getCommentOwnerInfo(tokenAtPos); if (!commentOwnerInfo) { - return emptyDocComment; + return undefined; } const { commentOwner, parameters } = commentOwnerInfo; if (commentOwner.getStart() < position) { - return emptyDocComment; + return undefined; } if (!parameters || parameters.length === 0) { From b17b7b9374cbfb7ac2b344b5363eefbac700ddd0 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Thu, 2 Nov 2017 11:00:30 -0700 Subject: [PATCH 08/12] Revert "Update tests to expect empty doc comment template" This reverts commit b566480aaaf92460b37eb0977b5c07c1c0729c85. --- src/harness/fourslash.ts | 4 ++-- tests/cases/fourslash/docCommentTemplateEmptyFile.ts | 2 +- tests/cases/fourslash/docCommentTemplateInMultiLineComment.ts | 2 +- .../cases/fourslash/docCommentTemplateInSingleLineComment.ts | 2 +- .../fourslash/docCommentTemplateInsideFunctionDeclaration.ts | 2 +- .../fourslash/docCommentTemplateNamespacesAndModules02.ts | 4 ++-- tests/cases/fourslash/docCommentTemplateRegex.ts | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 79cd18da8397c..7ba4e94902db8 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -4050,9 +4050,9 @@ namespace FourSlashInterface { this.state.verifyDocCommentTemplate({ newText: expectedText.replace(/\r?\n/g, "\r\n"), caretOffset: expectedOffset }); } - public emptyDocCommentTemplateAt(marker: string | FourSlash.Marker) { + public noDocCommentTemplateAt(marker: string | FourSlash.Marker) { this.state.goToMarker(marker); - this.state.verifyDocCommentTemplate({ newText: "", caretOffset: 0 }); + this.state.verifyDocCommentTemplate(/*expected*/ undefined); } public rangeAfterCodeFix(expectedText: string, includeWhiteSpace?: boolean, errorCode?: number, index?: number): void { diff --git a/tests/cases/fourslash/docCommentTemplateEmptyFile.ts b/tests/cases/fourslash/docCommentTemplateEmptyFile.ts index 6dcb5ef832b56..f04653dc32838 100644 --- a/tests/cases/fourslash/docCommentTemplateEmptyFile.ts +++ b/tests/cases/fourslash/docCommentTemplateEmptyFile.ts @@ -3,4 +3,4 @@ // @Filename: emptyFile.ts /////*0*/ -verify.emptyDocCommentTemplateAt("0"); +verify.noDocCommentTemplateAt("0"); diff --git a/tests/cases/fourslash/docCommentTemplateInMultiLineComment.ts b/tests/cases/fourslash/docCommentTemplateInMultiLineComment.ts index dc3da4e7599db..6e749782c7dcc 100644 --- a/tests/cases/fourslash/docCommentTemplateInMultiLineComment.ts +++ b/tests/cases/fourslash/docCommentTemplateInMultiLineComment.ts @@ -3,4 +3,4 @@ // @Filename: justAComment.ts //// /* /*0*/ */ -verify.emptyDocCommentTemplateAt("0"); +verify.noDocCommentTemplateAt("0"); diff --git a/tests/cases/fourslash/docCommentTemplateInSingleLineComment.ts b/tests/cases/fourslash/docCommentTemplateInSingleLineComment.ts index 472d417a9ff97..b60fff2d59022 100644 --- a/tests/cases/fourslash/docCommentTemplateInSingleLineComment.ts +++ b/tests/cases/fourslash/docCommentTemplateInSingleLineComment.ts @@ -9,5 +9,5 @@ //// // /*2*/ for (const marker of test.markers()) { - verify.emptyDocCommentTemplateAt(marker); + verify.noDocCommentTemplateAt(marker); } diff --git a/tests/cases/fourslash/docCommentTemplateInsideFunctionDeclaration.ts b/tests/cases/fourslash/docCommentTemplateInsideFunctionDeclaration.ts index 13b6ebc0df64d..e0ebc00dc392a 100644 --- a/tests/cases/fourslash/docCommentTemplateInsideFunctionDeclaration.ts +++ b/tests/cases/fourslash/docCommentTemplateInsideFunctionDeclaration.ts @@ -4,5 +4,5 @@ ////f/*0*/unction /*1*/foo/*2*/(/*3*/) /*4*/{ /*5*/} for (const marker of test.markers()) { - verify.emptyDocCommentTemplateAt(marker); + verify.noDocCommentTemplateAt(marker); } diff --git a/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts b/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts index c1b9ed23ad6d6..787e9f04481d1 100644 --- a/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts +++ b/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts @@ -9,6 +9,6 @@ verify.docCommentTemplateAt("top", /*indentation*/ 3, "/** */"); -verify.emptyDocCommentTemplateAt("n2"); +verify.noDocCommentTemplateAt("n2"); -verify.emptyDocCommentTemplateAt("n3"); +verify.noDocCommentTemplateAt("n3"); diff --git a/tests/cases/fourslash/docCommentTemplateRegex.ts b/tests/cases/fourslash/docCommentTemplateRegex.ts index 7a6af09aeb562..685c1ca5aef79 100644 --- a/tests/cases/fourslash/docCommentTemplateRegex.ts +++ b/tests/cases/fourslash/docCommentTemplateRegex.ts @@ -4,5 +4,5 @@ ////var regex = /*0*///*1*/asdf/*2*/ /*3*///*4*/; for (const marker of test.markers()) { - verify.emptyDocCommentTemplateAt(marker); + verify.noDocCommentTemplateAt(marker); } From b1b611f40adc1ced57f22d9c5ba8abf2569d7502 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Thu, 2 Nov 2017 11:08:26 -0700 Subject: [PATCH 09/12] Add undefined to return type --- src/harness/harnessLanguageService.ts | 2 +- src/services/jsDoc.ts | 3 ++- src/services/services.ts | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/harness/harnessLanguageService.ts b/src/harness/harnessLanguageService.ts index 64ef1b552f5fa..c074b260a1d6b 100644 --- a/src/harness/harnessLanguageService.ts +++ b/src/harness/harnessLanguageService.ts @@ -492,7 +492,7 @@ namespace Harness.LanguageService { getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: ts.FormatCodeOptions): ts.TextChange[] { return unwrapJSONCallResult(this.shim.getFormattingEditsAfterKeystroke(fileName, position, key, JSON.stringify(options))); } - getDocCommentTemplateAtPosition(fileName: string, position: number): ts.TextInsertion { + getDocCommentTemplateAtPosition(fileName: string, position: number): ts.TextInsertion | undefined { return unwrapJSONCallResult(this.shim.getDocCommentTemplateAtPosition(fileName, position)); } isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean { diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index c88fe26161325..78ea0c6b53400 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -189,7 +189,8 @@ namespace ts.JsDoc { * @param position The (character-indexed) position in the file where the check should * be performed. */ - export function getDocCommentTemplateAtPosition(newLine: string, sourceFile: SourceFile, position: number): TextInsertion { + + export function getDocCommentTemplateAtPosition(newLine: string, sourceFile: SourceFile, position: number): TextInsertion | undefined { // Check if in a context where we don't want to perform any insertion if (isInString(sourceFile, position) || isInComment(sourceFile, position) || hasDocComment(sourceFile, position)) { return undefined; diff --git a/src/services/services.ts b/src/services/services.ts index d6e75868852ae..0df273e8fcecb 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1791,7 +1791,7 @@ namespace ts { } } - function getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion { + function getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion | undefined { return JsDoc.getDocCommentTemplateAtPosition(getNewLineOrDefaultFromHost(host), syntaxTreeCache.getCurrentSourceFile(fileName), position); } From 612616a1058d7aa9fc181126f83041549bfbe295 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Fri, 3 Nov 2017 09:53:56 -0700 Subject: [PATCH 10/12] Loosen restrictions on jsdoc completion locations --- src/services/jsDoc.ts | 54 ++++++++----------- .../fourslash/docCommentTemplateEmptyFile.ts | 2 +- ...ommentTemplateInsideFunctionDeclaration.ts | 10 ++-- .../fourslash/docCommentTemplateJSXText.ts | 12 +++++ ...ocCommentTemplateNamespacesAndModules02.ts | 4 +- .../fourslash/docCommentTemplateRegex.ts | 8 +-- 6 files changed, 50 insertions(+), 40 deletions(-) create mode 100644 tests/cases/fourslash/docCommentTemplateJSXText.ts diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index 78ea0c6b53400..109330c177c86 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -1,5 +1,6 @@ /* @internal */ namespace ts.JsDoc { + const singleLineTemplate = { newText: "/** */", caretOffset: 3 }; const jsDocTagNames = [ "augments", "author", @@ -170,15 +171,9 @@ namespace ts.JsDoc { /** * Checks if position points to a valid position to add JSDoc comments, and if so, * returns the appropriate template. Otherwise returns an empty string. - * Valid positions are - * - outside of comments, statements, and expressions, and - * - preceding a: - * - function/constructor/method declaration - * - class declarations - * - variable statements - * - namespace declarations - * - interface declarations - * - method signatures + * Invalid positions are + * - within comments, strings (including template literals and regex), and JSXText + * - within a token * * Hosts should ideally check that: * - The line is all whitespace up to 'position' before performing the insertion. @@ -204,17 +199,23 @@ namespace ts.JsDoc { const commentOwnerInfo = getCommentOwnerInfo(tokenAtPos); if (!commentOwnerInfo) { - return undefined; + // if climbing the tree did not find a declaration with parameters, complete to a single line comment + return singleLineTemplate; } const { commentOwner, parameters } = commentOwnerInfo; - if (commentOwner.getStart() < position) { + + if (commentOwner.kind === SyntaxKind.JsxText) { return undefined; } - 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 }; + if (commentOwner.getStart() < position) { + // if climbing the tree found a declaration with parameters but the request was made inside it, complete to a single line comment + return singleLineTemplate; + } + + if (parameters.length === 0) { + // if there are no parameters, complete to a single line comment + return singleLineTemplate; } const posLineAndChar = sourceFile.getLineAndCharacterOfPosition(position); @@ -258,7 +259,7 @@ namespace ts.JsDoc { interface CommentOwnerInfo { readonly commentOwner: Node; - readonly parameters?: ReadonlyArray; + readonly parameters: ReadonlyArray; } function getCommentOwnerInfo(tokenAtPos: Node): CommentOwnerInfo | undefined { for (let commentOwner = tokenAtPos; commentOwner; commentOwner = commentOwner.parent) { @@ -270,32 +271,18 @@ namespace ts.JsDoc { const { parameters } = commentOwner as FunctionDeclaration | MethodDeclaration | ConstructorDeclaration | MethodSignature; return { commentOwner, parameters }; - case SyntaxKind.ClassDeclaration: - case SyntaxKind.InterfaceDeclaration: - case SyntaxKind.PropertySignature: - case SyntaxKind.EnumDeclaration: - case SyntaxKind.EnumMember: - case SyntaxKind.TypeAliasDeclaration: - return { commentOwner }; - case SyntaxKind.VariableStatement: { const varStatement = commentOwner; const varDeclarations = varStatement.declarationList.declarations; const parameters = varDeclarations.length === 1 && varDeclarations[0].initializer ? getParametersFromRightHandSideOfAssignment(varDeclarations[0].initializer) : undefined; - return { commentOwner, parameters }; + return parameters ? { commentOwner, parameters } : undefined; } case SyntaxKind.SourceFile: return undefined; - case SyntaxKind.ModuleDeclaration: - // If in walking up the tree, we hit a a nested namespace declaration, - // then we must be somewhere within a dotted namespace name; however we don't - // want to give back a JSDoc template for the 'b' or 'c' in 'namespace a.b.c { }'. - return commentOwner.parent.kind === SyntaxKind.ModuleDeclaration ? undefined : { commentOwner }; - case SyntaxKind.BinaryExpression: { const be = commentOwner as BinaryExpression; if (getSpecialPropertyAssignmentKind(be) === ts.SpecialPropertyAssignmentKind.None) { @@ -304,6 +291,11 @@ namespace ts.JsDoc { const parameters = isFunctionLike(be.right) ? be.right.parameters : emptyArray; return { commentOwner, parameters }; } + + case SyntaxKind.JsxText: { + const parameters: ReadonlyArray = emptyArray; + return { commentOwner, parameters }; + } } } } diff --git a/tests/cases/fourslash/docCommentTemplateEmptyFile.ts b/tests/cases/fourslash/docCommentTemplateEmptyFile.ts index f04653dc32838..064306e3fbd92 100644 --- a/tests/cases/fourslash/docCommentTemplateEmptyFile.ts +++ b/tests/cases/fourslash/docCommentTemplateEmptyFile.ts @@ -3,4 +3,4 @@ // @Filename: emptyFile.ts /////*0*/ -verify.noDocCommentTemplateAt("0"); +verify.docCommentTemplateAt("0", 3, "/** */"); diff --git a/tests/cases/fourslash/docCommentTemplateInsideFunctionDeclaration.ts b/tests/cases/fourslash/docCommentTemplateInsideFunctionDeclaration.ts index e0ebc00dc392a..67a11a27133e9 100644 --- a/tests/cases/fourslash/docCommentTemplateInsideFunctionDeclaration.ts +++ b/tests/cases/fourslash/docCommentTemplateInsideFunctionDeclaration.ts @@ -3,6 +3,10 @@ // @Filename: functionDecl.ts ////f/*0*/unction /*1*/foo/*2*/(/*3*/) /*4*/{ /*5*/} -for (const marker of test.markers()) { - verify.noDocCommentTemplateAt(marker); -} +verify.noDocCommentTemplateAt("0"); + +verify.docCommentTemplateAt("1", 3, "/** */"); +verify.docCommentTemplateAt("2", 3, "/** */"); +verify.docCommentTemplateAt("3", 3, "/** */"); +verify.docCommentTemplateAt("4", 3, "/** */"); +verify.docCommentTemplateAt("5", 3, "/** */"); diff --git a/tests/cases/fourslash/docCommentTemplateJSXText.ts b/tests/cases/fourslash/docCommentTemplateJSXText.ts new file mode 100644 index 0000000000000..845f969f4e3bf --- /dev/null +++ b/tests/cases/fourslash/docCommentTemplateJSXText.ts @@ -0,0 +1,12 @@ +/// + +//@Filename: file.tsx +//// +//// var x =
+//// /*0*/hello/*1*/ +//// /*2*/goodbye/*3*/ +////
; + +for (const marker in test.markers()) { + verify.noDocCommentTemplateAt(marker); +} \ No newline at end of file diff --git a/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts b/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts index 787e9f04481d1..3beb9368661aa 100644 --- a/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts +++ b/tests/cases/fourslash/docCommentTemplateNamespacesAndModules02.ts @@ -9,6 +9,6 @@ verify.docCommentTemplateAt("top", /*indentation*/ 3, "/** */"); -verify.noDocCommentTemplateAt("n2"); +verify.docCommentTemplateAt("n2", 3, "/** */"); -verify.noDocCommentTemplateAt("n3"); +verify.docCommentTemplateAt("n3", 3, "/** */"); diff --git a/tests/cases/fourslash/docCommentTemplateRegex.ts b/tests/cases/fourslash/docCommentTemplateRegex.ts index 685c1ca5aef79..c1368190ca248 100644 --- a/tests/cases/fourslash/docCommentTemplateRegex.ts +++ b/tests/cases/fourslash/docCommentTemplateRegex.ts @@ -3,6 +3,8 @@ // @Filename: regex.ts ////var regex = /*0*///*1*/asdf/*2*/ /*3*///*4*/; -for (const marker of test.markers()) { - verify.noDocCommentTemplateAt(marker); -} +verify.docCommentTemplateAt("0", 3, "/** */"); +verify.noDocCommentTemplateAt("1"); +verify.noDocCommentTemplateAt("2"); +verify.noDocCommentTemplateAt("3"); +verify.docCommentTemplateAt("4", 3, "/** */"); \ No newline at end of file From 21093503a8f0fab321c85e9f830219c2bfc4d451 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Fri, 3 Nov 2017 11:19:53 -0700 Subject: [PATCH 11/12] Respond to CR --- src/services/jsDoc.ts | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index 109330c177c86..bb2cfb03e4738 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -208,13 +208,9 @@ namespace ts.JsDoc { return undefined; } - if (commentOwner.getStart() < position) { - // if climbing the tree found a declaration with parameters but the request was made inside it, complete to a single line comment - return singleLineTemplate; - } - - if (parameters.length === 0) { - // if there are no parameters, complete to a single line comment + if (commentOwner.getStart() < position || parameters.length === 0) { + // if climbing the tree found a declaration with parameters but the request was made inside it + // or if there are no parameters, complete to a single line comment return singleLineTemplate; } @@ -225,19 +221,11 @@ namespace ts.JsDoc { const indentationStr = sourceFile.text.substr(lineStart, posLineAndChar.character).replace(/\S/i, () => " "); const isJavaScriptFile = hasJavaScriptFileExtension(sourceFile.fileName); - let docParams = ""; - 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}`; - } - } + const docParams = parameters.map(({name}, i) => { + const nameText = isIdentifier(name) ? name.text : `param${i}`; + const type = isJavaScriptFile ? "{any} " : ""; + return `${indentationStr} * @param ${type}${nameText}${newLine}`; + }).join(""); // A doc comment consists of the following // * The opening comment line From d2114e1b9eb8cfc6fabeb6b775b1aa3e29625027 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Fri, 3 Nov 2017 16:14:47 -0700 Subject: [PATCH 12/12] Rename offsets in tests --- .../docCommentTemplateClassDeclMethods01.ts | 18 ++++++++---------- .../docCommentTemplateClassDeclMethods02.ts | 10 ++++------ ...docCommentTemplateObjectLiteralMethods01.ts | 10 ++++------ 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/tests/cases/fourslash/docCommentTemplateClassDeclMethods01.ts b/tests/cases/fourslash/docCommentTemplateClassDeclMethods01.ts index 34e5587567683..2e72924349730 100644 --- a/tests/cases/fourslash/docCommentTemplateClassDeclMethods01.ts +++ b/tests/cases/fourslash/docCommentTemplateClassDeclMethods01.ts @@ -1,9 +1,7 @@ /// -const enum Indentation { - Standard = 3, - Indented = 12, -} +const singleLineOffset = 3; +const multiLineOffset = 12; ////class C { @@ -16,22 +14,22 @@ const enum Indentation { //// } ////} -verify.docCommentTemplateAt("0", Indentation.Standard, +verify.docCommentTemplateAt("0", singleLineOffset, "/** */"); -verify.docCommentTemplateAt("1", Indentation.Standard, +verify.docCommentTemplateAt("1", singleLineOffset, "/** */"); -verify.docCommentTemplateAt("2", Indentation.Indented, +verify.docCommentTemplateAt("2", multiLineOffset, `/** * * @param a */ `); -verify.docCommentTemplateAt("3", Indentation.Indented, +verify.docCommentTemplateAt("3", multiLineOffset, `/** * * @param a @@ -39,7 +37,7 @@ verify.docCommentTemplateAt("3", Indentation.Indented, */ `); -verify.docCommentTemplateAt("4", Indentation.Indented, +verify.docCommentTemplateAt("4", multiLineOffset, `/** * * @param a @@ -47,7 +45,7 @@ verify.docCommentTemplateAt("4", Indentation.Indented, * @param param2 */`); -verify.docCommentTemplateAt("5", Indentation.Indented, +verify.docCommentTemplateAt("5", multiLineOffset, `/** * * @param a diff --git a/tests/cases/fourslash/docCommentTemplateClassDeclMethods02.ts b/tests/cases/fourslash/docCommentTemplateClassDeclMethods02.ts index a16fbd860647a..7a35e60ae9ec7 100644 --- a/tests/cases/fourslash/docCommentTemplateClassDeclMethods02.ts +++ b/tests/cases/fourslash/docCommentTemplateClassDeclMethods02.ts @@ -1,9 +1,7 @@ /// -const enum Indentation { - Standard = 3, - Indented = 12, -} +const singleLineOffset = 3; +const multiLineOffset = 12; ////class C { //// /*0*/ @@ -14,10 +12,10 @@ const enum Indentation { //// [1 + 2 + 3 + Math.rand()](x: number, y: string, z = true) { } ////} -verify.docCommentTemplateAt("0", Indentation.Standard, +verify.docCommentTemplateAt("0", singleLineOffset, "/** */"); -verify.docCommentTemplateAt("1", Indentation.Indented, +verify.docCommentTemplateAt("1", multiLineOffset, `/** * * @param x diff --git a/tests/cases/fourslash/docCommentTemplateObjectLiteralMethods01.ts b/tests/cases/fourslash/docCommentTemplateObjectLiteralMethods01.ts index 7fb6156be17e0..5121957a71889 100644 --- a/tests/cases/fourslash/docCommentTemplateObjectLiteralMethods01.ts +++ b/tests/cases/fourslash/docCommentTemplateObjectLiteralMethods01.ts @@ -1,9 +1,7 @@ /// -const enum Indentation { - Standard = 3, - Indented = 12, -} +const singleLineOffset = 3; +const multiLineOffset = 12; ////var x = { //// /*0*/ @@ -14,10 +12,10 @@ const enum Indentation { //// [1 + 2 + 3 + Math.rand()](x: number, y: string, z = true) { } ////} -verify.docCommentTemplateAt("0", Indentation.Standard, +verify.docCommentTemplateAt("0", singleLineOffset, "/** */"); -verify.docCommentTemplateAt("1", Indentation.Indented, +verify.docCommentTemplateAt("1", multiLineOffset, `/** * * @param x