Skip to content

Commit e1975d4

Browse files
authored
feat(react-component-annotate): Allow skipping annotations on specified components (#617)
1 parent d051b07 commit e1975d4

File tree

9 files changed

+52
-1101
lines changed

9 files changed

+52
-1101
lines changed

packages/babel-plugin-component-annotate/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ Using pnpm:
4747
pnpm add @sentry/babel-plugin-component-annotate --save-dev
4848
```
4949

50+
## Options
51+
52+
### `ignoredComponents`
53+
54+
Type: `string[]`
55+
56+
A list of strings representing the names of components to ignore. The plugin will not apply `data-sentry` annotations on the DOM element for these components.
57+
5058
## Example
5159

5260
```js
@@ -57,7 +65,8 @@ pnpm add @sentry/babel-plugin-component-annotate --save-dev
5765

5866
plugins: [
5967
// Put this plugin before any other plugins you have that transform JSX code
60-
['@sentry/babel-plugin-component-annotate']
68+
// The options are set by providing an object as the second element in the array, but not required
69+
['@sentry/babel-plugin-component-annotate', {ignoredComponents: ['Foo', 'Bar']}]
6170
],
6271
}
6372
```

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@ const nativeSourceFileName = "dataSentrySourceFile";
4848
interface AnnotationOpts {
4949
native?: boolean;
5050
"annotate-fragments"?: boolean;
51-
ignoreComponents?: IgnoredComponent[];
51+
ignoredComponents?: string[];
5252
}
5353

5454
interface AnnotationPluginPass extends PluginPass {
5555
opts: AnnotationOpts;
5656
}
5757

58-
type IgnoredComponent = [file: string, component: string, element: string];
59-
6058
type AnnotationPlugin = PluginObj<AnnotationPluginPass>;
6159

6260
// We must export the plugin as default, otherwise the Babel loader will not be able to resolve it when configured using its string identifier
@@ -78,7 +76,7 @@ export default function componentNameAnnotatePlugin({ types: t }: typeof Babel):
7876
path.node.id.name,
7977
sourceFileNameFromState(state),
8078
attributeNamesFromState(state),
81-
state.opts.ignoreComponents ?? []
79+
state.opts.ignoredComponents ?? []
8280
);
8381
},
8482
ArrowFunctionExpression(path, state) {
@@ -106,7 +104,7 @@ export default function componentNameAnnotatePlugin({ types: t }: typeof Babel):
106104
parent.id.name,
107105
sourceFileNameFromState(state),
108106
attributeNamesFromState(state),
109-
state.opts.ignoreComponents ?? []
107+
state.opts.ignoredComponents ?? []
110108
);
111109
},
112110
ClassDeclaration(path, state) {
@@ -120,7 +118,7 @@ export default function componentNameAnnotatePlugin({ types: t }: typeof Babel):
120118
return;
121119
}
122120

123-
const ignoredComponents = state.opts.ignoreComponents ?? [];
121+
const ignoredComponents = state.opts.ignoredComponents ?? [];
124122

125123
render.traverse({
126124
ReturnStatement(returnStatement) {
@@ -153,7 +151,7 @@ function functionBodyPushAttributes(
153151
componentName: string,
154152
sourceFileName: string | undefined,
155153
attributeNames: string[],
156-
ignoredComponents: IgnoredComponent[]
154+
ignoredComponents: string[]
157155
) {
158156
let jsxNode: Babel.NodePath;
159157

@@ -249,7 +247,7 @@ function processJSX(
249247
componentName: string | null,
250248
sourceFileName: string | undefined,
251249
attributeNames: string[],
252-
ignoredComponents: IgnoredComponent[]
250+
ignoredComponents: string[]
253251
) {
254252
if (!jsxNode) {
255253
return;
@@ -324,7 +322,7 @@ function applyAttributes(
324322
componentName: string | null,
325323
sourceFileName: string | undefined,
326324
attributeNames: string[],
327-
ignoredComponents: IgnoredComponent[]
325+
ignoredComponents: string[]
328326
) {
329327
const [componentAttributeName, elementAttributeName, sourceFileAttributeName] = attributeNames;
330328

@@ -340,10 +338,7 @@ function applyAttributes(
340338
const elementName = getPathName(t, openingElement);
341339

342340
const isAnIgnoredComponent = ignoredComponents.some(
343-
(ignoredComponent) =>
344-
matchesIgnoreRule(ignoredComponent[0], sourceFileName) &&
345-
matchesIgnoreRule(ignoredComponent[1], componentName) &&
346-
matchesIgnoreRule(ignoredComponent[2], elementName)
341+
(ignoredComponent) => ignoredComponent === componentName || ignoredComponent === elementName
347342
);
348343

349344
// Add a stable attribute for the element name but only for non-DOM names
@@ -501,10 +496,6 @@ function isReactFragment(t: typeof Babel.types, openingElement: Babel.NodePath):
501496
return false;
502497
}
503498

504-
function matchesIgnoreRule(rule: string, name: string | undefined | null) {
505-
return rule === "*" || rule === name;
506-
}
507-
508499
function hasAttributeWithName(
509500
openingElement: Babel.NodePath<Babel.types.JSXOpeningElement>,
510501
name: string | undefined | null

0 commit comments

Comments
 (0)