@@ -22,6 +22,8 @@ export function generateDtsTypes(sourceCode: string): string {
22
22
let lastCommentBlock = ''
23
23
24
24
function processDeclaration ( declaration : string ) : string {
25
+ console . log ( 'processDeclaration input:' , declaration )
26
+
25
27
// Remove comments
26
28
const declWithoutComments = declaration . replace ( / \/ \/ .* $ / gm, '' ) . trim ( )
27
29
const trimmed = declWithoutComments
@@ -74,72 +76,84 @@ export function generateDtsTypes(sourceCode: string): string {
74
76
return ''
75
77
}
76
78
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
-
102
79
function parseObjectLiteral ( objectLiteral : string ) : string {
80
+ console . log ( 'parseObjectLiteral input:' , objectLiteral )
103
81
// Remove the opening and closing braces
104
82
const content = objectLiteral . slice ( 1 , - 1 ) . trim ( )
83
+ console . log ( 'Cleaned content:' , content )
105
84
106
- // Split the object literal into key-value pairs, respecting nested structures
107
85
const pairs = [ ]
108
86
let currentPair = ''
109
- let nestLevel = 0
110
87
let inQuotes = false
88
+ let bracketCount = 0
111
89
112
90
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 === '\'' ) {
118
92
inQuotes = ! inQuotes
93
+ }
94
+ else if ( ! inQuotes ) {
95
+ if ( char === '{' )
96
+ bracketCount ++
97
+ if ( char === '}' )
98
+ bracketCount --
99
+ }
119
100
120
- if ( char === ',' && nestLevel === 0 && ! inQuotes ) {
101
+ if ( char === ',' && ! inQuotes && bracketCount === 0 ) {
121
102
pairs . push ( currentPair . trim ( ) )
122
103
currentPair = ''
123
104
}
124
105
else {
125
106
currentPair += char
126
107
}
127
108
}
128
- if ( currentPair )
109
+
110
+ if ( currentPair . trim ( ) ) {
129
111
pairs . push ( currentPair . trim ( ) )
112
+ }
130
113
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 )
134
115
135
- if ( ! key )
116
+ const parsedProperties = pairs . map ( ( pair ) => {
117
+ console . log ( 'Processing pair:' , pair )
118
+ const colonIndex = pair . indexOf ( ':' )
119
+ if ( colonIndex === - 1 )
136
120
return null // Invalid pair
137
121
122
+ const key = pair . slice ( 0 , colonIndex ) . trim ( )
123
+ const value = pair . slice ( colonIndex + 1 ) . trim ( )
124
+ console . log ( 'Key:' , key , 'Value:' , value )
125
+
138
126
const sanitizedValue = preserveValueType ( value )
139
- return ` ${ key . trim ( ) } : ${ sanitizedValue } ;`
127
+ return ` ${ key } : ${ sanitizedValue } ;`
140
128
} ) . filter ( Boolean )
141
129
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
143
157
}
144
158
145
159
for ( let i = 0 ; i < lines . length ; i ++ ) {
0 commit comments