type-compiler
Version:
A TypeScript compiler plugin for enhanced runtime type checking and analysis with Zod validation
104 lines (103 loc) • 3.43 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.shouldProcessFile = shouldProcessFile;
exports.isExported = isExported;
exports.shouldProcessType = shouldProcessType;
exports.generateStableTypeId = generateStableTypeId;
const typescript_1 = __importDefault(require("typescript"));
/**
* Check if a file should be processed based on excludePatterns
*/
function shouldProcessFile(fileName, excludePatterns = []) {
// Skip declaration files
if (fileName.endsWith('.d.ts')) {
return false;
}
// Skip files in node_modules
if (fileName.includes('node_modules')) {
return false;
}
// Skip files matching exclude patterns
for (const pattern of excludePatterns) {
if (matchPattern(fileName, pattern)) {
return false;
}
}
return true;
}
/**
* Match a file path against a glob pattern
*/
function matchPattern(filePath, pattern) {
// Handle common glob patterns
if (pattern === '**/*.test.ts' && filePath.endsWith('.test.ts')) {
return true;
}
if (pattern.includes('**/excluded/**') && filePath.includes('/excluded/')) {
return true;
}
// Simple implementation for other patterns
const regex = new RegExp('^' +
pattern
.replace(/\./g, '\\.')
.replace(/\*\*/g, '.*')
.replace(/\*/g, '[^/]*')
.replace(/\?/g, '.')
+ '$');
return regex.test(filePath);
}
/**
* Check if a declaration is exported
*/
function isExported(node) {
// Cast to a type that might have modifiers
const nodeWithModifiers = node;
// Check for export keyword in modifiers
if (nodeWithModifiers.modifiers &&
Array.isArray(nodeWithModifiers.modifiers) &&
nodeWithModifiers.modifiers.some((modifier) => modifier.kind === typescript_1.default.SyntaxKind.ExportKeyword)) {
return true;
}
// Check if parent is an export declaration
if (node.parent && typescript_1.default.isExportDeclaration(node.parent)) {
return true;
}
return false;
}
/**
* Check if a type should be processed based on options
*/
function shouldProcessType(declaration, excludedTypes = [], includedTypes = []) {
if (!declaration.name) {
return false;
}
const typeName = declaration.name.text;
// Skip types explicitly excluded
if (excludedTypes.includes(typeName)) {
return false;
}
// Only include specified types if the list is provided
if (includedTypes.length > 0 && !includedTypes.includes(typeName)) {
return false;
}
return true;
}
/**
* Generate a stable ID for a type by hashing its name and position
*/
function generateStableTypeId(type, typeChecker) {
// Get the display name of the type
const typeName = typeChecker.typeToString(type);
// If the type has a symbol, get its declaration
let location = '';
if (type.symbol && type.symbol.declarations && type.symbol.declarations.length > 0) {
const declaration = type.symbol.declarations[0];
const sourceFile = declaration.getSourceFile();
location = `${sourceFile.fileName}:${declaration.pos}`;
}
// Combine name and location for a stable ID
return `${typeName}_${location}`;
}