From 84c687ee96d69bb809d848b810cac83746562828 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 21 Oct 2024 19:27:22 +0200 Subject: [PATCH] chore: wip --- src/extract.ts | 109 ++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 55 deletions(-) diff --git a/src/extract.ts b/src/extract.ts index 62d6506..752c924 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -1,7 +1,10 @@ +import { Bun } from 'bun' + +const DEBUG = true // Set to false to disable debug logs + export async function extract(filePath: string): Promise { try { const sourceCode = await Bun.file(filePath).text() - return generateDtsTypes(sourceCode) } catch (error) { @@ -11,7 +14,8 @@ export async function extract(filePath: string): Promise { } function generateDtsTypes(sourceCode: string): string { - console.log('Starting generateDtsTypes') + if (DEBUG) + console.log('Starting generateDtsTypes') const lines = sourceCode.split('\n') const dtsLines: string[] = [] const imports: string[] = [] @@ -25,26 +29,30 @@ function generateDtsTypes(sourceCode: string): string { for (let i = 0; i < lines.length; i++) { const line = lines[i] - console.log(`Processing line ${i + 1}: ${line}`) + if (DEBUG) + console.log(`Processing line ${i + 1}: ${line}`) if (line.trim().startsWith('/**') || line.trim().startsWith('*') || line.trim().startsWith('*/')) { if (line.trim().startsWith('/**')) lastCommentBlock = '' lastCommentBlock += `${line}\n` - console.log('Comment line added to lastCommentBlock') + if (DEBUG) + console.log('Comment line added to lastCommentBlock') continue } if (line.trim().startsWith('import')) { const processedImport = processImport(line) imports.push(processedImport) - console.log(`Processed import: ${processedImport}`) + if (DEBUG) + console.log(`Processed import: ${processedImport}`) continue } if (line.trim().startsWith('export default')) { defaultExport = `${line.trim()};` - console.log(`Default export found: ${defaultExport}`) + if (DEBUG) + console.log(`Default export found: ${defaultExport}`) continue } @@ -56,13 +64,15 @@ function generateDtsTypes(sourceCode: string): string { if (!isMultiLineDeclaration) { if (lastCommentBlock) { dtsLines.push(lastCommentBlock.trimEnd()) - console.log(`Comment block added to dtsLines: ${lastCommentBlock.trimEnd()}`) + if (DEBUG) + console.log(`Comment block added to dtsLines: ${lastCommentBlock.trimEnd()}`) lastCommentBlock = '' } const processed = processConstDeclaration(currentDeclaration.trim()) if (processed) { dtsLines.push(processed) - console.log(`Processed const declaration added to dtsLines: ${processed}`) + if (DEBUG) + console.log(`Processed const declaration added to dtsLines: ${processed}`) } currentDeclaration = '' bracketCount = 0 @@ -71,24 +81,28 @@ function generateDtsTypes(sourceCode: string): string { else if (line.trim().startsWith('export')) { if (lastCommentBlock) { dtsLines.push(lastCommentBlock.trimEnd()) - console.log(`Comment block added to dtsLines: ${lastCommentBlock.trimEnd()}`) + if (DEBUG) + console.log(`Comment block added to dtsLines: ${lastCommentBlock.trimEnd()}`) lastCommentBlock = '' } const processed = processDeclaration(line) if (processed) { dtsLines.push(processed) - console.log(`Processed declaration added to dtsLines: ${processed}`) + if (DEBUG) + console.log(`Processed declaration added to dtsLines: ${processed}`) } } } const result = cleanOutput([...imports, '', ...dtsLines, '', ...exports, defaultExport].filter(Boolean).join('\n')) - console.log('Final result:', result) + if (DEBUG) + console.log('Final result:', result) return result } function processImport(importLine: string): string { - console.log(`Processing import: ${importLine}`) + if (DEBUG) + console.log(`Processing import: ${importLine}`) if (importLine.includes('type')) { return importLine.replace('import', 'import type').replace('type type', 'type') } @@ -96,7 +110,8 @@ function processImport(importLine: string): string { } function processDeclaration(declaration: string): string { - console.log(`Processing declaration: ${declaration}`) + if (DEBUG) + console.log(`Processing declaration: ${declaration}`) if (declaration.startsWith('export const')) { return processConstDeclaration(declaration) } @@ -112,12 +127,14 @@ function processDeclaration(declaration: string): string { else if (declaration.startsWith('export default')) { return `${declaration};` } - console.log(`Declaration not processed: ${declaration}`) + if (DEBUG) + console.log(`Declaration not processed: ${declaration}`) return declaration } function processConstDeclaration(declaration: string): string { - console.log(`Processing const declaration: ${declaration}`) + if (DEBUG) + console.log(`Processing const declaration: ${declaration}`) const lines = declaration.split('\n') const firstLine = lines[0] const name = firstLine.split('export const')[1].split('=')[0].trim().split(':')[0].trim() @@ -141,71 +158,53 @@ function processConstDeclaration(declaration: string): string { } function processInterfaceDeclaration(declaration: string): string { - console.log(`Processing interface declaration: ${declaration}`) + if (DEBUG) + console.log(`Processing interface declaration: ${declaration}`) const lines = declaration.split('\n') const interfaceName = lines[0].split('interface')[1].split('{')[0].trim() const interfaceBody = lines.slice(1, -1).map(line => ` ${line.trim()}`).join('\n') const result = `export declare interface ${interfaceName} {\n${interfaceBody}\n}` - console.log(`Processed interface declaration: ${result}`) + if (DEBUG) + console.log(`Processed interface declaration: ${result}`) return result } function processTypeDeclaration(declaration: string): string { - console.log(`Processing type declaration: ${declaration}`) + if (DEBUG) + console.log(`Processing type declaration: ${declaration}`) const result = declaration.replace('export type', 'export declare type') - console.log(`Processed type declaration: ${result}`) + if (DEBUG) + console.log(`Processed type declaration: ${result}`) return result } function processFunctionDeclaration(declaration: string): string { - console.log(`Processing function declaration: ${declaration}`) + if (DEBUG) + console.log(`Processing function declaration: ${declaration}`) const functionSignature = declaration.split('{')[0].trim() const result = `export declare ${functionSignature.replace('export ', '')};` - console.log(`Processed function declaration: ${result}`) + if (DEBUG) + console.log(`Processed function declaration: ${result}`) return result } -function parseObjectLiteral(objectLiteral: string): string { - console.log(`Parsing object literal: ${objectLiteral}`) - const content = objectLiteral.replace(/^\{|\}$/g, '').split(',').map(pair => pair.trim()) - const parsedProperties = content.map((pair) => { - const [key, value] = pair.split(':').map(p => p.trim()) - console.log(`Parsing property: key=${key}, value=${value}`) - return ` ${key}: ${value};` - }) - const result = `{\n${parsedProperties.join('\n')}\n}` - console.log(`Parsed object literal: ${result}`) - return result -} - -function inferType(properties: string[]): string { - const types = properties.map((prop) => { - const value = prop.split(':')[1].trim() - if (value.startsWith('\'') || value.startsWith('"')) - return 'string' - if (value === 'true' || value === 'false') - return 'boolean' - if (!isNaN(Number(value))) - return 'number' - return 'any' - }) - const uniqueTypes = [...new Set(types)] - return uniqueTypes.length === 1 ? `{ [key: string]: ${uniqueTypes[0]} }` : '{ [key: string]: any }' -} - function cleanOutput(output: string): string { - console.log('Cleaning output') + if (DEBUG) + console.log('Cleaning output') const result = output .replace(/\{\s*\}/g, '{}') .replace(/\s*;\s*(?=\}|$)/g, ';') .replace(/\n+/g, '\n') .replace(/;\n\}/g, ';\n}') .replace(/\{;/g, '{') - .replace(/\};\n/g, '}\n\n') // Add an extra line break after each declaration - .replace(/\}\n(?!$)/g, '}\n\n') // Add an extra line break after closing braces, except for the last one - .replace(/\n{3,}/g, '\n\n') // Replace three or more consecutive newlines with two newlines - .replace(/;\n(\s*)\}/g, ';\n$1\n$1}') // Ensure closing bracket is on its own line + .replace(/\};\n/g, '}\n\n') + .replace(/\}\n(?!$)/g, '}\n\n') + .replace(/\n{3,}/g, '\n\n') + .replace(/;\n(\s*)\}/g, ';\n$1\n$1}') + .replace(/,\n\s*;/g, ';') // Remove unnecessary commas before semicolons + .replace(/;\s*\/\/\s*/g, '; // ') // Ensure comments are properly formatted .trim() - console.log('Cleaned output:', result) + if (DEBUG) + console.log('Cleaned output:', result) return result }