Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 21, 2024
1 parent d5be422 commit 6cdeeab
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ export function generateDtsTypes(sourceCode: string): string {
continue
}

if (line.trim().startsWith('import')) {
imports.push(line)
continue
}

if (line.trim().startsWith('export') && !line.includes('{')) {
exports.push(line)
continue
}

if (isMultiLineDeclaration || line.trim().startsWith('export')) {
currentDeclaration += `${line}\n`
bracketCount += (line.match(/\{/g) || []).length - (line.match(/\}/g) || []).length
Expand All @@ -51,9 +61,6 @@ export function generateDtsTypes(sourceCode: string): string {
isMultiLineDeclaration = true
}
}
else if (line.trim().startsWith('import')) {
imports.push(line)
}
}

// Combine imports, declarations, and exports
Expand Down Expand Up @@ -145,7 +152,35 @@ function parseObjectLiteral(objectLiteral: string): string {
// Remove the opening and closing braces and newlines
const content = objectLiteral.replace(/^\{|\}$/g, '').replace(/\n/g, ' ').trim()

const pairs = content.split(',').map(pair => pair.trim()).filter(Boolean)
const pairs = []
let currentPair = ''
let inQuotes = false
let quoteChar = ''

for (let i = 0; i < content.length; i++) {
const char = content[i]
if ((char === '"' || char === '\'') && content[i - 1] !== '\\') {
if (!inQuotes) {
inQuotes = true
quoteChar = char
}
else if (char === quoteChar) {
inQuotes = false
}
}

if (char === ',' && !inQuotes) {
pairs.push(currentPair.trim())
currentPair = ''
}
else {
currentPair += char
}
}

if (currentPair.trim()) {
pairs.push(currentPair.trim())
}

const parsedProperties = pairs.map((pair) => {
const [key, ...valueParts] = pair.split(':')
Expand Down

0 comments on commit 6cdeeab

Please sign in to comment.