Skip to content

Commit 60a6240

Browse files
authored
feat(42637): add generateReturn option to UserPreferences (#42642)
1 parent 4c43e09 commit 60a6240

File tree

15 files changed

+70
-31
lines changed

15 files changed

+70
-31
lines changed

src/harness/client.ts

Lines changed: 1 addition & 1 deletion
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

src/harness/fourslashImpl.ts

Lines changed: 2 additions & 2 deletions
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) {

src/harness/fourslashInterfaceImpl.ts

Lines changed: 2 additions & 2 deletions
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) {

src/harness/harnessLanguageService.ts

Lines changed: 2 additions & 2 deletions
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));

src/server/protocol.ts

Lines changed: 2 additions & 0 deletions
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 {

src/server/session.ts

Lines changed: 1 addition & 1 deletion
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) {

src/services/jsDoc.ts

Lines changed: 15 additions & 14 deletions
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 {

src/services/services.ts

Lines changed: 2 additions & 2 deletions
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 {

src/services/shims.ts

Lines changed: 3 additions & 3 deletions
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

src/services/types.ts

Lines changed: 5 additions & 1 deletion
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[];

0 commit comments

Comments
 (0)