Skip to content

Commit ebee4f4

Browse files
antonisclaude
andcommitted
fix(babel): Handle JSXFragment children inside text components
Fragments inside text components (e.g. <Text>Hello <>World</> more</Text>) were silently dropped, producing partial labels. Now extractTextFromTextComponent recurses into fragment children the same way extractStaticTextFromChildren does. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7f79364 commit ebee4f4

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ function extractStaticTextFromChildren(
755755
*/
756756
function extractTextFromTextComponent(
757757
t: typeof Babel.types,
758-
node: Babel.types.JSXElement,
758+
node: Babel.types.JSXElement | Babel.types.JSXFragment,
759759
textComponentNames: string[]
760760
): string[] | null {
761761
const texts: string[] = [];
@@ -779,6 +779,12 @@ function extractTextFromTextComponent(
779779
}
780780
texts.push(...innerTexts);
781781
}
782+
} else if (t.isJSXFragment(child)) {
783+
const innerTexts = extractTextFromTextComponent(t, child, textComponentNames);
784+
if (innerTexts === null) {
785+
return null;
786+
}
787+
texts.push(...innerTexts);
782788
} else if (t.isJSXSpreadChild(child)) {
783789
return null;
784790
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,22 @@ describe("autoInjectSentryLabel", () => {
533533
expect(result?.code).toContain('"sentry-label": "Hello world"');
534534
});
535535

536+
it("extracts text from fragment children inside Text", () => {
537+
const result = transformWith(`
538+
import React from 'react';
539+
import { Text, View } from 'react-native';
540+
541+
export default function MyComponent() {
542+
return (
543+
<View>
544+
<Text>Hello <>World</> more</Text>
545+
</View>
546+
);
547+
}
548+
`);
549+
expect(result?.code).toContain('"sentry-label": "Hello World more"');
550+
});
551+
536552
it("handles Text wrapping only a non-text element", () => {
537553
const result = transformWith(`
538554
import React from 'react';

0 commit comments

Comments
 (0)