Parsee Examples
Real-world examples and common patterns for using Parsee in your applications.
API Documentation Generator
Automatically generate API docs from source code:
ts
import { parseWithTsMorph } from "@briklab/parsee";
class APIDocGenerator {
generateDocs(sourceCode: string, filename: string): string {
const items = parseWithTsMorph(sourceCode, filename, {
Name: "include",
Type: "include",
JSDoc: "include",
Members: "include"
});
let docs = "# API Documentation\n\n";
items.forEach(item => {
if (item.kind === "class") {
docs += `## Class: ${item.Name}\n\n`;
} else if (item.kind === "function") {
docs += `### Function: ${item.Name}\n`;
if ("Type" in item) {
docs += `Returns: ${item.Type}\n\n`;
}
} else if (item.kind === "interface") {
docs += `### Interface: ${item.Name}\n\n`;
}
});
return docs;
}
}
const generator = new APIDocGenerator();
const docs = generator.generateDocs(sourceCode, "api.ts");Type Definition Analyzer
Analyze type definitions in code:
ts
import { parseWithTsMorph } from "@briklab/parsee";
const source = `
export interface User {
id: number;
name: string;
email: string;
role: "admin" | "user";
}
export type UserInput = Omit<User, "id">;
`;
const items = parseWithTsMorph(source, "types.ts", {
Name: "include",
Type: "include"
});
items.forEach(item => {
console.log(`${item.kind}: ${item.Name}`);
});Live Demo - Code Analysis
Console
No logs yet.
Validation Schema Generator
Generate validation schemas from TypeScript interfaces:
ts
import { parseWithTsMorph } from "@briklab/parsee";
class SchemaGenerator {
generate(typeScriptCode: string): any {
const items = parseWithTsMorph(typeScriptCode, "types.ts", {
Name: "include",
Type: "include",
Members: "include"
});
const schema: any = {};
items.forEach(item => {
if (item.kind === "interface") {
schema[item.Name] = {};
}
});
return schema;
}
}
const generator = new SchemaGenerator();
const schema = generator.generate(sourceCode);Export Analyzer
List all exported symbols:
ts
import { parseWithTsMorph } from "@briklab/parsee";
class ExportAnalyzer {
analyzeExports(source: string, filename: string): string[] {
const items = parseWithTsMorph(source, filename, {
Name: "include"
});
return items
.filter(item => {
if ("Export" in item) {
return item.Export !== "not-exported";
}
return false;
})
.map(item => item.Name || "");
}
}
const analyzer = new ExportAnalyzer();
const exports = analyzer.analyzeExports(sourceCode, "module.ts");
console.log("Exported symbols:", exports);Code Coverage Calculator
Find untested code paths:
ts
import { parseWithTsMorph } from "@briklab/parsee";
class CoverageAnalyzer {
analyze(source: string): void {
const items = parseWithTsMorph(source, "code.ts", {
Name: "include",
Type: "include"
});
const functionCount = items.filter(i => i.kind === "function").length;
const classCount = items.filter(i => i.kind === "class").length;
console.log(`Found ${functionCount} functions`);
console.log(`Found ${classCount} classes`);
console.log(`Total items: ${items.length}`);
}
}
const analyzer = new CoverageAnalyzer();
analyzer.analyze(sourceCode);Dependency Tracker
Track imports and dependencies:
ts
import { parseWithTsMorph } from "@briklab/parsee";
class DependencyTracker {
trackDependencies(sourceCode: string): string[] {
// Parse and analyze imports
const items = parseWithTsMorph(sourceCode, "code.ts", {
Name: "include"
});
// Extract dependency information
const dependencies: string[] = [];
// Add logic to identify imported modules
if (sourceCode.includes("import")) {
const importRegex = /import.*from\s['"]([^'"]+)['"]/g;
let match;
while ((match = importRegex.exec(sourceCode)) !== null) {
dependencies.push(match[1]);
}
}
return dependencies;
}
}
const tracker = new DependencyTracker();
const deps = tracker.trackDependencies(sourceCode);
console.log("Dependencies:", deps);Next Steps
- Advanced Usage: Complex patterns
- Getting Started: Back to basics