import ts from "typescript"; import type { ReferenceKind } from "./types"; export function classifyReference(node: ts.Node): ReferenceKind { if (isImportReference(node)) return "import"; if (isReExportReference(node)) return "re-export"; if (isJsxReference(node)) return "jsx"; if (isTypeReference(node)) return "type"; if (isConstructReference(node)) return "construct"; if (isCallReference(node)) return "call"; if (isWriteReference(node)) return "write"; if (isRuntimeRead(node)) return "read"; return "unknown"; } export function isDeclarationName(node: ts.Node): boolean { const parent = node.parent; return ( (ts.isFunctionDeclaration(parent) && parent.name === node) || (ts.isClassDeclaration(parent) && parent.name === node) || (ts.isInterfaceDeclaration(parent) && parent.name === node) || (ts.isTypeAliasDeclaration(parent) && parent.name === node) || (ts.isEnumDeclaration(parent) && parent.name === node) || (ts.isModuleDeclaration(parent) && parent.name === node) || (ts.isVariableDeclaration(parent) && parent.name === node) || (ts.isMethodDeclaration(parent) && parent.name === node) || (ts.isMethodSignature(parent) && parent.name === node) || (ts.isPropertyDeclaration(parent) && parent.name === node) || (ts.isPropertySignature(parent) && parent.name === node) || (ts.isParameter(parent) && parent.name === node) ); } function isImportReference(node: ts.Node): boolean { return ancestorMatches(node, (parent) => ts.isImportDeclaration(parent)); } function isReExportReference(node: ts.Node): boolean { return ancestorMatches( node, (parent) => ts.isExportDeclaration(parent) && Boolean(parent.moduleSpecifier), ); } function isJsxReference(node: ts.Node): boolean { return ancestorMatches( node, (parent) => ts.isJsxOpeningElement(parent) || ts.isJsxClosingElement(parent) || ts.isJsxSelfClosingElement(parent) || ts.isJsxAttribute(parent), ); } function isConstructReference(node: ts.Node): boolean { const expression = getExpressionNode(node); return ts.isNewExpression(expression.parent) && expression.parent.expression === expression; } function isCallReference(node: ts.Node): boolean { const expression = getExpressionNode(node); return ts.isCallExpression(expression.parent) && expression.parent.expression === expression; } function isWriteReference(node: ts.Node): boolean { const expression = getExpressionNode(node); const parent = expression.parent; if (ts.isBinaryExpression(parent) && parent.left === expression) { return isAssignmentOperator(parent.operatorToken.kind); } return ( (ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent)) && (parent.operator === ts.SyntaxKind.PlusPlusToken || parent.operator === ts.SyntaxKind.MinusMinusToken) ); } function isRuntimeRead(node: ts.Node): boolean { return ts.isIdentifier(node) || ts.isPrivateIdentifier(node) || ts.isStringLiteralLike(node); } function isTypeReference(node: ts.Node): boolean { return ancestorMatches(node, (parent) => { if ( ts.isTypeNode(parent) || ts.isInterfaceDeclaration(parent) || ts.isTypeAliasDeclaration(parent) || ts.isHeritageClause(parent) || ts.isExpressionWithTypeArguments(parent) || ts.isImportTypeNode(parent) ) { return true; } if (ts.isImportDeclaration(parent) && parent.importClause?.isTypeOnly) { return true; } if (ts.isImportSpecifier(parent) && parent.isTypeOnly) { return true; } return false; }); } function getExpressionNode(node: ts.Node): ts.Node { if ( (ts.isIdentifier(node) || ts.isPrivateIdentifier(node)) && ts.isPropertyAccessExpression(node.parent) && node.parent.name === node ) { return node.parent; } return node; } function isAssignmentOperator(kind: ts.SyntaxKind): boolean { return kind >= ts.SyntaxKind.FirstAssignment && kind <= ts.SyntaxKind.LastAssignment; } function ancestorMatches(node: ts.Node, matches: (node: ts.Node) => boolean): boolean { let current: ts.Node | undefined = node; while (current) { if (matches(current)) { return true; } current = current.parent; } return false; }