1
1
import _ from 'lodash'
2
2
import path from 'path'
3
- import { defaultHandlers , parse } from 'react-docgen'
3
+ import { defaultHandlers , parse , resolver } from 'react-docgen'
4
4
import fs from 'fs'
5
5
6
6
import { parseDefaultValue , parseDocblock , parserCustomHandler , parseType } from './'
@@ -19,7 +19,22 @@ const getComponentInfo = (filepath) => {
19
19
const componentType = path . basename ( path . dirname ( dir ) ) . replace ( / s $ / , '' )
20
20
21
21
// start with react-docgen info
22
- const info = parse ( contents , null , [ ...defaultHandlers , parserCustomHandler ] )
22
+ const components = parse ( contents , resolver . findAllComponentDefinitions , [
23
+ ...defaultHandlers ,
24
+ parserCustomHandler ,
25
+ ] )
26
+ if ( ! components . length ) {
27
+ throw new Error ( `Could not find a component definition in "${ filepath } ".` )
28
+ }
29
+ if ( components . length > 1 ) {
30
+ throw new Error (
31
+ [
32
+ `Found more than one component definition in "${ filepath } ".` ,
33
+ 'This is currently not supported, please ensure your module only defines a single React component.' ,
34
+ ] . join ( ' ' ) ,
35
+ )
36
+ }
37
+ const info = components [ 0 ]
23
38
24
39
// remove keys we don't use
25
40
delete info . methods
@@ -40,11 +55,13 @@ const getComponentInfo = (filepath) => {
40
55
? null
41
56
: info . displayName . replace ( info . parentDisplayName , '' )
42
57
58
+ // "ListItem.js" is a subcomponent is the "List" directory
59
+ const subcomponentRegExp = new RegExp ( `^${ dirname } \\w+\\.js$` )
60
+
43
61
info . subcomponents = info . isParent
44
62
? fs
45
63
. readdirSync ( dir )
46
- . filter ( file => / ^ (? ! i n d e x ) .* \. j s $ / . test ( file ) )
47
- . filter ( file => dirname !== path . basename ( file , path . extname ( file ) ) )
64
+ . filter ( file => subcomponentRegExp . test ( file ) )
48
65
. map ( file => path . basename ( file , path . extname ( file ) ) )
49
66
: null
50
67
0 commit comments