Skip to content

Commit 8aca257

Browse files
committed
chore: wip
1 parent c71dccd commit 8aca257

File tree

1 file changed

+75
-66
lines changed

1 file changed

+75
-66
lines changed

src/extract.ts

Lines changed: 75 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,62 @@ export function generateDtsTypes(sourceCode: string): string {
7474
return result
7575
}
7676

77+
function parseObjectLiteral(objectLiteral: string): string {
78+
// Remove the opening and closing braces
79+
const content = objectLiteral.slice(1, -1).trim()
80+
81+
const pairs = []
82+
let currentPair = ''
83+
let inQuotes = false
84+
let bracketCount = 0
85+
86+
for (let i = 0; i < content.length; i++) {
87+
const char = content[i]
88+
89+
if (char === '"' || char === '\'') {
90+
inQuotes = !inQuotes
91+
}
92+
else if (!inQuotes) {
93+
if (char === '{')
94+
bracketCount++
95+
if (char === '}')
96+
bracketCount--
97+
}
98+
99+
if (char === ',' && !inQuotes && bracketCount === 0) {
100+
pairs.push(currentPair.trim())
101+
currentPair = ''
102+
}
103+
else {
104+
currentPair += char
105+
}
106+
}
107+
108+
if (currentPair.trim()) {
109+
pairs.push(currentPair.trim())
110+
}
111+
112+
const parsedProperties = pairs.map((pair) => {
113+
const colonIndex = pair.indexOf(':')
114+
if (colonIndex === -1)
115+
return null // Invalid pair
116+
117+
const key = pair.slice(0, colonIndex).trim()
118+
const value = pair.slice(colonIndex + 1).trim()
119+
120+
// If the value is a string literal, keep it as is
121+
if (value.startsWith('\'') || value.startsWith('"')) {
122+
return ` ${key}: ${value};`
123+
}
124+
125+
// For other types, use preserveValueType
126+
const preservedValue = preserveValueType(value)
127+
return ` ${key}: ${preservedValue};`
128+
}).filter(Boolean)
129+
130+
return `{\n${parsedProperties.join('\n')}\n}`
131+
}
132+
77133
function processDeclaration(declaration: string): string {
78134
// Remove comments
79135
const declWithoutComments = declaration.replace(/\/\/.*$/gm, '').trim()
@@ -90,16 +146,10 @@ function processDeclaration(declaration: string): string {
90146

91147
// Handle multi-line object literals
92148
if (value.startsWith('{')) {
93-
let bracketCount = 1
94-
let i = 1
95-
while (bracketCount > 0 && i < value.length) {
96-
if (value[i] === '{')
97-
bracketCount++
98-
if (value[i] === '}')
99-
bracketCount--
100-
i++
149+
const lastBracketIndex = trimmed.lastIndexOf('}')
150+
if (lastBracketIndex !== -1) {
151+
value = trimmed.slice(equalIndex + 1, lastBracketIndex + 1).trim()
101152
}
102-
value = value.slice(0, i)
103153
}
104154

105155
const declaredType = name.includes(':') ? name.split(':')[1].trim() : null
@@ -127,74 +177,33 @@ function processDeclaration(declaration: string): string {
127177
}
128178
}
129179

130-
// Handle other declarations (interfaces, types, functions)
131-
if (trimmed.startsWith('export')) {
132-
return trimmed.endsWith(';') ? trimmed : `${trimmed};`
180+
// Handle interface declarations
181+
if (trimmed.startsWith('export interface')) {
182+
return `export declare interface ${trimmed.slice('export interface'.length).trim()};`
133183
}
134184

135-
return ''
136-
}
137-
138-
function parseObjectLiteral(objectLiteral: string): string {
139-
// Remove the opening and closing braces
140-
const content = objectLiteral.slice(1, -1).trim()
141-
142-
const pairs = []
143-
let currentPair = ''
144-
let inQuotes = false
145-
let bracketCount = 0
146-
let escapeNext = false
147-
148-
for (let i = 0; i < content.length; i++) {
149-
const char = content[i]
150-
151-
if (!escapeNext && (char === '"' || char === '\'')) {
152-
inQuotes = !inQuotes
153-
}
154-
else if (!inQuotes) {
155-
if (char === '{')
156-
bracketCount++
157-
if (char === '}')
158-
bracketCount--
159-
}
160-
161-
if (char === ',' && !inQuotes && bracketCount === 0) {
162-
pairs.push(currentPair.trim())
163-
currentPair = ''
164-
}
165-
else {
166-
currentPair += char
167-
}
168-
169-
escapeNext = char === '\\' && !escapeNext
185+
// Handle type declarations
186+
if (trimmed.startsWith('export type')) {
187+
return `export declare type ${trimmed.slice('export type'.length).trim()};`
170188
}
171189

172-
if (currentPair.trim()) {
173-
pairs.push(currentPair.trim())
190+
// Handle function declarations
191+
if (trimmed.startsWith('export function')) {
192+
const functionSignature = trimmed.split('{')[0].trim()
193+
return `export declare ${functionSignature.slice('export'.length).trim()};`
174194
}
175195

176-
const parsedProperties = pairs.map((pair) => {
177-
const colonIndex = pair.indexOf(':')
178-
if (colonIndex === -1)
179-
return null // Invalid pair
180-
181-
const key = pair.slice(0, colonIndex).trim()
182-
const value = pair.slice(colonIndex + 1).trim()
183-
184-
const sanitizedValue = preserveValueType(value)
185-
return ` ${key}: ${sanitizedValue};`
186-
}).filter(Boolean)
196+
// Handle other declarations
197+
if (trimmed.startsWith('export')) {
198+
return trimmed.endsWith(';') ? trimmed : `${trimmed};`
199+
}
187200

188-
return `{\n${parsedProperties.join('\n')}\n}`
201+
return ''
189202
}
190203

191204
function preserveValueType(value: string): string {
192205
value = value.trim()
193-
if (value.startsWith('\'') || value.startsWith('"')) {
194-
// Handle string literals, including URLs
195-
return value // Keep the original string as is, including quotes
196-
}
197-
else if (value === 'true' || value === 'false') {
206+
if (value === 'true' || value === 'false') {
198207
return value // Keep true and false as literal types
199208
}
200209
else if (!Number.isNaN(Number(value))) {

0 commit comments

Comments
 (0)