Skip to content

Commit

Permalink
feat: add prefix to type information
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 4, 2022
1 parent cd3c164 commit 8d9e0a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
35 changes: 22 additions & 13 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 23 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ function init(modules: { typescript: typeof import("typescript/lib/tsserverlibra

const program = info.project['program'] as ts.Program|undefined

if(!program) {
return prior
}
if(!program) return prior

const typeChecker = program.getTypeChecker()
const sourceFile = program.getSourceFile(fileName)

if(!sourceFile) return prior

// @ts-expect-error
const node: ts.Node = ts.getTouchingPropertyName(sourceFile, position);
if (!node || node === sourceFile) {
Expand All @@ -33,23 +33,32 @@ function init(modules: { typescript: typeof import("typescript/lib/tsserverlibra
}

const symbol = typeChecker.getSymbolAtLocation(node)
if(!symbol) {
return prior
}
if(!symbol) return prior

const type = getTypeOrDeclaredType(typeChecker, symbol, node)
const expandedType = recursiveMergeIntersection(typeChecker, type)

prior?.displayParts?.push({
kind: 'lineBreak',
text: "\n"
})
if(!prior?.displayParts) return prior

prior.displayParts.push({ kind: 'lineBreak', text: "\n\n" })

prior.displayParts.push({ kind: 'punctuation', text: '(' })
prior.displayParts.push({ kind: 'text', text: 'type' })
prior.displayParts.push({ kind: 'punctuation', text: ')' })
prior.displayParts.push({ kind: 'space', text: ' ' })

const typeString = resolvedTypeToString(typeChecker, sourceFile, expandedType, undefined, ts.TypeFormatFlags.MultilineObjectLiterals)

const typeString = resolvedTypeToString(typeChecker, expandedType)
typeString.split("\n").forEach(line => {
prior.displayParts!.push({
kind: 'punctuation',
text: line,
})

prior?.displayParts?.push({
kind: 'punctuation',
text: typeString
prior.displayParts!.push({
kind: 'lineBreak',
text: "\n"
})
})

return prior
Expand Down

0 comments on commit 8d9e0a6

Please sign in to comment.