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)
| 198 | // (after the close paren for function-like, after the name/exclamation/question for variable-like). |
| 199 | // Returns true if successful. |
| 200 | func (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. |
no test coverage detected