Skip to content

Commit 2dd4d30

Browse files
committed
chore: improve export processing
1 parent 14eea76 commit 2dd4d30

File tree

3 files changed

+202
-3
lines changed

3 files changed

+202
-3
lines changed

fixtures/input/example/0006.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { collect, type Collection } from '@stacksjs/collections'
2+
3+
export {
4+
align,
5+
box,
6+
centerAlign,
7+
colorize,
8+
colors,
9+
getColor,
10+
leftAlign,
11+
rightAlign,
12+
stripAnsi,
13+
} from 'consola/utils'
14+
15+
export * as kolorist from 'kolorist'
16+
17+
export {
18+
ansi256,
19+
ansi256Bg,
20+
bgBlack,
21+
bgBlue,
22+
bgCyan,
23+
bgGray,
24+
bgGreen,
25+
bgLightBlue,
26+
bgLightCyan,
27+
bgLightGray,
28+
bgLightGreen,
29+
bgLightMagenta,
30+
bgLightRed,
31+
bgLightYellow,
32+
bgMagenta,
33+
bgRed,
34+
bgWhite,
35+
bgYellow,
36+
black,
37+
blue,
38+
bold,
39+
cyan,
40+
dim,
41+
gray,
42+
green,
43+
hidden,
44+
inverse,
45+
italic,
46+
lightBlue,
47+
lightCyan,
48+
lightGray,
49+
lightGreen,
50+
lightMagenta,
51+
lightRed,
52+
lightYellow,
53+
link,
54+
magenta,
55+
red,
56+
reset,
57+
strikethrough,
58+
stripColors,
59+
trueColor,
60+
trueColorBg,
61+
underline,
62+
white,
63+
yellow,
64+
} from 'kolorist'
65+
66+
export const quotes: Collection<string[]> = collect([
67+
// could be queried from any API or database
68+
'The best way to get started is to quit talking and begin doing.',
69+
'The pessimist sees difficulty in every opportunity. The optimist sees opportunity in every difficulty.',
70+
'Don’t let yesterday take up too much of today.',
71+
'You learn more from failure than from success. Don’t let it stop you. Failure builds character.',
72+
'It’s not whether you get knocked down, it’s whether you get up.',
73+
'If you are working on something that you really care about, you don’t have to be pushed. The vision pulls you.',
74+
'People who are crazy enough to think they can change the world, are the ones who do.',
75+
'Failure will never overtake me if my determination to succeed is strong enough.',
76+
'Entrepreneurs are great at dealing with uncertainty and also very good at minimizing risk. That’s the classic entrepreneur.',
77+
'We may encounter many defeats but we must not be defeated.',
78+
'Knowing is not enough; we must apply. Wishing is not enough; we must do.',
79+
'Imagine your life is perfect in every respect; what would it look like?',
80+
'We generate fears while we sit. We overcome them by action.',
81+
'Whether you think you can or think you can’t, you’re right.',
82+
'Security is mostly a superstition. Life is either a daring adventure or nothing.',
83+
])

fixtures/output/0006.d.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import type { Collection } from '@stacksjs/collections';
2+
3+
export {
4+
align,
5+
box,
6+
centerAlign,
7+
colorize,
8+
colors,
9+
getColor,
10+
leftAlign,
11+
rightAlign,
12+
stripAnsi,
13+
} from 'consola/utils'
14+
export {
15+
ansi256,
16+
ansi256Bg,
17+
bgBlack,
18+
bgBlue,
19+
bgCyan,
20+
bgGray,
21+
bgGreen,
22+
bgLightBlue,
23+
bgLightCyan,
24+
bgLightGray,
25+
bgLightGreen,
26+
bgLightMagenta,
27+
bgLightRed,
28+
bgLightYellow,
29+
bgMagenta,
30+
bgRed,
31+
bgWhite,
32+
bgYellow,
33+
black,
34+
blue,
35+
bold,
36+
cyan,
37+
dim,
38+
gray,
39+
green,
40+
hidden,
41+
inverse,
42+
italic,
43+
lightBlue,
44+
lightCyan,
45+
lightGray,
46+
lightGreen,
47+
lightMagenta,
48+
lightRed,
49+
lightYellow,
50+
link,
51+
magenta,
52+
red,
53+
reset,
54+
strikethrough,
55+
stripColors,
56+
trueColor,
57+
trueColorBg,
58+
underline,
59+
white,
60+
yellow,
61+
} from 'kolorist'
62+
export * as kolorist from 'kolorist'
63+
export declare let quotes: Collection<string[]>;

src/extract.ts

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,12 @@ function processExportBlock(cleanDeclaration: string, declarationText: string, s
15521552
return false
15531553

15541554
// Handle various export types
1555+
if (cleanDeclaration.startsWith('export {')) {
1556+
// Handle multiline exports by preserving the entire declaration
1557+
state.dtsLines.push(declarationText)
1558+
return true
1559+
}
1560+
15551561
if (processExportedClass(cleanDeclaration, state))
15561562
return true
15571563
if (processExportedEnum(cleanDeclaration, state))
@@ -1560,7 +1566,7 @@ function processExportBlock(cleanDeclaration: string, declarationText: string, s
15601566
return true
15611567

15621568
// Handle named exports
1563-
if (cleanDeclaration.startsWith('export {')) {
1569+
if (cleanDeclaration.includes('export {')) {
15641570
state.dtsLines.push(declarationText)
15651571
return true
15661572
}
@@ -1573,15 +1579,39 @@ function processExportBlock(cleanDeclaration: string, declarationText: string, s
15731579
function processExport(line: string, state: ProcessingState): void {
15741580
debugLog('export-processing', `Processing export: ${line}`)
15751581

1582+
// Handle multiline exports by concatenating until we have a complete statement
1583+
if (line.includes('{') && !line.includes('}')) {
1584+
state.currentDeclaration = line
1585+
return
1586+
}
1587+
1588+
// Continue building multiline export
1589+
if (state.currentDeclaration) {
1590+
state.currentDeclaration += ` ${line}`
1591+
if (!line.includes('}'))
1592+
return
1593+
line = state.currentDeclaration
1594+
state.currentDeclaration = ''
1595+
}
1596+
15761597
const exportMatch = line.match(/export\s*\{([^}]+)\}(?:\s*from\s*['"]([^'"]+)['"])?/)
15771598
if (!exportMatch) {
15781599
debugLog('export-error', 'Failed to match export pattern')
1600+
if (line.startsWith('export {')) {
1601+
// If it's a malformed export statement, add it as-is to preserve the declaration
1602+
state.dtsLines.push(line)
1603+
}
15791604
return
15801605
}
15811606

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

1610+
// If it's a complete export statement, add it to dtsLines
1611+
if (line.startsWith('export {')) {
1612+
state.dtsLines.push(line)
1613+
}
1614+
15851615
exports.split(',').forEach((exp) => {
15861616
const [itemName, aliasName] = exp.trim().split(/\s+as\s+/).map(e => e.trim())
15871617

@@ -1798,6 +1828,7 @@ function processSourceFile(content: string, state: ProcessingState): void {
17981828
let bracketDepth = 0
17991829
let angleDepth = 0
18001830
let inDeclaration = false
1831+
let inExport = false
18011832
state.currentScope = 'top'
18021833

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

1810-
debugLog('source-scan', `First pass - Line ${i + 1}: ${trimmedLine}`)
1841+
// Handle export blocks
1842+
if (trimmedLine.startsWith('export {')) {
1843+
if (trimmedLine.includes('}')) {
1844+
// Single-line export
1845+
state.dtsLines.push(line)
1846+
continue
1847+
}
1848+
inExport = true
1849+
currentBlock = [line]
1850+
continue
1851+
}
1852+
1853+
if (inExport) {
1854+
currentBlock.push(line)
1855+
if (line.includes('}')) {
1856+
state.dtsLines.push(currentBlock.join('\n'))
1857+
currentBlock = []
1858+
inExport = false
1859+
}
1860+
continue
1861+
}
18111862

18121863
// Process imports
18131864
if (line.includes('import ')) {
@@ -1827,7 +1878,9 @@ function processSourceFile(content: string, state: ProcessingState): void {
18271878
if (trimmedLine.startsWith('export {')) {
18281879
debugLog('mixed-export', `Found mixed export: ${trimmedLine}`)
18291880
processExport(trimmedLine, state)
1830-
state.dtsLines.push(line)
1881+
if (trimmedLine.includes('}')) {
1882+
state.dtsLines.push(line)
1883+
}
18311884
continue
18321885
}
18331886
}

0 commit comments

Comments
 (0)