MCPcopy Create free account
hub / github.com/microsoft/typescript-go / TryInsertTypeAnnotation

Method TryInsertTypeAnnotation

internal/ls/change/tracker.go:200–235  ·  view source on GitHub ↗

TryInsertTypeAnnotation inserts a type annotation after the appropriate position on a node (after the close paren for function-like, after the name/exclamation/question for variable-like). Returns true if successful.

(sourceFile *ast.SourceFile, node *ast.Node, typeNode *ast.Node)

Source from the content-addressed store, hash-verified

198// (after the close paren for function-like, after the name/exclamation/question for variable-like).
199// Returns true if successful.
200func (t *Tracker) TryInsertTypeAnnotation(sourceFile *ast.SourceFile, node *ast.Node, typeNode *ast.Node) bool {
201 var endNode *ast.Node
202 if ast.IsFunctionLike(node) {
203 endNode = astnav.FindChildOfKind(node, ast.KindCloseParenToken, sourceFile)
204 if endNode == nil {
205 if !ast.IsArrowFunction(node) {
206 return false
207 }
208 // If no `)`, is an arrow function `x => x`, so use the end of the first parameter
209 params := node.Parameters()
210 if len(params) == 0 {
211 return false
212 }
213 endNode = params[0]
214 }
215 } else {
216 switch node.Kind {
217 case ast.KindVariableDeclaration:
218 endNode = node.AsVariableDeclaration().ExclamationToken
219 case ast.KindPropertySignature:
220 endNode = node.AsPropertySignatureDeclaration().PostfixToken
221 case ast.KindPropertyDeclaration:
222 endNode = node.AsPropertyDeclaration().PostfixToken
223 case ast.KindParameter:
224 endNode = node.AsParameterDeclaration().QuestionToken
225 }
226 if endNode == nil {
227 endNode = node.Name()
228 }
229 }
230 if endNode == nil {
231 return false
232 }
233 t.InsertNodeAt(sourceFile, core.TextPos(endNode.End()), typeNode, NodeOptions{Prefix: ": "})
234 return true
235}
236
237// ParenthesizeArrowParameters wraps the parameters of a paren-less arrow function in `(` and `)`.
238// This is a no-op if the arrow function already has parens.

Callers 2

addTypeToVariableLikeMethod · 0.80

Calls 13

InsertNodeAtMethod · 0.95
EndMethod · 0.95
IsFunctionLikeFunction · 0.92
FindChildOfKindFunction · 0.92
IsArrowFunctionFunction · 0.92
TextPosTypeAlias · 0.92
lenFunction · 0.85
AsVariableDeclarationMethod · 0.80
AsPropertyDeclarationMethod · 0.80
NameMethod · 0.65

Tested by

no test coverage detected