Skip to content

Commit

Permalink
feat: handle multiple declarations by outputting twice
Browse files Browse the repository at this point in the history
Co-authored-by: Braden Wiggins <[email protected]>
  • Loading branch information
ghostdevv and braebo committed Sep 17, 2024
1 parent 02aa1c3 commit 072f83f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-buses-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'extractinator': minor
---

feat: handle multiple declarations by outputting twice
39 changes: 17 additions & 22 deletions src/files/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,24 @@ export function parseTSFile({

//? Loop over all the export declarations
for (const [name, declarations] of file.getExportedDeclarations()) {
// todo handle this
if (declarations.length > 1) {
throw new Error(
`Multiple declarations are not handled yet, please file issue "${file_name}"`,
)
}

const [declaration] = declarations
for (const declaration of declarations) {
const tsdoc_node_parent = declaration.getFirstChildIfKind(ts.SyntaxKind.JSDoc)
? //? Check whether the declaration has a comment
declaration
: //? Find the comment by traversing up the parent, this should be safe here
declaration
.getAncestors()
.find((ancestor) => !!ancestor.getFirstChildIfKind(ts.SyntaxKind.JSDoc))

const tsdoc_node_parent = declaration.getFirstChildIfKind(ts.SyntaxKind.JSDoc)
? //? Check whether the declaration has a comment
declaration
: //? Find the comment by traversing up the parent, this should be safe here
declaration
.getAncestors()
.find((ancestor) => !!ancestor.getFirstChildIfKind(ts.SyntaxKind.JSDoc))

export_bits.push({
name,
type: getType(declaration),
isDefaultExport: name == 'default',
comment: tsdoc_node_parent ? parseCommentFromNode(tsdoc_node_parent, tsdoc) : undefined,
})
export_bits.push({
name,
type: getType(declaration),
isDefaultExport: name == 'default',
comment: tsdoc_node_parent
? parseCommentFromNode(tsdoc_node_parent, tsdoc)
: undefined,
})
}
}

return {
Expand Down
27 changes: 27 additions & 0 deletions tests/test-files/multiple-declarations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Adds two or three numbers together.
* @param a The first number to add.
* @param b The second number to add.
* @returns The sum of the two numbers.
*/
export function add(a: number, b: number): number

/**
* Adds two or three numbers together.
* @param a The first number to add.
* @param b The second number to add.
* @param c The third number to add.
* @returns The sum of the three numbers.
*/
export function add(a: number, b: number, c: number): number

/**
* Adds two or three numbers together.
* @param a The first number to add.
* @param b The second number to add.
* @param c Optional third number to add.
* @returns The sum of the input numbers.
*/
export function add(a: number, b: number, c?: number): number {
return a + b + (c ?? 0)
}

0 comments on commit 072f83f

Please sign in to comment.