Skip to content

feat: Ignore Fragment components by default for react component annotation #687

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions packages/babel-plugin-component-annotate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ interface AnnotationPluginPass extends PluginPass {

type AnnotationPlugin = PluginObj<AnnotationPluginPass>;

const DEFAULT_IGNORED_REACT_COMPONENTS = ["Fragment"];

// We must export the plugin as default, otherwise the Babel loader will not be able to resolve it when configured using its string identifier
export default function componentNameAnnotatePlugin({ types: t }: typeof Babel): AnnotationPlugin {
return {
Expand All @@ -69,14 +71,21 @@ export default function componentNameAnnotatePlugin({ types: t }: typeof Babel):
return;
}

const ignoredComponents = [
...new Set([
...(state.opts.ignoredComponents ?? []),
...DEFAULT_IGNORED_REACT_COMPONENTS,
]),
];

functionBodyPushAttributes(
state.opts["annotate-fragments"] === true,
t,
path,
path.node.id.name,
sourceFileNameFromState(state),
attributeNamesFromState(state),
state.opts.ignoredComponents ?? []
ignoredComponents
);
},
ArrowFunctionExpression(path, state) {
Expand All @@ -97,14 +106,21 @@ export default function componentNameAnnotatePlugin({ types: t }: typeof Babel):
return;
}

const ignoredComponents = [
...new Set([
...(state.opts.ignoredComponents ?? []),
...DEFAULT_IGNORED_REACT_COMPONENTS,
]),
];

functionBodyPushAttributes(
state.opts["annotate-fragments"] === true,
t,
path,
parent.id.name,
sourceFileNameFromState(state),
attributeNamesFromState(state),
state.opts.ignoredComponents ?? []
ignoredComponents
);
},
ClassDeclaration(path, state) {
Expand All @@ -118,7 +134,12 @@ export default function componentNameAnnotatePlugin({ types: t }: typeof Babel):
return;
}

const ignoredComponents = state.opts.ignoredComponents ?? [];
const ignoredComponents = [
...new Set([
...(state.opts.ignoredComponents ?? []),
...DEFAULT_IGNORED_REACT_COMPONENTS,
]),
];

render.traverse({
ReturnStatement(returnStatement) {
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-plugin-component-annotate/src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"extends": "@sentry-internal/sentry-bundler-plugin-tsconfig/base-config.json",
"include": ["./**/*", "../package.json"],
"compilerOptions": {
"esModuleInterop": true
"esModuleInterop": true,
"target": "ES2020" // es2020 needed for "new Set()"
}
}
2 changes: 1 addition & 1 deletion packages/bundler-plugin-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ export function sentryUnpluginFactory({
if (options.reactComponentAnnotation) {
if (!options.reactComponentAnnotation.enabled) {
logger.debug(
"The component name annotate plugin is currently disabled. Skipping component name annotations."
"The component name annotate plugin is disabled. Skipping component name annotations."
);
} else if (options.reactComponentAnnotation.enabled && !componentNameAnnotatePlugin) {
logger.warn(
Expand Down
Loading