1 1
import * as bt from '@babel/types'
2
import { NodePath } from 'ast-types/lib/node-path'
3
import Documentation from '../Documentation'
4 1
import getArgFromDecorator from '../utils/getArgFromDecorator'
5 1
import getProperties from './utils/getProperties'
6

7
/**
8
 * Extracts the name of the component from a class-style component
9
 * @param documentation
10
 * @param path
11
 */
12 1
export default function classDisplayNameHandler(
13
	documentation: Documentation,
14 1
	path: NodePath
15
): Promise<void> {
16 1
	if (bt.isClassDeclaration(path.node)) {
17 1
		const config = getArgFromDecorator(path.get('decorators') as NodePath<bt.Decorator>)
18

19
		let displayName: string | undefined
20 1
		if (config && bt.isObjectExpression(config.node)) {
21 1
			getProperties(config, 'name').forEach((p: NodePath<bt.ObjectProperty>) => {
22 1
				const valuePath = p.get('value')
23 1
				if (bt.isStringLiteral(valuePath.node)) {
24 1
					displayName = valuePath.node.value
25
				}
26
			})
27
		} else {
28 1
			displayName = path.node.id ? path.node.id.name : undefined
29
		}
30

31 1
		if (displayName) {
32 1
			documentation.set('displayName', displayName)
33
		}
34
	}
35 1
	return Promise.resolve()
36
}

Read our documentation on viewing source code .

Loading