|
| 1 | +import { findChildContainingExactPosition } from '../utils' |
| 2 | + |
| 3 | +export default (languageService: ts.LanguageService, sourceFile: ts.SourceFile, position: number) => { |
| 4 | + let node = findChildContainingExactPosition(sourceFile, position) |
| 5 | + if (!node) return |
| 6 | + node = getNodeForQuickInfo(node) |
| 7 | + const checker = languageService.getProgram()!.getTypeChecker()! |
| 8 | + const symbol = getSymbolAtLocationForQuickInfo(node, checker) |
| 9 | + if (!symbol) return |
| 10 | + const type = checker.getTypeOfSymbol(symbol) |
| 11 | + return checker.typeToString(type, undefined, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.NoTypeReduction) |
| 12 | +} |
| 13 | + |
| 14 | +function getNodeForQuickInfo(node: ts.Node): ts.Node { |
| 15 | + if (ts.isNewExpression(node.parent) && node.pos === node.parent.pos) { |
| 16 | + return node.parent.expression |
| 17 | + } |
| 18 | + if (ts.isNamedTupleMember(node.parent) && node.pos === node.parent.pos) { |
| 19 | + return node.parent |
| 20 | + } |
| 21 | + //@ts-expect-error |
| 22 | + if (tsFull.isImportMeta(node.parent) && node.parent.name === node) { |
| 23 | + return node.parent |
| 24 | + } |
| 25 | + if (ts.isJsxNamespacedName(node.parent)) { |
| 26 | + return node.parent |
| 27 | + } |
| 28 | + return node |
| 29 | +} |
| 30 | + |
| 31 | +function getSymbolAtLocationForQuickInfo(node: ts.Node, checker: ts.TypeChecker): ts.Symbol | undefined { |
| 32 | + const object = tsFull.getContainingObjectLiteralElement(node as any) as any |
| 33 | + if (object) { |
| 34 | + const contextualType = checker.getContextualType(object.parent) |
| 35 | + const properties = contextualType && tsFull.getPropertySymbolsFromContextualType(object, checker as any, contextualType as any, /*unionSymbolOk*/ false) |
| 36 | + if (properties && properties.length === 1) { |
| 37 | + return properties[0]! as any |
| 38 | + } |
| 39 | + } |
| 40 | + return checker.getSymbolAtLocation(node) |
| 41 | +} |
0 commit comments