Skip to content

Commit d74fcee

Browse files
committed
chore: wip
1 parent 752fc35 commit d74fcee

File tree

2 files changed

+57
-57
lines changed

2 files changed

+57
-57
lines changed

fixtures/output/example-0001.d.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,6 @@ export declare type DynamicRecord<K extends PropertyKey> = {
177177
/**
178178
* Regular expression patterns used throughout the module
179179
*/
180-
/** Import type declarations */
181-
/** Regular import declarations */
182-
/** Async function declarations */
183-
/** Generic type parameters */
184-
/** Module declaration pattern */
185-
/**
186-
* Module augmentation pattern
187-
*/
188180
declare interface RegexPatterns {
189181
readonly typeImport: RegExp
190182
readonly regularImport: RegExp

src/extract.ts

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -622,41 +622,24 @@ function processImports(line: string, state: ImportTrackingState): void {
622622
}
623623
}
624624

625-
function processDeclarationBlock(
626-
lines: string[],
627-
comments: string[],
628-
state: ProcessingState,
629-
): void {
630-
console.log('Processing declaration block:', { lines, comments })
631-
632-
// Only include JSDoc comments
633-
const jsdocComments = comments.filter(isJSDocComment)
634-
console.log('Filtered JSDoc comments:', jsdocComments)
635-
636-
// Add JSDoc comments directly before the declaration
637-
if (jsdocComments.length > 0) {
638-
// Directly add the comments without the extra newline
639-
state.dtsLines.push(...jsdocComments.map(comment => comment.trimEnd()))
640-
}
641-
642-
// Remove any non-JSDoc comments that might have slipped through
643-
const cleanedLines = lines.map((line) => {
644-
const commentIndex = line.indexOf('//')
645-
return commentIndex !== -1 ? line.substring(0, commentIndex).trim() : line
646-
}).filter(Boolean)
647-
648-
const declaration = cleanedLines.join('\n').trim()
625+
function processTypeDeclaration(declaration: string, isExported = true): string {
626+
const lines = declaration.split('\n')
627+
const firstLine = lines[0].trim()
649628

650-
if (!declaration) {
651-
console.log('Empty declaration, skipping')
652-
return
629+
// Preserve direct type exports
630+
if (firstLine.startsWith('export type {')) {
631+
return declaration
653632
}
654633

655-
// Remove leading comments and whitespace when checking its type
656-
const declarationWithoutComments = removeLeadingComments(declaration).trimStart()
634+
// Only modify the first line
635+
const prefix = isExported ? 'export declare' : 'declare'
636+
const modifiedFirstLine = lines[0].replace(
637+
/^(\s*)(?:export\s+)?type(?!\s*\{)/,
638+
`$1${prefix} type`,
639+
)
657640

658-
// Process the declaration as before
659-
processSpecificDeclaration(declarationWithoutComments, declaration, state)
641+
// Return original declaration with only the first line modified
642+
return [modifiedFirstLine, ...lines.slice(1)].join('\n')
660643
}
661644

662645
function processSpecificDeclaration(
@@ -905,45 +888,70 @@ function processFunctionDeclaration(
905888
* Process interface declarations
906889
*/
907890
function processInterfaceDeclaration(declaration: string, isExported = true): string {
891+
// Split into lines while preserving all formatting and comments
908892
const lines = declaration.split('\n')
909893

910-
// Only modify the first line to add 'declare' if needed
894+
// Only modify the first line to add necessary keywords
911895
const firstLine = lines[0]
912896
const prefix = isExported ? 'export declare' : 'declare'
913897

914-
// Replace 'export interface' or 'interface' with the correct prefix
898+
// Replace only the 'interface' or 'export interface' part
915899
const modifiedFirstLine = firstLine.replace(
916900
/^(\s*)(?:export\s+)?interface/,
917901
`$1${prefix} interface`,
918902
)
919903

920-
// Return the modified first line with all other lines unchanged
904+
// Return original declaration with only the first line modified
921905
return [modifiedFirstLine, ...lines.slice(1)].join('\n')
922906
}
923907

924908
/**
925909
* Process type declarations
926910
*/
927-
function processTypeDeclaration(declaration: string, isExported = true): string {
928-
const lines = declaration.split('\n')
929-
const firstLine = lines[0].trim()
911+
function processDeclarationBlock(
912+
lines: string[],
913+
comments: string[],
914+
state: ProcessingState,
915+
): void {
916+
const declarationText = lines.join('\n')
917+
const cleanedDeclaration = removeLeadingComments(declarationText).trimStart()
930918

931-
// Preserve direct type exports (export type { X })
932-
if (firstLine.startsWith('export type {')) {
933-
return declaration
919+
if (
920+
cleanedDeclaration.startsWith('interface')
921+
|| cleanedDeclaration.startsWith('export interface')
922+
|| cleanedDeclaration.startsWith('type')
923+
|| cleanedDeclaration.startsWith('export type')
924+
) {
925+
// Process the declaration while preserving all formatting and comments
926+
const isInterface = cleanedDeclaration.startsWith('interface') || cleanedDeclaration.startsWith('export interface')
927+
const isExported = declarationText.trimStart().startsWith('export')
928+
929+
const processed = isInterface
930+
? processInterfaceDeclaration(declarationText, isExported)
931+
: processTypeDeclaration(declarationText, isExported)
932+
933+
state.dtsLines.push(processed)
934+
return
934935
}
935936

936-
// Only modify the first line to add 'declare' if needed
937-
const prefix = isExported ? 'export declare' : 'declare'
937+
// Handle other declarations as before...
938+
const jsdocComments = comments.filter(isJSDocComment)
939+
if (jsdocComments.length > 0) {
940+
state.dtsLines.push(...jsdocComments.map(comment => comment.trimEnd()))
941+
}
938942

939-
// Replace 'export type' or 'type' with the correct prefix
940-
const modifiedFirstLine = lines[0].replace(
941-
/^(\s*)(?:export\s+)?type(?!\s*\{)/,
942-
`$1${prefix} type`,
943-
)
943+
const cleanedLines = lines.map((line) => {
944+
const commentIndex = line.indexOf('//')
945+
return commentIndex !== -1 ? line.substring(0, commentIndex).trim() : line
946+
}).filter(Boolean)
944947

945-
// Return the modified first line with all other lines unchanged
946-
return [modifiedFirstLine, ...lines.slice(1)].join('\n')
948+
const declaration = cleanedLines.join('\n').trim()
949+
if (!declaration) {
950+
return
951+
}
952+
953+
const declarationWithoutComments = removeLeadingComments(declaration).trimStart()
954+
processSpecificDeclaration(declarationWithoutComments, declaration, state)
947955
}
948956

949957
function processSourceFile(content: string, state: ProcessingState): void {

0 commit comments

Comments
 (0)