Skip to content

Commit e35cd55

Browse files
committed
feat(42637): add generateReturn option to UserPreferences
1 parent 05e2f74 commit e35cd55

15 files changed

+70
-31
lines changed

Diff for: src/harness/client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ namespace ts.server {
605605
return notImplemented();
606606
}
607607

608-
getDocCommentTemplateAtPosition(_fileName: string, _position: number): TextInsertion {
608+
getDocCommentTemplateAtPosition(_fileName: string, _position: number, _options?: DocCommentTemplateOptions): TextInsertion {
609609
return notImplemented();
610610
}
611611

Diff for: src/harness/fourslashImpl.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3048,9 +3048,9 @@ namespace FourSlash {
30483048
assert.deepEqual(actualModuleSpecifiers, moduleSpecifiers);
30493049
}
30503050

3051-
public verifyDocCommentTemplate(expected: ts.TextInsertion | undefined) {
3051+
public verifyDocCommentTemplate(expected: ts.TextInsertion | undefined, options?: ts.DocCommentTemplateOptions) {
30523052
const name = "verifyDocCommentTemplate";
3053-
const actual = this.languageService.getDocCommentTemplateAtPosition(this.activeFile.fileName, this.currentCaretPosition)!;
3053+
const actual = this.languageService.getDocCommentTemplateAtPosition(this.activeFile.fileName, this.currentCaretPosition, options || { generateReturnInDocTemplate: true })!;
30543054

30553055
if (expected === undefined) {
30563056
if (actual) {

Diff for: src/harness/fourslashInterfaceImpl.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,9 @@ namespace FourSlashInterface {
432432
this.state.verifyNoMatchingBracePosition(bracePosition);
433433
}
434434

435-
public docCommentTemplateAt(marker: string | FourSlash.Marker, expectedOffset: number, expectedText: string) {
435+
public docCommentTemplateAt(marker: string | FourSlash.Marker, expectedOffset: number, expectedText: string, options?: ts.DocCommentTemplateOptions) {
436436
this.state.goToMarker(marker);
437-
this.state.verifyDocCommentTemplate({ newText: expectedText.replace(/\r?\n/g, "\r\n"), caretOffset: expectedOffset });
437+
this.state.verifyDocCommentTemplate({ newText: expectedText.replace(/\r?\n/g, "\r\n"), caretOffset: expectedOffset }, options);
438438
}
439439

440440
public noDocCommentTemplateAt(marker: string | FourSlash.Marker) {

Diff for: src/harness/harnessLanguageService.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,8 @@ namespace Harness.LanguageService {
558558
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: ts.FormatCodeOptions): ts.TextChange[] {
559559
return unwrapJSONCallResult(this.shim.getFormattingEditsAfterKeystroke(fileName, position, key, JSON.stringify(options)));
560560
}
561-
getDocCommentTemplateAtPosition(fileName: string, position: number): ts.TextInsertion {
562-
return unwrapJSONCallResult(this.shim.getDocCommentTemplateAtPosition(fileName, position));
561+
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: ts.DocCommentTemplateOptions): ts.TextInsertion {
562+
return unwrapJSONCallResult(this.shim.getDocCommentTemplateAtPosition(fileName, position, options));
563563
}
564564
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean {
565565
return unwrapJSONCallResult(this.shim.isValidBraceCompletionAtPosition(fileName, position, openingBrace));

Diff for: src/server/protocol.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3273,6 +3273,8 @@ namespace ts.server.protocol {
32733273
readonly provideRefactorNotApplicableReason?: boolean;
32743274
readonly allowRenameOfImportPath?: boolean;
32753275
readonly includePackageJsonAutoImports?: "auto" | "on" | "off";
3276+
3277+
readonly generateReturnInDocTemplate?: boolean;
32763278
}
32773279

32783280
export interface CompilerOptions {

Diff for: src/server/session.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ namespace ts.server {
16411641
private getDocCommentTemplate(args: protocol.FileLocationRequestArgs) {
16421642
const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args);
16431643
const position = this.getPositionInFile(args, file);
1644-
return languageService.getDocCommentTemplateAtPosition(file, position);
1644+
return languageService.getDocCommentTemplateAtPosition(file, position, this.getPreferences(file));
16451645
}
16461646

16471647
private getSpanOfEnclosingComment(args: protocol.SpanOfEnclosingCommentRequestArgs) {

Diff for: src/services/jsDoc.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ namespace ts.JsDoc {
251251
* @param position The (character-indexed) position in the file where the check should
252252
* be performed.
253253
*/
254-
export function getDocCommentTemplateAtPosition(newLine: string, sourceFile: SourceFile, position: number): TextInsertion | undefined {
254+
export function getDocCommentTemplateAtPosition(newLine: string, sourceFile: SourceFile, position: number, options?: DocCommentTemplateOptions): TextInsertion | undefined {
255255
const tokenAtPos = getTokenAtPosition(sourceFile, position);
256256
const existingDocComment = findAncestor(tokenAtPos, isJSDoc);
257257
if (existingDocComment && (existingDocComment.comment !== undefined || length(existingDocComment.tags))) {
@@ -265,7 +265,7 @@ namespace ts.JsDoc {
265265
return undefined;
266266
}
267267

268-
const commentOwnerInfo = getCommentOwnerInfo(tokenAtPos);
268+
const commentOwnerInfo = getCommentOwnerInfo(tokenAtPos, options);
269269
if (!commentOwnerInfo) {
270270
return undefined;
271271
}
@@ -325,10 +325,10 @@ namespace ts.JsDoc {
325325
readonly parameters?: readonly ParameterDeclaration[];
326326
readonly hasReturn?: boolean;
327327
}
328-
function getCommentOwnerInfo(tokenAtPos: Node): CommentOwnerInfo | undefined {
329-
return forEachAncestor(tokenAtPos, getCommentOwnerInfoWorker);
328+
function getCommentOwnerInfo(tokenAtPos: Node, options: DocCommentTemplateOptions | undefined): CommentOwnerInfo | undefined {
329+
return forEachAncestor(tokenAtPos, n => getCommentOwnerInfoWorker(n, options));
330330
}
331-
function getCommentOwnerInfoWorker(commentOwner: Node): CommentOwnerInfo | undefined | "quit" {
331+
function getCommentOwnerInfoWorker(commentOwner: Node, options: DocCommentTemplateOptions | undefined): CommentOwnerInfo | undefined | "quit" {
332332
switch (commentOwner.kind) {
333333
case SyntaxKind.FunctionDeclaration:
334334
case SyntaxKind.FunctionExpression:
@@ -337,10 +337,10 @@ namespace ts.JsDoc {
337337
case SyntaxKind.MethodSignature:
338338
case SyntaxKind.ArrowFunction:
339339
const host = commentOwner as ArrowFunction | FunctionDeclaration | MethodDeclaration | ConstructorDeclaration | MethodSignature;
340-
return { commentOwner, parameters: host.parameters, hasReturn: hasReturn(host) };
340+
return { commentOwner, parameters: host.parameters, hasReturn: hasReturn(host, options) };
341341

342342
case SyntaxKind.PropertyAssignment:
343-
return getCommentOwnerInfoWorker((commentOwner as PropertyAssignment).initializer);
343+
return getCommentOwnerInfoWorker((commentOwner as PropertyAssignment).initializer, options);
344344

345345
case SyntaxKind.ClassDeclaration:
346346
case SyntaxKind.InterfaceDeclaration:
@@ -357,7 +357,7 @@ namespace ts.JsDoc {
357357
? getRightHandSideOfAssignment(varDeclarations[0].initializer)
358358
: undefined;
359359
return host
360-
? { commentOwner, parameters: host.parameters, hasReturn: hasReturn(host) }
360+
? { commentOwner, parameters: host.parameters, hasReturn: hasReturn(host, options) }
361361
: { commentOwner };
362362
}
363363

@@ -371,27 +371,28 @@ namespace ts.JsDoc {
371371
return commentOwner.parent.kind === SyntaxKind.ModuleDeclaration ? undefined : { commentOwner };
372372

373373
case SyntaxKind.ExpressionStatement:
374-
return getCommentOwnerInfoWorker((commentOwner as ExpressionStatement).expression);
374+
return getCommentOwnerInfoWorker((commentOwner as ExpressionStatement).expression, options);
375375
case SyntaxKind.BinaryExpression: {
376376
const be = commentOwner as BinaryExpression;
377377
if (getAssignmentDeclarationKind(be) === AssignmentDeclarationKind.None) {
378378
return "quit";
379379
}
380380
return isFunctionLike(be.right)
381-
? { commentOwner, parameters: be.right.parameters, hasReturn: hasReturn(be.right) }
381+
? { commentOwner, parameters: be.right.parameters, hasReturn: hasReturn(be.right, options) }
382382
: { commentOwner };
383383
}
384384
case SyntaxKind.PropertyDeclaration:
385385
const init = (commentOwner as PropertyDeclaration).initializer;
386386
if (init && (isFunctionExpression(init) || isArrowFunction(init))) {
387-
return { commentOwner, parameters: init.parameters, hasReturn: hasReturn(init) };
387+
return { commentOwner, parameters: init.parameters, hasReturn: hasReturn(init, options) };
388388
}
389389
}
390390
}
391391

392-
function hasReturn(node: Node) {
393-
return isArrowFunction(node) && isExpression(node.body)
394-
|| isFunctionLikeDeclaration(node) && node.body && isBlock(node.body) && !!forEachReturnStatement(node.body, n => n);
392+
function hasReturn(node: Node, options: DocCommentTemplateOptions | undefined) {
393+
return !!options?.generateReturnInDocTemplate &&
394+
(isArrowFunction(node) && isExpression(node.body)
395+
|| isFunctionLikeDeclaration(node) && node.body && isBlock(node.body) && !!forEachReturnStatement(node.body, n => n));
395396
}
396397

397398
function getRightHandSideOfAssignment(rightHandSide: Expression): FunctionExpression | ArrowFunction | ConstructorDeclaration | undefined {

Diff for: src/services/services.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2004,8 +2004,8 @@ namespace ts {
20042004
: Promise.reject("Host does not implement `installPackage`");
20052005
}
20062006

2007-
function getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion | undefined {
2008-
return JsDoc.getDocCommentTemplateAtPosition(getNewLineOrDefaultFromHost(host), syntaxTreeCache.getCurrentSourceFile(fileName), position);
2007+
function getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): TextInsertion | undefined {
2008+
return JsDoc.getDocCommentTemplateAtPosition(getNewLineOrDefaultFromHost(host), syntaxTreeCache.getCurrentSourceFile(fileName), position, options);
20092009
}
20102010

20112011
function isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean {

Diff for: src/services/shims.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ namespace ts {
263263
/**
264264
* Returns JSON-encoded value of the type TextInsertion.
265265
*/
266-
getDocCommentTemplateAtPosition(fileName: string, position: number): string;
266+
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): string;
267267

268268
/**
269269
* Returns JSON-encoded boolean to indicate whether we should support brace location
@@ -999,10 +999,10 @@ namespace ts {
999999
});
10001000
}
10011001

1002-
public getDocCommentTemplateAtPosition(fileName: string, position: number): string {
1002+
public getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): string {
10031003
return this.forwardJSONCall(
10041004
`getDocCommentTemplateAtPosition('${fileName}', ${position})`,
1005-
() => this.languageService.getDocCommentTemplateAtPosition(fileName, position)
1005+
() => this.languageService.getDocCommentTemplateAtPosition(fileName, position, options)
10061006
);
10071007
}
10081008

Diff for: src/services/types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ namespace ts {
490490
getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
491491
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
492492

493-
getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion | undefined;
493+
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): TextInsertion | undefined;
494494

495495
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
496496
/**
@@ -1073,6 +1073,10 @@ namespace ts {
10731073
readonly allowRenameOfImportPath?: boolean;
10741074
}
10751075

1076+
export interface DocCommentTemplateOptions {
1077+
readonly generateReturnInDocTemplate?: boolean;
1078+
}
1079+
10761080
export interface SignatureHelpParameter {
10771081
name: string;
10781082
documentation: SymbolDisplayPart[];

Diff for: tests/baselines/reference/api/tsserverlibrary.d.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5555,7 +5555,7 @@ declare namespace ts {
55555555
getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
55565556
getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
55575557
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
5558-
getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion | undefined;
5558+
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): TextInsertion | undefined;
55595559
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
55605560
/**
55615561
* This will return a defined result if the position is after the `>` of the opening tag, or somewhere in the text, of a JSXElement with no closing tag.
@@ -6020,6 +6020,9 @@ declare namespace ts {
60206020
interface RenameInfoOptions {
60216021
readonly allowRenameOfImportPath?: boolean;
60226022
}
6023+
interface DocCommentTemplateOptions {
6024+
readonly generateReturnInDocTemplate?: boolean;
6025+
}
60236026
interface SignatureHelpParameter {
60246027
name: string;
60256028
documentation: SymbolDisplayPart[];
@@ -9045,6 +9048,7 @@ declare namespace ts.server.protocol {
90459048
readonly provideRefactorNotApplicableReason?: boolean;
90469049
readonly allowRenameOfImportPath?: boolean;
90479050
readonly includePackageJsonAutoImports?: "auto" | "on" | "off";
9051+
readonly generateReturnInDocTemplate?: boolean;
90489052
}
90499053
interface CompilerOptions {
90509054
allowJs?: boolean;

Diff for: tests/baselines/reference/api/typescript.d.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -5555,7 +5555,7 @@ declare namespace ts {
55555555
getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
55565556
getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
55575557
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
5558-
getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion | undefined;
5558+
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): TextInsertion | undefined;
55595559
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
55605560
/**
55615561
* This will return a defined result if the position is after the `>` of the opening tag, or somewhere in the text, of a JSXElement with no closing tag.
@@ -6020,6 +6020,9 @@ declare namespace ts {
60206020
interface RenameInfoOptions {
60216021
readonly allowRenameOfImportPath?: boolean;
60226022
}
6023+
interface DocCommentTemplateOptions {
6024+
readonly generateReturnInDocTemplate?: boolean;
6025+
}
60236026
interface SignatureHelpParameter {
60246027
name: string;
60256028
documentation: SymbolDisplayPart[];
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
/////*0*/
4+
////function f1(x: number, y: number) {
5+
//// return 1;
6+
////}
7+
8+
verify.docCommentTemplateAt("0", 8,
9+
`/**
10+
*
11+
* @param x
12+
* @param y
13+
* @returns
14+
*/`, { generateReturnInDocTemplate: true });
15+
16+
verify.docCommentTemplateAt("0", 8,
17+
`/**
18+
*
19+
* @param x
20+
* @param y
21+
*/`, { generateReturnInDocTemplate: false });

Diff for: tests/cases/fourslash/fourslash.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ declare namespace FourSlashInterface {
328328
todoCommentsInCurrentFile(descriptors: string[]): void;
329329
matchingBracePositionInCurrentFile(bracePosition: number, expectedMatchPosition: number): void;
330330
noMatchingBracePositionInCurrentFile(bracePosition: number): void;
331-
docCommentTemplateAt(markerName: string | FourSlashInterface.Marker, expectedOffset: number, expectedText: string): void;
331+
docCommentTemplateAt(markerName: string | FourSlashInterface.Marker, expectedOffset: number, expectedText: string, options?: VerifyDocCommentTemplateOptions): void;
332332
noDocCommentTemplateAt(markerName: string | FourSlashInterface.Marker): void;
333333
rangeAfterCodeFix(expectedText: string, includeWhiteSpace?: boolean, errorCode?: number, index?: number): void;
334334
codeFixAll(options: { fixId: string, fixAllDescription: string, newFileContent: NewFileContent, commands?: {}[] }): void;
@@ -664,6 +664,10 @@ declare namespace FourSlashInterface {
664664
overrideSelectedItemIndex?: number;
665665
}
666666

667+
interface VerifyDocCommentTemplateOptions {
668+
generateReturnInDocTemplate?: boolean;
669+
}
670+
667671
export type SignatureHelpTriggerReason =
668672
| SignatureHelpInvokedReason
669673
| SignatureHelpCharacterTypedReason

0 commit comments

Comments
 (0)