Skip to content

Commit d7a39af

Browse files
authored
Refactor resolveComponentDefinition into findComponentDefinition (#719)
1 parent ff1fc07 commit d7a39af

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

Diff for: .changeset/five-trainers-report.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'react-docgen': major
3+
---
4+
5+
Refactored `resolveComponentDefinition` utility.
6+
7+
- Renamed to `findComponentDefinition`
8+
- Removed named export `isComponentDefinition`
9+
- The utility now does a lot more than previously, check out the commit to see
10+
the changes in detail.

Diff for: packages/react-docgen/src/resolver/FindExportedDefinitionsResolver.ts

+14-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import isExportsOrModuleAssignment from '../utils/isExportsOrModuleAssignment.js';
22
import resolveExportDeclaration from '../utils/resolveExportDeclaration.js';
33
import resolveToValue from '../utils/resolveToValue.js';
4-
import resolveHOC from '../utils/resolveHOC.js';
54
import type { NodePath } from '@babel/traverse';
65
import { visitors } from '@babel/traverse';
76
import { shallowIgnoreVisitors } from '../utils/traverse.js';
@@ -12,9 +11,7 @@ import type {
1211
} from '@babel/types';
1312
import type FileState from '../FileState.js';
1413
import type { ComponentNodePath, ResolverClass } from './index.js';
15-
import resolveComponentDefinition, {
16-
isComponentDefinition,
17-
} from '../utils/resolveComponentDefinition.js';
14+
import findComponentDefinition from '../utils/findComponentDefinition.js';
1815
import { ERROR_CODES, ReactDocgenError } from '../error.js';
1916

2017
interface TraverseState {
@@ -26,24 +23,16 @@ function exportDeclaration(
2623
path: NodePath<ExportDefaultDeclaration | ExportNamedDeclaration>,
2724
state: TraverseState,
2825
): void {
29-
resolveExportDeclaration(path).forEach(definition => {
30-
if (!isComponentDefinition(definition)) {
31-
definition = resolveToValue(resolveHOC(definition));
26+
resolveExportDeclaration(path).forEach(exportedPath => {
27+
const definition = findComponentDefinition(exportedPath);
3228

33-
if (!isComponentDefinition(definition)) {
34-
return;
29+
if (definition) {
30+
if (state.limit > 0 && state.foundDefinitions.size > 0) {
31+
// If a file exports multiple components, ... complain!
32+
throw new ReactDocgenError(ERROR_CODES.MULTIPLE_DEFINITIONS);
3533
}
36-
}
37-
38-
if (state.limit > 0 && state.foundDefinitions.size > 0) {
39-
// If a file exports multiple components, ... complain!
40-
throw new ReactDocgenError(ERROR_CODES.MULTIPLE_DEFINITIONS);
41-
}
4234

43-
const resolved = resolveComponentDefinition(definition);
44-
45-
if (resolved) {
46-
state.foundDefinitions.add(resolved);
35+
state.foundDefinitions.add(definition);
4736
}
4837
});
4938

@@ -61,22 +50,15 @@ function assignmentExpressionVisitor(
6150
}
6251
// Resolve the value of the right hand side. It should resolve to a call
6352
// expression, something like React.createClass
64-
let resolvedPath = resolveToValue(path.get('right'));
53+
const resolvedPath = resolveToValue(path.get('right'));
54+
const definition = findComponentDefinition(resolvedPath);
6555

66-
if (!isComponentDefinition(resolvedPath)) {
67-
resolvedPath = resolveToValue(resolveHOC(resolvedPath));
68-
if (!isComponentDefinition(resolvedPath)) {
69-
return path.skip();
56+
if (definition) {
57+
if (state.limit > 0 && state.foundDefinitions.size > 0) {
58+
// If a file exports multiple components, ... complain!
59+
throw new ReactDocgenError(ERROR_CODES.MULTIPLE_DEFINITIONS);
7060
}
71-
}
72-
if (state.limit > 0 && state.foundDefinitions.size > 0) {
73-
// If a file exports multiple components, ... complain!
74-
throw new ReactDocgenError(ERROR_CODES.MULTIPLE_DEFINITIONS);
75-
}
76-
77-
const definition = resolveComponentDefinition(resolvedPath);
7861

79-
if (definition) {
8062
state.foundDefinitions.add(definition);
8163
}
8264

Diff for: packages/react-docgen/src/utils/resolveComponentDefinition.ts renamed to packages/react-docgen/src/utils/findComponentDefinition.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import isReactCreateClassCall from './isReactCreateClassCall.js';
55
import isReactForwardRefCall from './isReactForwardRefCall.js';
66
import isStatelessComponent from './isStatelessComponent.js';
77
import normalizeClassDefinition from './normalizeClassDefinition.js';
8+
import resolveHOC from './resolveHOC.js';
89
import resolveToValue from './resolveToValue.js';
910

10-
export function isComponentDefinition(
11+
function isComponentDefinition(
1112
path: NodePath,
1213
): path is NodePath<ComponentNode> {
1314
return (
@@ -18,7 +19,7 @@ export function isComponentDefinition(
1819
);
1920
}
2021

21-
export default function resolveComponentDefinition(
22+
function resolveComponentDefinition(
2223
definition: NodePath<ComponentNode>,
2324
): NodePath<ComponentNode> | null {
2425
if (isReactCreateClassCall(definition)) {
@@ -41,3 +42,18 @@ export default function resolveComponentDefinition(
4142

4243
return null;
4344
}
45+
46+
export default function findComponentDefinition(
47+
path: NodePath,
48+
): NodePath<ComponentNode> | null {
49+
let resolvedPath = path;
50+
51+
if (!isComponentDefinition(resolvedPath)) {
52+
resolvedPath = resolveToValue(resolveHOC(resolvedPath));
53+
if (!isComponentDefinition(resolvedPath)) {
54+
return null;
55+
}
56+
}
57+
58+
return resolveComponentDefinition(resolvedPath);
59+
}

0 commit comments

Comments
 (0)