Skip to content

Commit 61a188a

Browse files
committed
chore: wip
1 parent 382d80f commit 61a188a

File tree

1 file changed

+54
-40
lines changed

1 file changed

+54
-40
lines changed

src/extract.ts

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export function generateDtsTypes(sourceCode: string): string {
2222
let lastCommentBlock = ''
2323

2424
function processDeclaration(declaration: string): string {
25+
console.log('processDeclaration input:', declaration)
26+
2527
// Remove comments
2628
const declWithoutComments = declaration.replace(/\/\/.*$/gm, '').trim()
2729
const trimmed = declWithoutComments
@@ -74,72 +76,84 @@ export function generateDtsTypes(sourceCode: string): string {
7476
return ''
7577
}
7678

77-
function preserveValueType(value: string): string {
78-
value = value.trim()
79-
if (value.startsWith('\'') || value.startsWith('"')) {
80-
// Preserve string literals exactly as they appear in the source
81-
// Ensure that the entire string is captured, including any special characters
82-
const match = value.match(/^(['"])(.*)\1$/)
83-
if (match) {
84-
return `'${match[2]}'` // Return the content of the string, wrapped in single quotes
85-
}
86-
return 'string' // Fallback to string if the regex doesn't match
87-
}
88-
else if (value === 'true' || value === 'false') {
89-
return value // Keep true and false as literal types
90-
}
91-
else if (!Number.isNaN(Number(value))) {
92-
return value // Keep numbers as literal types
93-
}
94-
else if (value.startsWith('[') && value.endsWith(']')) {
95-
return 'any[]' // Generic array type
96-
}
97-
else {
98-
return 'string' // Default to string for other cases
99-
}
100-
}
101-
10279
function parseObjectLiteral(objectLiteral: string): string {
80+
console.log('parseObjectLiteral input:', objectLiteral)
10381
// Remove the opening and closing braces
10482
const content = objectLiteral.slice(1, -1).trim()
83+
console.log('Cleaned content:', content)
10584

106-
// Split the object literal into key-value pairs, respecting nested structures
10785
const pairs = []
10886
let currentPair = ''
109-
let nestLevel = 0
11087
let inQuotes = false
88+
let bracketCount = 0
11189

11290
for (const char of content) {
113-
if (char === '{' && !inQuotes)
114-
nestLevel++
115-
if (char === '}' && !inQuotes)
116-
nestLevel--
117-
if (char === '"' || char === '\'')
91+
if (char === '"' || char === '\'') {
11892
inQuotes = !inQuotes
93+
}
94+
else if (!inQuotes) {
95+
if (char === '{')
96+
bracketCount++
97+
if (char === '}')
98+
bracketCount--
99+
}
119100

120-
if (char === ',' && nestLevel === 0 && !inQuotes) {
101+
if (char === ',' && !inQuotes && bracketCount === 0) {
121102
pairs.push(currentPair.trim())
122103
currentPair = ''
123104
}
124105
else {
125106
currentPair += char
126107
}
127108
}
128-
if (currentPair)
109+
110+
if (currentPair.trim()) {
129111
pairs.push(currentPair.trim())
112+
}
130113

131-
const parsedProperties = pairs.map((pair) => {
132-
const [key, ...valueParts] = pair.split(':')
133-
const value = valueParts.join(':').trim() // Rejoin in case the value contained a colon
114+
console.log('Pairs:', pairs)
134115

135-
if (!key)
116+
const parsedProperties = pairs.map((pair) => {
117+
console.log('Processing pair:', pair)
118+
const colonIndex = pair.indexOf(':')
119+
if (colonIndex === -1)
136120
return null // Invalid pair
137121

122+
const key = pair.slice(0, colonIndex).trim()
123+
const value = pair.slice(colonIndex + 1).trim()
124+
console.log('Key:', key, 'Value:', value)
125+
138126
const sanitizedValue = preserveValueType(value)
139-
return ` ${key.trim()}: ${sanitizedValue};`
127+
return ` ${key}: ${sanitizedValue};`
140128
}).filter(Boolean)
141129

142-
return `{\n${parsedProperties.join('\n')}\n}`
130+
const result = `{\n${parsedProperties.join('\n')}\n}`
131+
console.log('parseObjectLiteral output:', result)
132+
return result
133+
}
134+
135+
function preserveValueType(value: string): string {
136+
console.log('preserveValueType input:', value)
137+
value = value.trim()
138+
let result
139+
if (value.startsWith('\'') || value.startsWith('"')) {
140+
// Handle string literals, including URLs
141+
result = value // Keep the original string as is
142+
}
143+
else if (value === 'true' || value === 'false') {
144+
result = value // Keep true and false as literal types
145+
}
146+
else if (!Number.isNaN(Number(value))) {
147+
result = value // Keep numbers as literal types
148+
}
149+
else if (value.startsWith('[') && value.endsWith(']')) {
150+
result = 'any[]' // Generic array type
151+
}
152+
else {
153+
result = 'string' // Default to string for other cases
154+
}
155+
console.log('preserveValueType output:', result)
156+
return result
143157
}
144158

145159
for (let i = 0; i < lines.length; i++) {

0 commit comments

Comments
 (0)