Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Functional components returning conditionals fix #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ function doesReturnJSX (body) {
var lastBlock = block.slice(0).pop()

if (lastBlock.type === 'ReturnStatement') {
return lastBlock.argument !== null && lastBlock.argument.type === 'JSXElement'
return lastBlock.argument !== null && (lastBlock.argument.type === 'JSXElement' ||
(lastBlock.argument.type === 'ConditionalExpression' &&
(lastBlock.argument.consequent.type === 'JSXElement' || lastBlock.argument.alternate.type === 'JSXElement')))
}
}

Expand Down
12 changes: 11 additions & 1 deletion test/fixtures/functionExpr/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ export default function Component1f(value) {
value
);
}
Component1f.displayName = "Component1f";

Component1f.displayName = "Component1f";
// Exported named stateless component used in variable declaration returning conditional JSX
export var Component1g = function (value) {
return true ? React.createElement(
"div",
null,
value
) : null;
};
Component1g.displayName = "Component1g";
5 changes: 5 additions & 0 deletions test/fixtures/functionExpr/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ Component1e = function (value) {
export default function Component1f (value) {
return <div>{value}</div>
}

// Exported named stateless component used in variable declaration returning conditional JSX
export var Component1g = function (value) {
return true ? <div>{value}</div> : null
}