Skip to content

Commit 8dc5b80

Browse files
committed
chore: wip
chore: wip
1 parent ccf56ac commit 8dc5b80

7 files changed

Lines changed: 130 additions & 19 deletions

File tree

.vscode/dictionary.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ destructurable
1515
dtsx
1616
entrypoints
1717
heroicons
18+
iconify
1819
localtunnels
1920
lockb
2021
mkcert
@@ -27,12 +28,16 @@ Postcardware
2728
postcompile
2829
prefetch
2930
preinstall
31+
shikijs
3032
socio
3133
softprops
3234
Solana
3335
stacksjs
3436
tlsx
37+
twoslash
3538
typecheck
39+
unconfig
40+
unocss
3641
unplugin
3742
unref
3843
upath

eslint.config.js renamed to eslint.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { ESLintConfig } from '@stacksjs/eslint-config'
12
import stacks from '@stacksjs/eslint-config'
23

3-
export default stacks({
4+
const config: ESLintConfig = stacks({
45
stylistic: {
56
indent: 2,
67
quotes: 'single',
@@ -13,3 +14,5 @@ export default stacks({
1314
'fixtures/**',
1415
],
1516
})
17+
18+
export default config

fixtures/output/0005.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ declare function createProcessingState(): ProcessingState;
1818
declare function createImportTrackingState(): ImportTrackingState;
1919
declare function indentMultilineType(type: string, baseIndent: string, isLast: boolean): string;
2020
declare function inferValueType(value: string): string;
21-
declare function inferArrayType(value: string, state?: ProcessingState, preserveLineBreaks): string;
22-
declare function inferComplexObjectType(value: string, state?: ProcessingState, indentLevel): string;
21+
declare function inferArrayType(value: string, state?: ProcessingState, preserveLineBreaks?: boolean): string;
22+
declare function inferComplexObjectType(value: string, state?: ProcessingState, indentLevel?: number): string;
2323
declare function inferConstArrayType(value: string, state?: ProcessingState): string;
2424
declare function inferConstType(value: string, state: ProcessingState): string;
2525
declare function inferTypeFromDefaultValue(defaultValue: string): string;
@@ -48,16 +48,16 @@ declare function processModuleBlock(cleanDeclaration: string, declarationText: s
4848
export declare function processSpecificDeclaration(declarationWithoutComments: string, fullDeclaration: string, state: ProcessingState): void;
4949
declare function processSourceFile(content: string, state: ProcessingState): void;
5050
declare function processImports(line: string, state: ImportTrackingState): void;
51-
declare function processType(declaration: string, isExported): string;
51+
declare function processType(declaration: string, isExported?: boolean): string;
5252
declare function processTypeExport(line: string, state: ProcessingState): void;
5353
declare function processVariable(declaration: string, isExported: boolean, state: ProcessingState): string;
54-
declare function processFunction(declaration: string, usedTypes?: Set<string>, isExported): string;
54+
declare function processFunction(declaration: string, usedTypes?: Set<string>, isExported?: boolean): string;
5555
declare function getCleanDeclaration(declaration: string): string;
5656
declare function processGeneratorFunction(declaration: string): string;
57-
declare function processInterface(declaration: string, isExported): string;
57+
declare function processInterface(declaration: string, isExported?: boolean): string;
5858
declare function processModule(declaration: string): string;
5959
declare function processObjectMethod(declaration: string): ProcessedMethod;
60-
declare function processObjectProperties(content: string, state?: ProcessingState, indentLevel): Array<{ key: string, value: string }>;
60+
declare function processObjectProperties(content: string, state?: ProcessingState, indentLevel?: number): Array<{ key: string, value: string }>;
6161
declare function processPropertyValue(value: string, indentLevel: number, state?: ProcessingState): string;
6262
declare function trackTypeUsage(content: string, state: ImportTrackingState): void;
6363
declare function trackValueUsage(content: string, state: ImportTrackingState): void;

fixtures/output/0007.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ export declare interface Cert {
66
privateKey: string
77
}
88
export declare function generateRandomSerial(verbose?: boolean): string;
9-
export declare function calculateValidityDates(options: { validityDays?: number, validityYears?: number, notBeforeDays?: number, verbose?: boolean }): void;
9+
export declare function calculateValidityDates(options: {
10+
validityDays?: number
11+
validityYears?: number
12+
notBeforeDays?: number
13+
verbose?: boolean
14+
}): void;
1015
declare function generateCertificateExtensions(options: CertificateOptions): void;
11-
export declare function createRootCA(options: CAOptions): Promise<GenerateCertReturn>;
16+
export declare function createRootCA(options: CAOptions = {}): Promise<GenerateCertReturn>;
1217
export declare function generateCertificate(options: CertificateOptions): Promise<GenerateCertReturn>;
1318
export declare function addCertToSystemTrustStoreAndSaveCert(cert: Cert, caCert: string, options?: TlsOption): Promise<string>;
1419
export declare function storeCertificate(cert: Cert, options?: TlsOption): string;

fixtures/output/0009.d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
export declare function loadConfig<T>({ name, cwd, defaultConfig, endpoint, headers): Promise<T>;
1+
import type { Config } from './types';
2+
3+
export declare function loadConfig<T>({
4+
name,
5+
cwd,
6+
defaultConfig,
7+
endpoint,
8+
headers = {
9+
'Accept': 'application/json',
10+
'Content-Type': 'application/json',
11+
},
12+
}: Config<T>): Promise<T>;
213

314
export * from './types'
415
export * from './utils'

fixtures/output/imports.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { something as dts } from './generate';
55

66
export declare function actionsPath(path?: string): string;
77
export declare function corePath(path?: string): string;
8-
export declare function frameworkPath(path?: string, options?: { relative?: , boolean, cwd?: , string }): string;
8+
export declare function frameworkPath(path?: string, options?: { relative?: boolean, cwd?: string }): string;
99
export declare function storagePath(path?: string): string;
10-
export declare function projectPath(filePath, options?: { relative: , boolean }): string;
11-
export declare function userActionsPath(path?: string, options?: { relative: , true }): string;
12-
export declare function builtUserActionsPath(path?: string, options?: { relative: , boolean }): string;
10+
export declare function projectPath(filePath = '', options?: { relative: boolean }): string;
11+
export declare function userActionsPath(path?: string, options?: { relative: true }): string;
12+
export declare function builtUserActionsPath(path?: string, options?: { relative: boolean }): string;
1313
export declare function homeDir(path?: string): string;
1414
export declare type LibraryType = 'vue-components' | 'web-components' | 'functions'
1515
export declare function libraryEntryPath(type: LibraryType): string;

src/extract.ts

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ function extractFunctionSignature(declaration: string, verbose?: boolean | strin
196196
rest = restAfterGenerics.trim()
197197
debugLog('signature-after-generics', `Remaining content: ${rest}`, verbose)
198198

199-
// Extract parameters
199+
// Extract parameters with full object type support
200200
const { params, rest: restAfterParams } = extractParams(rest, verbose)
201201
rest = restAfterParams.trim()
202202
debugLog('signature-after-params', `Remaining content: ${rest}`, verbose)
@@ -205,10 +205,20 @@ function extractFunctionSignature(declaration: string, verbose?: boolean | strin
205205
const { returnType } = extractReturnType(rest)
206206
debugLog('signature-return', `Extracted return type: ${returnType}`, verbose)
207207

208+
// Handle object parameter types
209+
let processedParams = params
210+
if (params.includes('{')) {
211+
const objectMatch = params.match(/\{([^}]+)\}:\s*([^)]+)/)
212+
if (objectMatch) {
213+
const [, paramList, typeRef] = objectMatch
214+
processedParams = `{ ${paramList} }: ${typeRef}`
215+
}
216+
}
217+
208218
const signature = {
209219
name,
210220
generics,
211-
params,
221+
params: processedParams,
212222
returnType,
213223
}
214224

@@ -2046,12 +2056,89 @@ function processVariable(declaration: string, isExported: boolean, state: Proces
20462056
* Process function declarations with overloads
20472057
*/
20482058
function processFunction(declaration: string, usedTypes?: Set<string>, isExported = true, verbose?: boolean | string[]): string {
2049-
const normalizedDeclaration = declaration.trim().replace(/\s+/g, ' ')
2059+
const normalizedDeclaration = declaration.trim()
20502060
const signature = extractFunctionSignature(normalizedDeclaration, verbose)
20512061

2052-
// Clean up params
2062+
// Clean up all parameters - both object destructuring and regular parameters
20532063
if (signature.params) {
2054-
signature.params = cleanParameterTypes(signature.params)
2064+
// Handle regular parameters
2065+
if (!signature.params.includes('{')) {
2066+
const cleanedParams = signature.params
2067+
.split(',')
2068+
.map((param) => {
2069+
const paramTrimmed = param.trim()
2070+
if (!paramTrimmed)
2071+
return null
2072+
2073+
// Handle parameters with default values and type annotations
2074+
if (paramTrimmed.includes('=')) {
2075+
const [paramPart] = paramTrimmed.split(/\s*=\s*/)
2076+
const paramWithoutDefault = paramPart.trim()
2077+
2078+
// If there's an explicit type annotation, use it
2079+
if (paramWithoutDefault.includes(':')) {
2080+
const [paramName, paramType] = paramWithoutDefault.split(':').map(p => p.trim())
2081+
return `${paramName}?: ${paramType}`
2082+
}
2083+
2084+
// Infer type from default value if no explicit type
2085+
const defaultValue = paramTrimmed.split(/\s*=\s*/)[1].trim()
2086+
const inferredType = defaultValue === '{}'
2087+
? paramTrimmed.includes(':') ? paramTrimmed.split(':')[1].trim().split('=')[0].trim() : 'Record<string, unknown>'
2088+
: defaultValue === ''
2089+
? 'string'
2090+
: typeof eval(defaultValue)
2091+
2092+
return `${paramWithoutDefault}?: ${inferredType}`
2093+
}
2094+
2095+
// Handle parameters without default values
2096+
if (paramTrimmed.includes(':')) {
2097+
const [paramName, paramType] = paramTrimmed.split(':').map(p => p.trim())
2098+
const isOptional = paramName.endsWith('?')
2099+
const cleanName = paramName.replace(/\?$/, '')
2100+
return `${cleanName}${isOptional ? '?' : ''}: ${paramType}`
2101+
}
2102+
2103+
return paramTrimmed
2104+
})
2105+
.filter(Boolean)
2106+
.join(', ')
2107+
2108+
signature.params = cleanedParams
2109+
}
2110+
// Handle object destructuring (existing code)
2111+
else {
2112+
const paramMatch = declaration.match(/\{([^}]+)\}:\s*([^)]+)/)
2113+
if (paramMatch) {
2114+
const [, paramList, typeRef] = paramMatch
2115+
2116+
const cleanedParams = paramList
2117+
.split(',')
2118+
.map((param) => {
2119+
const paramTrimmed = param.trim()
2120+
if (!paramTrimmed)
2121+
return null
2122+
2123+
const hasDefaultValue = paramTrimmed.includes('=')
2124+
const [paramName] = paramTrimmed.split(/\s*=\s*/)
2125+
const nameOnly = paramName.trim()
2126+
2127+
const isOptional = hasDefaultValue || nameOnly.endsWith('?')
2128+
const cleanName = nameOnly.replace(/\?$/, '')
2129+
2130+
if (cleanName.includes(':')) {
2131+
const [name, type] = cleanName.split(':').map(p => p.trim())
2132+
return `${name}${isOptional ? '?' : ''}: ${type}`
2133+
}
2134+
2135+
return `${cleanName}${isOptional ? '?' : ''}`
2136+
})
2137+
.filter(Boolean)
2138+
2139+
signature.params = `{ ${cleanedParams.join(', ')} }: ${typeRef.trim()}`
2140+
}
2141+
}
20552142
}
20562143

20572144
// Preserve type predicates and complete return types

0 commit comments

Comments
 (0)