Skip to content

Fix detection of ESM imports #858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/young-carrots-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-docgen': patch
---

Fix detection of React.Children with ESM imports
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,42 @@ describe('isReactChildrenElementCall', () => {

expect(isReactChildrenElementCall(def)).toBe(true);
});

test('React.Children.only without React', () => {
const def = parse.expressionLast(`
var Children = require("React").Children;
Children.only(() => {});
`);

expect(isReactChildrenElementCall(def)).toBe(true);
});

test('React.Children.only with destructuring', () => {
const def = parse.expressionLast(`
var { Children } = require("React");
Children.only(() => {});
`);

expect(isReactChildrenElementCall(def)).toBe(true);
});

test('React.Children.only with import', () => {
const def = parse.expressionLast(`
import React from 'react';
React.Children.only(() => {});
`);

expect(isReactChildrenElementCall(def)).toBe(true);
});

test('React.Children.only with named import', () => {
const def = parse.expressionLast(`
import { Children } from 'react';
Children.only(() => {});
`);

expect(isReactChildrenElementCall(def)).toBe(true);
});
});
describe('false', () => {
test('not call expression', () => {
Expand Down
29 changes: 10 additions & 19 deletions packages/react-docgen/src/utils/isReactChildrenElementCall.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { NodePath } from '@babel/traverse';
import isReactModuleName from './isReactModuleName.js';
import resolveToModule from './resolveToModule.js';
import type { CallExpression } from '@babel/types';
import isReactBuiltinReference from './isReactBuiltinReference.js';

/**
* Returns true if the expression is a function call of the form
Expand All @@ -16,24 +15,16 @@ export default function isReactChildrenElementCall(

const callee = path.get('callee');

if (
!callee.isMemberExpression() ||
(!callee.get('property').isIdentifier({ name: 'only' }) &&
!callee.get('property').isIdentifier({ name: 'map' }))
) {
return false;
}

const calleeObj = callee.get('object');
if (callee.isMemberExpression()) {
const calleeProperty = callee.get('property');

if (
!calleeObj.isMemberExpression() ||
!calleeObj.get('property').isIdentifier({ name: 'Children' })
) {
return false;
if (
calleeProperty.isIdentifier({ name: 'only' }) ||
calleeProperty.isIdentifier({ name: 'map' })
) {
return isReactBuiltinReference(callee.get('object'), 'Children');
}
}

const module = resolveToModule(calleeObj);

return Boolean(module && isReactModuleName(module));
return false;
}