Complete to single line jsdoc comment if no params

This commit is contained in:
uniqueiniquity
2017-11-02 09:55:56 -07:00
parent 976c25c672
commit 509b9ad087
13 changed files with 99 additions and 127 deletions

View File

@@ -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 ?
(<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 ?
(<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<ParameterDeclaration>;
}
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: {

View File

@@ -11,8 +11,5 @@
//// }
////}
verify.docCommentTemplateAt("decl", /*newTextOffset*/ 8,
`/**
*
*/
`);
verify.docCommentTemplateAt("decl", /*newTextOffset*/ 3,
"/** */");

View File

@@ -1,7 +1,7 @@
/// <reference path='fourslash.ts' />
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

View File

@@ -1,6 +1,7 @@
/// <reference path='fourslash.ts' />
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,
`/**

View File

@@ -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);

View File

@@ -1,50 +0,0 @@
/// <reference path='fourslash.ts' />
/////*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,
`/**
*
*/`);

View File

@@ -0,0 +1,49 @@
/// <reference path='fourslash.ts' />
/////*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,
"/** */");

View File

@@ -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,
"/** */");

View File

@@ -6,10 +6,8 @@
//// /*n3*/ n3 {
////}
verify.docCommentTemplateAt("top", /*indentation*/ 8,
`/**
*
*/`);
verify.docCommentTemplateAt("top", /*indentation*/ 3,
"/** */");
verify.emptyDocCommentTemplateAt("n2");

View File

@@ -1,6 +1,7 @@
/// <reference path='fourslash.ts' />
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,
`/**

View File

@@ -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,

View File

@@ -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,
"/** */");
}

View File

@@ -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,
`/**