feat(babel): Auto-inject sentry-label from static text children#925
Open
feat(babel): Auto-inject sentry-label from static text children#925
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit df91d27. Configure here.
Add opt-in `autoInjectSentryLabel` option to the Babel component annotate plugin. When enabled, the plugin extracts static text from JSX children (up to 3 levels deep) and injects a `sentry-label` attribute on the root element. This gives React Native apps meaningful touch breadcrumb labels without manual annotation. Closes getsentry/sentry-react-native#6098 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
0bd679b to
9eaa08f
Compare
Collaborator
|
The regular component injection plugin breaks props in some circumstances which has stopped us from enabling this plugin by default: We've recently added an experimental mode which injects the annotations into the HTML elements inside the component: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Adds an opt-in
autoInjectSentryLabeloption to@sentry/babel-plugin-component-annotatethat automatically extracts static text from JSX children and injects asentry-labelattribute on the root element.This enables React Native apps to get meaningful touch breadcrumb labels (e.g., "Save workout", "Add to cart") without manual annotation. The feature walks the JSX tree up to 3 levels deep, collecting text from recognized text components (default:
Text,text), and bails out entirely if any dynamic content is found anywhere in the subtree.Resolves getsentry/sentry-react-native#6098
Key design decisions
autoInjectSentryLabeldefaults tofalse, no behavior change for existing users. When disabled, the only overhead is two property reads during context creation and one boolean check perprocessJSXcall — none of the new extraction/injection code is reached.{variable},{t('key')}, template literals) anywhere in the subtree causes the entire label to be skipped — including dynamic content nested inside non-text wrapper elements within text components (e.g.,<Text>Hello <Bold>{name}</Bold></Text>). This is stricter than the runtime extraction in sentry-react-native#6106, which skips dynamic nodes and continues. Build-time can't evaluate dynamic content, so skipping entirely avoids partial/misleading labels.sentry-label— React strips unknown hyphenated props from DOM elements, so this is safe for web even though the feature targets React Nativebundler-plugin-core'screateComponentNameAnnotateHooksdoes not pass this option through, so only React Native (which configures the Babel plugin directly via Metro) can activate it. Web SDKs are unaffected.<Text>support — handles the standard RN inline styling pattern (<Text>Hello <Text style={bold}>world</Text></Text>), recursively extracting text from nested text componentsJSXElementchild is used as the injection target. Fragments are treated as transparent wrappers and do not consume depth budget during text search....textComponentNamesoption — configurable list of component names whose children are treated as text contentNew plugin options
autoInjectSentryLabelbooleanfalsesentry-labeltextComponentNamesstring[]["Text", "text"]Test plan
sentry-label.test.tscovering: opt-in behavior, basic extraction, multiple text children, skip conditions, truncation, depth limit, text component names, nested text (RN inline styling), fragment handling (including depth budget), dynamic content bail-out in non-text wrappers, web compatibility, edge cases, multiple components per file, ternary returnstest-plugin.test.ts(65 tests) andexperimental.test.ts(55 tests)🤖 Generated with Claude Code