1 1
import * as bt from '@babel/types'
2
import { NodePath } from 'ast-types/lib/node-path'
3
import Documentation from '../Documentation'
4
import { ParseOptions } from '../parse'
5 1
import resolveRequired from '../utils/resolveRequired'
6 1
import documentRequiredComponents from '../utils/documentRequiredComponents'
7

8
/**
9
 * Returns documentation of the component referenced in the extends property of the component
10
 * @param {NodePath} astPath
11
 * @param {Array<NodePath>} componentDefinitions
12
 * @param {string} originalFilePath
13
 */
14 1
export default async function extendsHandler(
15
	documentation: Documentation,
16
	componentDefinition: NodePath,
17
	astPath: bt.File,
18 1
	opt: ParseOptions
19
) {
20 1
	const extendsVariableName = getExtendsVariableName(componentDefinition)
21

22
	// if there is no extends or extends is a direct require
23 1
	if (!extendsVariableName) {
24 1
		return
25
	}
26

27
	// get all require / import statements
28 1
	const extendsFilePath = resolveRequired(astPath, [extendsVariableName])
29

30 1
	await documentRequiredComponents(documentation, extendsFilePath, 'extends', opt)
31
}
32

33 1
function getExtendsVariableName(compDef: NodePath): string | undefined {
34
	const extendsVariable: NodePath | undefined =
35 1
		compDef &&
36 1
		bt.isClassDeclaration(compDef.node) &&
37 1
		compDef.node.superClass &&
38 1
		bt.isIdentifier(compDef.node.superClass)
39 1
			? (compDef.get('superClass') as NodePath<bt.Identifier>)
40 1
			: getExtendsVariableNameFromCompDef(compDef)
41

42 1
	if (extendsVariable) {
43 1
		const extendsValue = bt.isProperty(extendsVariable.node)
44 1
			? extendsVariable.node.value
45 1
			: extendsVariable.node
46 1
		return extendsValue && bt.isIdentifier(extendsValue) ? extendsValue.name : undefined
47
	}
48 1
	return undefined
49
}
50

51 1
function getExtendsVariableNameFromCompDef(compDef: NodePath): NodePath | undefined {
52 1
	if (!compDef) {
53 0
		return undefined
54
	}
55 1
	const compDefProperties = compDef.get('properties')
56 1
	const pathExtends = compDefProperties.value
57 1
		? compDefProperties.filter(
58 1
				(p: NodePath<bt.Property>) => bt.isIdentifier(p.node.key) && p.node.key.name === 'extends'
59
		  )
60 1
		: []
61 1
	return pathExtends.length ? pathExtends[0] : undefined
62
}

Read our documentation on viewing source code .

Loading