Skip to content

Commit

Permalink
chore: improve export processing
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Nov 9, 2024
1 parent 14eea76 commit 2dd4d30
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 3 deletions.
83 changes: 83 additions & 0 deletions fixtures/input/example/0006.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { collect, type Collection } from '@stacksjs/collections'

export {
align,
box,
centerAlign,
colorize,
colors,
getColor,
leftAlign,
rightAlign,
stripAnsi,
} from 'consola/utils'

export * as kolorist from 'kolorist'

export {
ansi256,
ansi256Bg,
bgBlack,
bgBlue,
bgCyan,
bgGray,
bgGreen,
bgLightBlue,
bgLightCyan,
bgLightGray,
bgLightGreen,
bgLightMagenta,
bgLightRed,
bgLightYellow,
bgMagenta,
bgRed,
bgWhite,
bgYellow,
black,
blue,
bold,
cyan,
dim,
gray,
green,
hidden,
inverse,
italic,
lightBlue,
lightCyan,
lightGray,
lightGreen,
lightMagenta,
lightRed,
lightYellow,
link,
magenta,
red,
reset,
strikethrough,
stripColors,
trueColor,
trueColorBg,
underline,
white,
yellow,
} from 'kolorist'

export const quotes: Collection<string[]> = collect([
// could be queried from any API or database
'The best way to get started is to quit talking and begin doing.',
'The pessimist sees difficulty in every opportunity. The optimist sees opportunity in every difficulty.',
'Don’t let yesterday take up too much of today.',
'You learn more from failure than from success. Don’t let it stop you. Failure builds character.',
'It’s not whether you get knocked down, it’s whether you get up.',
'If you are working on something that you really care about, you don’t have to be pushed. The vision pulls you.',
'People who are crazy enough to think they can change the world, are the ones who do.',
'Failure will never overtake me if my determination to succeed is strong enough.',
'Entrepreneurs are great at dealing with uncertainty and also very good at minimizing risk. That’s the classic entrepreneur.',
'We may encounter many defeats but we must not be defeated.',
'Knowing is not enough; we must apply. Wishing is not enough; we must do.',
'Imagine your life is perfect in every respect; what would it look like?',
'We generate fears while we sit. We overcome them by action.',
'Whether you think you can or think you can’t, you’re right.',
'Security is mostly a superstition. Life is either a daring adventure or nothing.',
])
63 changes: 63 additions & 0 deletions fixtures/output/0006.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { Collection } from '@stacksjs/collections';

export {
align,
box,
centerAlign,
colorize,
colors,
getColor,
leftAlign,
rightAlign,
stripAnsi,
} from 'consola/utils'
export {
ansi256,
ansi256Bg,
bgBlack,
bgBlue,
bgCyan,
bgGray,
bgGreen,
bgLightBlue,
bgLightCyan,
bgLightGray,
bgLightGreen,
bgLightMagenta,
bgLightRed,
bgLightYellow,
bgMagenta,
bgRed,
bgWhite,
bgYellow,
black,
blue,
bold,
cyan,
dim,
gray,
green,
hidden,
inverse,
italic,
lightBlue,
lightCyan,
lightGray,
lightGreen,
lightMagenta,
lightRed,
lightYellow,
link,
magenta,
red,
reset,
strikethrough,
stripColors,
trueColor,
trueColorBg,
underline,
white,
yellow,
} from 'kolorist'
export * as kolorist from 'kolorist'
export declare let quotes: Collection<string[]>;
59 changes: 56 additions & 3 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,12 @@ function processExportBlock(cleanDeclaration: string, declarationText: string, s
return false

// Handle various export types
if (cleanDeclaration.startsWith('export {')) {
// Handle multiline exports by preserving the entire declaration
state.dtsLines.push(declarationText)
return true
}

if (processExportedClass(cleanDeclaration, state))
return true
if (processExportedEnum(cleanDeclaration, state))
Expand All @@ -1560,7 +1566,7 @@ function processExportBlock(cleanDeclaration: string, declarationText: string, s
return true

// Handle named exports
if (cleanDeclaration.startsWith('export {')) {
if (cleanDeclaration.includes('export {')) {
state.dtsLines.push(declarationText)
return true
}
Expand All @@ -1573,15 +1579,39 @@ function processExportBlock(cleanDeclaration: string, declarationText: string, s
function processExport(line: string, state: ProcessingState): void {
debugLog('export-processing', `Processing export: ${line}`)

// Handle multiline exports by concatenating until we have a complete statement
if (line.includes('{') && !line.includes('}')) {
state.currentDeclaration = line
return
}

// Continue building multiline export
if (state.currentDeclaration) {
state.currentDeclaration += ` ${line}`
if (!line.includes('}'))
return
line = state.currentDeclaration
state.currentDeclaration = ''
}

const exportMatch = line.match(/export\s*\{([^}]+)\}(?:\s*from\s*['"]([^'"]+)['"])?/)
if (!exportMatch) {
debugLog('export-error', 'Failed to match export pattern')
if (line.startsWith('export {')) {
// If it's a malformed export statement, add it as-is to preserve the declaration
state.dtsLines.push(line)
}
return
}

const [, exports, sourceModule] = exportMatch
debugLog('export-found', `Found exports: ${exports}, source: ${sourceModule || 'local'}`)

// If it's a complete export statement, add it to dtsLines
if (line.startsWith('export {')) {
state.dtsLines.push(line)
}

exports.split(',').forEach((exp) => {
const [itemName, aliasName] = exp.trim().split(/\s+as\s+/).map(e => e.trim())

Expand Down Expand Up @@ -1798,6 +1828,7 @@ function processSourceFile(content: string, state: ProcessingState): void {
let bracketDepth = 0
let angleDepth = 0
let inDeclaration = false
let inExport = false
state.currentScope = 'top'

debugLog('source-processing', `Processing source file with ${lines.length} lines`)
Expand All @@ -1807,7 +1838,27 @@ function processSourceFile(content: string, state: ProcessingState): void {
const line = lines[i]
const trimmedLine = line.trim()

debugLog('source-scan', `First pass - Line ${i + 1}: ${trimmedLine}`)
// Handle export blocks
if (trimmedLine.startsWith('export {')) {
if (trimmedLine.includes('}')) {
// Single-line export
state.dtsLines.push(line)
continue
}
inExport = true
currentBlock = [line]
continue
}

if (inExport) {
currentBlock.push(line)
if (line.includes('}')) {
state.dtsLines.push(currentBlock.join('\n'))
currentBlock = []
inExport = false
}
continue
}

// Process imports
if (line.includes('import ')) {
Expand All @@ -1827,7 +1878,9 @@ function processSourceFile(content: string, state: ProcessingState): void {
if (trimmedLine.startsWith('export {')) {
debugLog('mixed-export', `Found mixed export: ${trimmedLine}`)
processExport(trimmedLine, state)
state.dtsLines.push(line)
if (trimmedLine.includes('}')) {
state.dtsLines.push(line)
}
continue
}
}
Expand Down

0 comments on commit 2dd4d30

Please sign in to comment.