@rehearsal/ts-utils
Advanced tools
Comparing version 1.0.19-beta to 1.0.20-beta
/** | ||
* This file contains helper functions to work with Typescript AST nodes and diagnostics | ||
*/ | ||
import type { DiagnosticWithLocation, Node, SourceFile } from 'typescript'; | ||
import type { DiagnosticWithLocation, Node, SourceFile, TypeChecker, TypeNode } from 'typescript'; | ||
/** | ||
@@ -23,2 +23,3 @@ * Checks if node starts with `start` position and its length equals to `length`. | ||
export declare function findNodeAtPosition(sourceFile: SourceFile, start: number, length: number): Node | undefined; | ||
export declare function findNodeEndsAtPosition(sourceFile: SourceFile, pos: number): Node | undefined; | ||
/** | ||
@@ -40,2 +41,3 @@ * Finds the first node in the line. | ||
export declare function isVariableOfCatchClause(node: Node): boolean; | ||
export declare function canTypeBeResolved(checker: TypeChecker, typeNode: TypeNode): boolean; | ||
//# sourceMappingURL=typescript-ast.d.ts.map |
@@ -5,3 +5,3 @@ /** | ||
import ts from 'typescript'; | ||
const { findAncestor, getLineAndCharacterOfPosition, isCatchClause, isIdentifier, isJsxElement, isJsxFragment, isSourceFile, } = ts; | ||
const { findAncestor, getLineAndCharacterOfPosition, isCatchClause, isIdentifier, isJsxElement, isJsxFragment, isSourceFile, isTypeReferenceNode, } = ts; | ||
/** | ||
@@ -48,2 +48,15 @@ * Checks if node starts with `start` position and its length equals to `length`. | ||
} | ||
export function findNodeEndsAtPosition(sourceFile, pos) { | ||
let previousNode = sourceFile; | ||
const visitor = (node) => { | ||
// Looking for a not that comes right after the target node... | ||
if (node.getStart() >= pos) { | ||
// ... and check the previous node children | ||
return ts.forEachChild(previousNode, visitor); | ||
} | ||
previousNode = node; | ||
}; | ||
ts.forEachChild(sourceFile, visitor); | ||
return previousNode; | ||
} | ||
/** | ||
@@ -81,2 +94,36 @@ * Finds the first node in the line. | ||
} | ||
export function canTypeBeResolved(checker, typeNode) { | ||
if (isTypeReferenceNode(typeNode)) { | ||
const type = checker.getTypeFromTypeNode(typeNode); | ||
const typeArguments = typeNode.typeArguments || []; | ||
const isTypeError = (type) => { | ||
// Check if Type can't be resolved | ||
//return (type as unknown as { intrinsicName?: string }).intrinsicName === 'error'; | ||
return type.flags === ts.TypeFlags.Any; | ||
}; | ||
return !isTypeError(type) && !typeArguments.find((node) => !canTypeBeResolved(checker, node)); | ||
} | ||
if (ts.isParenthesizedTypeNode(typeNode)) { | ||
return canTypeBeResolved(checker, typeNode.type); | ||
} | ||
if (ts.isTypeLiteralNode(typeNode)) { | ||
const types = typeNode.members | ||
.map((member) => (ts.isPropertySignature(member) ? member.type : undefined)) | ||
.filter((member) => member !== undefined); | ||
return !types.find((type) => !canTypeBeResolved(checker, type)); | ||
} | ||
if (ts.isUnionTypeNode(typeNode)) { | ||
return !typeNode.types.find((type) => !canTypeBeResolved(checker, type)); | ||
} | ||
if (ts.isFunctionTypeNode(typeNode)) { | ||
// Types of function types params (params without types are skipped) | ||
const types = typeNode.parameters | ||
.map((parameter) => parameter.type) | ||
.filter((parameter) => parameter !== undefined); | ||
// Checking a function return type + all available parameter types | ||
return ![typeNode.type, ...types].find((type) => !canTypeBeResolved(checker, type)); | ||
} | ||
// Bypass other king of types | ||
return true; | ||
} | ||
//# sourceMappingURL=typescript-ast.js.map |
{ | ||
"name": "@rehearsal/ts-utils", | ||
"version": "1.0.19-beta", | ||
"version": "1.0.20-beta", | ||
"description": "Rehearsal TypeScript Utils", | ||
@@ -52,3 +52,3 @@ "keywords": [ | ||
}, | ||
"packageManager": "pnpm@7.12.1", | ||
"packageManager": "pnpm@8.2.0", | ||
"engines": { | ||
@@ -55,0 +55,0 @@ "node": ">=14.16.0" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
139686
386