-
-
Notifications
You must be signed in to change notification settings - Fork 361
Expo Router ErrorBoundary auto wrapped #6347
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d714e2e
Auto wrap Expo Router ErrorBoundary
alwx 805c68a
fix(core): Don't auto-inject component-annotate plugin when only auto…
alwx 8a94fea
refactor(core): Address PR review feedback
alwx e815cd3
fix(core): Wrap every ErrorBoundary re-export, not just the first
alwx f18a2fb
fix(core): Inject helper imports only once per file
alwx 2c1b33c
fix(core): Hoist helper imports to the top of the file
alwx e4d2b88
fix(core): Use a fresh local for the wrapped boundary to avoid bindin…
alwx aa389da
fix(core): Clone reused specifier nodes; lock in 'use client' preserv…
alwx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
124 changes: 124 additions & 0 deletions
124
packages/core/src/js/tools/sentryExpoRouterAutoWrapBabelPlugin.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import type { NodePath, PluginObj, PluginPass, types as BabelTypes } from '@babel/core'; | ||
|
|
||
| /** | ||
| * Babel plugin that auto-wraps Expo Router's per-route `ErrorBoundary` so the | ||
| * Sentry SDK captures render-phase errors that hit the fallback without | ||
| * requiring the user to change their route file. | ||
| * | ||
| * It rewrites: | ||
| * | ||
| * ```ts | ||
| * export { ErrorBoundary } from 'expo-router'; | ||
| * ``` | ||
| * | ||
| * into: | ||
| * | ||
| * ```ts | ||
| * import { ErrorBoundary as __sentryOriginalExpoErrorBoundary } from 'expo-router'; | ||
| * import { wrapExpoRouterErrorBoundary as __sentryWrapExpoRouterErrorBoundary } from '@sentry/react-native'; | ||
| * export const ErrorBoundary = __sentryWrapExpoRouterErrorBoundary(__sentryOriginalExpoErrorBoundary); | ||
| * ``` | ||
| * | ||
| * Aliased re-exports (`export { ErrorBoundary as Foo } from 'expo-router'`) | ||
| * are preserved — the wrapped export keeps the user-chosen name. Mixed | ||
| * re-exports such as | ||
| * `export { ErrorBoundary, Stack } from 'expo-router'` keep the non-boundary | ||
| * specifiers in place. | ||
| * | ||
| * The transform is structurally idempotent: after the rewrite the re-export | ||
| * is no longer an `export ... from 'expo-router'`, so a second pass over the | ||
| * same file finds nothing to transform. | ||
| * | ||
| * Files inside `node_modules` are never transformed. | ||
| */ | ||
|
|
||
| const ORIGINAL_BOUNDARY_LOCAL = '__sentryOriginalExpoErrorBoundary'; | ||
| const WRAP_FN_LOCAL = '__sentryWrapExpoRouterErrorBoundary'; | ||
| const SENTRY_PACKAGE = '@sentry/react-native'; | ||
| const EXPO_ROUTER_PACKAGE = 'expo-router'; | ||
| const BOUNDARY_EXPORT = 'ErrorBoundary'; | ||
|
|
||
| interface BabelApi { | ||
| types: typeof BabelTypes; | ||
| } | ||
|
|
||
| export default function sentryExpoRouterAutoWrapBabelPlugin({ types: t }: BabelApi): PluginObj { | ||
| return { | ||
| name: 'sentry-expo-router-auto-wrap-error-boundary', | ||
| visitor: { | ||
| ExportNamedDeclaration(path: NodePath<BabelTypes.ExportNamedDeclaration>, state: PluginPass) { | ||
| const node = path.node; | ||
| if (!node.source || node.source.value !== EXPO_ROUTER_PACKAGE) { | ||
| return; | ||
| } | ||
|
|
||
| const filename = (state.file?.opts?.filename as string | undefined) ?? ''; | ||
| if (filename.includes('node_modules')) { | ||
| return; | ||
| } | ||
|
|
||
| const boundarySpecifierIndex = node.specifiers.findIndex( | ||
| s => t.isExportSpecifier(s) && t.isIdentifier(s.local) && s.local.name === BOUNDARY_EXPORT, | ||
| ); | ||
| if (boundarySpecifierIndex === -1) { | ||
| return; | ||
| } | ||
|
|
||
| const boundarySpecifier = node.specifiers[boundarySpecifierIndex] as BabelTypes.ExportSpecifier; | ||
| const exportedName = t.isIdentifier(boundarySpecifier.exported) | ||
| ? boundarySpecifier.exported.name | ||
| : boundarySpecifier.exported.value; | ||
|
|
||
| // Hoist the two helper imports to the top of the Program body so | ||
| // they sit alongside the file's other `import` declarations rather | ||
| // than landing mid-file. Some toolchains (e.g. Hermes) are strict | ||
| // about import placement, and mid-file imports are also harder to | ||
| // read. Inject once per file so a second wrap reuses the bindings. | ||
| const HELPERS_KEY = 'sentryAutoWrapHelpersInjected'; | ||
| if (state.get(HELPERS_KEY) !== true) { | ||
| const program = path.scope.getProgramParent().path as NodePath<BabelTypes.Program>; | ||
| program.unshiftContainer('body', [ | ||
| t.importDeclaration( | ||
| [t.importSpecifier(t.identifier(ORIGINAL_BOUNDARY_LOCAL), t.identifier(BOUNDARY_EXPORT))], | ||
| t.stringLiteral(EXPO_ROUTER_PACKAGE), | ||
| ), | ||
| t.importDeclaration( | ||
| [t.importSpecifier(t.identifier(WRAP_FN_LOCAL), t.identifier('wrapExpoRouterErrorBoundary'))], | ||
| t.stringLiteral(SENTRY_PACKAGE), | ||
| ), | ||
| ]); | ||
|
alwx marked this conversation as resolved.
|
||
| state.set(HELPERS_KEY, true); | ||
| } | ||
|
alwx marked this conversation as resolved.
|
||
|
|
||
| // Generate a unique local binding for the wrapped boundary instead of | ||
| // declaring `const <exportedName> = ...` directly. That avoids clashing | ||
| // with an existing top-level binding of the same name in the file | ||
| // (e.g. `import { ErrorBoundary } from 'expo-router'` used elsewhere) | ||
| // which would otherwise produce a duplicate-binding compile error. | ||
| const wrappedLocal = path.scope.generateUidIdentifier(`wrapped${exportedName}`); | ||
| const replacements: BabelTypes.Statement[] = [ | ||
| t.variableDeclaration('const', [ | ||
| t.variableDeclarator( | ||
| wrappedLocal, | ||
| t.callExpression(t.identifier(WRAP_FN_LOCAL), [t.identifier(ORIGINAL_BOUNDARY_LOCAL)]), | ||
| ), | ||
| ]), | ||
| t.exportNamedDeclaration(null, [t.exportSpecifier(t.cloneNode(wrappedLocal), t.identifier(exportedName))]), | ||
| ]; | ||
|
|
||
| const remainingSpecifiers = node.specifiers | ||
| .filter((_, i) => i !== boundarySpecifierIndex) | ||
| // Clone the reused specifier nodes so we don't share AST nodes | ||
| // between the original `path.node` (about to be replaced) and the | ||
| // new export declaration — Babel docs warn that reusing nodes can | ||
| // corrupt the scope cache. | ||
| .map(s => t.cloneNode(s)); | ||
| if (remainingSpecifiers.length > 0) { | ||
| replacements.push(t.exportNamedDeclaration(null, remainingSpecifiers, t.cloneNode(node.source))); | ||
| } | ||
|
|
||
| path.replaceWithMultiple(replacements); | ||
|
alwx marked this conversation as resolved.
cursor[bot] marked this conversation as resolved.
|
||
| }, | ||
| }, | ||
| }; | ||
| } | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.