Skip to content

Commit

Permalink
Refactor resolveComponentDefinition into findComponentDefinition (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
danez authored Jan 3, 2023
1 parent ff1fc07 commit d7a39af
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
10 changes: 10 additions & 0 deletions .changeset/five-trainers-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'react-docgen': major
---

Refactored `resolveComponentDefinition` utility.

- Renamed to `findComponentDefinition`
- Removed named export `isComponentDefinition`
- The utility now does a lot more than previously, check out the commit to see
the changes in detail.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import isExportsOrModuleAssignment from '../utils/isExportsOrModuleAssignment.js';
import resolveExportDeclaration from '../utils/resolveExportDeclaration.js';
import resolveToValue from '../utils/resolveToValue.js';
import resolveHOC from '../utils/resolveHOC.js';
import type { NodePath } from '@babel/traverse';
import { visitors } from '@babel/traverse';
import { shallowIgnoreVisitors } from '../utils/traverse.js';
Expand All @@ -12,9 +11,7 @@ import type {
} from '@babel/types';
import type FileState from '../FileState.js';
import type { ComponentNodePath, ResolverClass } from './index.js';
import resolveComponentDefinition, {
isComponentDefinition,
} from '../utils/resolveComponentDefinition.js';
import findComponentDefinition from '../utils/findComponentDefinition.js';
import { ERROR_CODES, ReactDocgenError } from '../error.js';

interface TraverseState {
Expand All @@ -26,24 +23,16 @@ function exportDeclaration(
path: NodePath<ExportDefaultDeclaration | ExportNamedDeclaration>,
state: TraverseState,
): void {
resolveExportDeclaration(path).forEach(definition => {
if (!isComponentDefinition(definition)) {
definition = resolveToValue(resolveHOC(definition));
resolveExportDeclaration(path).forEach(exportedPath => {
const definition = findComponentDefinition(exportedPath);

if (!isComponentDefinition(definition)) {
return;
if (definition) {
if (state.limit > 0 && state.foundDefinitions.size > 0) {
// If a file exports multiple components, ... complain!
throw new ReactDocgenError(ERROR_CODES.MULTIPLE_DEFINITIONS);
}
}

if (state.limit > 0 && state.foundDefinitions.size > 0) {
// If a file exports multiple components, ... complain!
throw new ReactDocgenError(ERROR_CODES.MULTIPLE_DEFINITIONS);
}

const resolved = resolveComponentDefinition(definition);

if (resolved) {
state.foundDefinitions.add(resolved);
state.foundDefinitions.add(definition);
}
});

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

if (!isComponentDefinition(resolvedPath)) {
resolvedPath = resolveToValue(resolveHOC(resolvedPath));
if (!isComponentDefinition(resolvedPath)) {
return path.skip();
if (definition) {
if (state.limit > 0 && state.foundDefinitions.size > 0) {
// If a file exports multiple components, ... complain!
throw new ReactDocgenError(ERROR_CODES.MULTIPLE_DEFINITIONS);
}
}
if (state.limit > 0 && state.foundDefinitions.size > 0) {
// If a file exports multiple components, ... complain!
throw new ReactDocgenError(ERROR_CODES.MULTIPLE_DEFINITIONS);
}

const definition = resolveComponentDefinition(resolvedPath);

if (definition) {
state.foundDefinitions.add(definition);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import isReactCreateClassCall from './isReactCreateClassCall.js';
import isReactForwardRefCall from './isReactForwardRefCall.js';
import isStatelessComponent from './isStatelessComponent.js';
import normalizeClassDefinition from './normalizeClassDefinition.js';
import resolveHOC from './resolveHOC.js';
import resolveToValue from './resolveToValue.js';

export function isComponentDefinition(
function isComponentDefinition(
path: NodePath,
): path is NodePath<ComponentNode> {
return (
Expand All @@ -18,7 +19,7 @@ export function isComponentDefinition(
);
}

export default function resolveComponentDefinition(
function resolveComponentDefinition(
definition: NodePath<ComponentNode>,
): NodePath<ComponentNode> | null {
if (isReactCreateClassCall(definition)) {
Expand All @@ -41,3 +42,18 @@ export default function resolveComponentDefinition(

return null;
}

export default function findComponentDefinition(
path: NodePath,
): NodePath<ComponentNode> | null {
let resolvedPath = path;

if (!isComponentDefinition(resolvedPath)) {
resolvedPath = resolveToValue(resolveHOC(resolvedPath));
if (!isComponentDefinition(resolvedPath)) {
return null;
}
}

return resolveComponentDefinition(resolvedPath);
}

0 comments on commit d7a39af

Please sign in to comment.