-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
jsDoc
typecast completions, add tests (#180)
- Loading branch information
Showing
4 changed files
with
62 additions
and
28 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { findChildContainingExactPosition } from '../utils' | ||
import { sharedCompletionContext } from './sharedContext' | ||
|
||
const getCastedTypeCompletionEntry = (typeChecker: ts.TypeChecker, node: ts.Expression) => { | ||
const typeAtLocation = typeChecker.getTypeAtLocation(node) | ||
const type = typeChecker.typeToString(typeAtLocation) | ||
const widenType = typeChecker.typeToString(typeChecker.getBaseTypeOfLiteralType(typeAtLocation)) | ||
|
||
return { | ||
kind: ts.ScriptElementKind.unknown, | ||
name: type === widenType ? type : widenType, | ||
sortText: '!', | ||
} | ||
} | ||
|
||
export default () => { | ||
const typeChecker = sharedCompletionContext.program.getTypeChecker() | ||
const { position, fullText, prior } = sharedCompletionContext | ||
|
||
// as completions | ||
if (fullText.slice(0, position - 1).endsWith('as')) { | ||
const node = findChildContainingExactPosition(sharedCompletionContext.sourceFile, position - 2) | ||
if (!node || !ts.isAsExpression(node)) return | ||
const entry = getCastedTypeCompletionEntry(typeChecker, node.expression) | ||
prior.entries.push(entry) | ||
return | ||
} | ||
|
||
// jsdoc typecast completions | ||
const node = findChildContainingExactPosition(sharedCompletionContext.sourceFile, position) | ||
if (!node) return | ||
let typeCastedNode: ts.ParenthesizedExpression | undefined | ||
node.forEachChild(node => { | ||
if (ts.isParenthesizedExpression(node) && ts.getJSDocTypeTag(node)) typeCastedNode = node | ||
}) | ||
|
||
if (!typeCastedNode) return | ||
const entry = getCastedTypeCompletionEntry(typeChecker, typeCastedNode.expression) | ||
prior.entries.push(entry) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters