-
Notifications
You must be signed in to change notification settings - Fork 13.2k
annotateWithTypeFromJSDoc: Do less special-casing for arrow functions #22407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -352,14 +352,16 @@ namespace ts.textChanges { | |
| /** Prefer this over replacing a node with another that has a type annotation, as it avoids reformatting the other parts of the node. */ | ||
| public insertTypeAnnotation(sourceFile: SourceFile, node: TypeAnnotatable, type: TypeNode): void { | ||
| const end = (isFunctionLike(node) | ||
| ? findChildOfKind(node, SyntaxKind.CloseParenToken, sourceFile)! | ||
| // If no `)`, is an arrow function `x => x`, so use the end of the first parameter | ||
| ? findChildOfKind(node, SyntaxKind.CloseParenToken, sourceFile) || first(node.parameters) | ||
| : node.kind !== SyntaxKind.VariableDeclaration && node.questionToken ? node.questionToken : node.name).end; | ||
| this.insertNodeAt(sourceFile, end, type, { prefix: ": " }); | ||
| } | ||
|
|
||
| public insertTypeParameters(sourceFile: SourceFile, node: SignatureDeclaration, typeParameters: ReadonlyArray<TypeParameterDeclaration>): void { | ||
| const lparen = findChildOfKind(node, SyntaxKind.OpenParenToken, sourceFile)!.pos; | ||
| this.insertNodesAt(sourceFile, lparen, typeParameters, { prefix: "<", suffix: ">" }); | ||
| // If no `(`, is an arrow function `x => x`, so use the pos of the first parameter | ||
| const start = (findChildOfKind(node, SyntaxKind.OpenParenToken, sourceFile) || first(node.parameters)).pos; | ||
|
||
| this.insertNodesAt(sourceFile, start, typeParameters, { prefix: "<", suffix: ">" }); | ||
| } | ||
|
|
||
| private getOptionsForInsertNodeBefore(before: Node, doubleNewlines: boolean): ChangeNodeOptions { | ||
|
|
@@ -369,6 +371,9 @@ namespace ts.textChanges { | |
| else if (isVariableDeclaration(before)) { // insert `x = 1, ` into `const x = 1, y = 2; | ||
| return { suffix: ", " }; | ||
| } | ||
| else if (isParameter(before)) { | ||
| return {}; | ||
| } | ||
| return Debug.failBadSyntaxKind(before); // We haven't handled this kind of node yet -- add it | ||
| } | ||
|
|
||
|
|
@@ -453,6 +458,9 @@ namespace ts.textChanges { | |
| else if (isVariableDeclaration(node)) { | ||
| return { prefix: ", " }; | ||
| } | ||
| else if (isParameter(node)) { | ||
| return {}; | ||
| } | ||
| return Debug.failBadSyntaxKind(node); // We haven't handled this kind of node yet -- add it | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
lastmight be clearer, even though they should return the same result.