Skip to content

Commit 8a26279

Browse files
authored
chore(getComponentInfo): handle HOC default exports (Semantic-Org#2883)
* chore(getComponentInfo): handle HOC default exports * fix(getComponentInfo): subcomponents start with dirname
1 parent 6d6f6eb commit 8a26279

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

gulp/plugins/util/getComponentInfo.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _ from 'lodash'
22
import path from 'path'
3-
import { defaultHandlers, parse } from 'react-docgen'
3+
import { defaultHandlers, parse, resolver } from 'react-docgen'
44
import fs from 'fs'
55

66
import { parseDefaultValue, parseDocblock, parserCustomHandler, parseType } from './'
@@ -19,7 +19,22 @@ const getComponentInfo = (filepath) => {
1919
const componentType = path.basename(path.dirname(dir)).replace(/s$/, '')
2020

2121
// 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]
2338

2439
// remove keys we don't use
2540
delete info.methods
@@ -40,11 +55,13 @@ const getComponentInfo = (filepath) => {
4055
? null
4156
: info.displayName.replace(info.parentDisplayName, '')
4257

58+
// "ListItem.js" is a subcomponent is the "List" directory
59+
const subcomponentRegExp = new RegExp(`^${dirname}\\w+\\.js$`)
60+
4361
info.subcomponents = info.isParent
4462
? fs
4563
.readdirSync(dir)
46-
.filter(file => /^(?!index).*\.js$/.test(file))
47-
.filter(file => dirname !== path.basename(file, path.extname(file)))
64+
.filter(file => subcomponentRegExp.test(file))
4865
.map(file => path.basename(file, path.extname(file)))
4966
: null
5067

0 commit comments

Comments
 (0)