Skip to content

Commit 57b0fc3

Browse files
committed
feat: handle function body returning a ternary
1 parent 22b9a47 commit 57b0fc3

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

packages/babel-plugin-component-annotate/src/index.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export default function componentNameAnnotatePlugin({ types: t }: typeof Babel):
8484
ArrowFunctionExpression(path, state) {
8585
// We're expecting a `VariableDeclarator` like `const MyComponent =`
8686
const parent = path.parent;
87+
8788
if (
8889
!parent ||
8990
!("id" in parent) ||
@@ -189,6 +190,36 @@ function functionBodyPushAttributes(
189190
return;
190191
}
191192

193+
// Handle the case of a function body returning a ternary operation.
194+
// `return (maybeTrue ? '' : (<SubComponent />))`
195+
if (arg.isConditionalExpression()) {
196+
const consequent = arg.get('consequent');
197+
if (consequent.isJSXFragment() || consequent.isJSXElement()) {
198+
processJSX(
199+
annotateFragments,
200+
t,
201+
consequent,
202+
componentName,
203+
sourceFileName,
204+
attributeNames,
205+
ignoredComponents
206+
);
207+
}
208+
const alternate = arg.get('alternate');
209+
if (alternate.isJSXFragment() || alternate.isJSXElement()) {
210+
processJSX(
211+
annotateFragments,
212+
t,
213+
alternate,
214+
componentName,
215+
sourceFileName,
216+
attributeNames,
217+
ignoredComponents
218+
);
219+
}
220+
return;
221+
}
222+
192223
if (!arg.isJSXFragment() && !arg.isJSXElement()) {
193224
return;
194225
}
@@ -223,7 +254,6 @@ function processJSX(
223254
if (!jsxNode) {
224255
return;
225256
}
226-
227257
// NOTE: I don't know of a case where `openingElement` would have more than one item,
228258
// but it's safer to always iterate
229259
const paths = jsxNode.get("openingElement");

packages/babel-plugin-component-annotate/test/__snapshots__/test-plugin.test.ts.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ class componentName extends Component {
223223
export default componentName;"
224224
`;
225225

226+
exports[`handles ternary operation returned by function body 1`] = `
227+
"const maybeTrue = Math.random() > 0.5;
228+
export default function componentName() {
229+
return maybeTrue ? '' : /*#__PURE__*/React.createElement(SubComponent, {
230+
\\"data-sentry-element\\": \\"SubComponent\\",
231+
\\"data-sentry-component\\": \\"componentName\\"
232+
});
233+
}"
234+
`;
235+
226236
exports[`nonJSX snapshot matches 1`] = `
227237
"import React, { Component } from 'react';
228238
class TestClass extends Component {

packages/babel-plugin-component-annotate/test/test-plugin.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,3 +2236,16 @@ it("Bananas incompatible plugin @react-navigation source snapshot matches", () =
22362236
}"
22372237
`);
22382238
});
2239+
2240+
it("handles ternary operation returned by function body", () => {
2241+
const result = transform(`const maybeTrue = Math.random() > 0.5;
2242+
export default function componentName() {
2243+
return (maybeTrue ? '' : (<SubComponent />))
2244+
}`,
2245+
{
2246+
presets: ["@babel/preset-react"],
2247+
plugins: [[plugin, { "annotate-fragments": true }]],
2248+
}
2249+
);
2250+
expect(result?.code).toMatchSnapshot();
2251+
});

0 commit comments

Comments
 (0)