This repository was archived by the owner on Jan 24, 2025. It is now read-only.
File tree 2 files changed +44
-0
lines changed
2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ import * as parser from '@babel/parser'
2
+ import traverse from '@babel/traverse'
3
+ import get from 'lodash/get'
4
+
5
+ const fromDeclarations = ( declarations : any = [ ] ) =>
6
+ Array . isArray ( declarations ) && declarations . length > 0
7
+ ? declarations . map ( declaration => get ( declaration , 'id.name' ) )
8
+ : [ ]
9
+
10
+ const traverseOnExports = ( fn : ( path : any ) => any [ ] ) => ( node : any ) => {
11
+ try {
12
+ const ast = parser . parse ( node . value , {
13
+ sourceType : 'module' ,
14
+ } )
15
+ let populated : any [ ] = [ ]
16
+ traverse ( ast , {
17
+ enter ( path : any ) : void {
18
+ if ( path . isExportDeclaration ( ) ) {
19
+ populated = populated . concat ( fn ( path ) )
20
+ return
21
+ }
22
+ } ,
23
+ } )
24
+ return populated
25
+ } catch ( err ) {
26
+ return [ ]
27
+ }
28
+ }
29
+
30
+ export const getExportsVariables = traverseOnExports ( path => {
31
+ const type = get ( path , 'node.declaration.type' )
32
+ switch ( type ) {
33
+ case 'VariableDeclaration' :
34
+ return fromDeclarations ( get ( path , 'node.declaration.declarations' , [ ] ) )
35
+ case 'FunctionDeclaration' :
36
+ const declaration = get ( path , 'node.declaration' , false )
37
+ return fromDeclarations ( declaration ? [ declaration ] : [ ] )
38
+ case 'Identifier' :
39
+ return get ( path , 'node.declaration.name' )
40
+ default :
41
+ console . error ( `Unexpected export type ${ type } in docz-utils/exports` )
42
+ }
43
+ } )
Original file line number Diff line number Diff line change 1
1
export * from './ast'
2
2
export * from './fs'
3
3
export * from './imports'
4
+ export * from './exports'
4
5
export * from './jsx'
5
6
export * from './mdast'
6
7
export { format } from './format'
You can’t perform that action at this time.
0 commit comments