Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

feat: Add configureScope transformer #19

Merged
merged 4 commits into from
Dec 14, 2023
Merged

feat: Add configureScope transformer #19

merged 4 commits into from
Dec 14, 2023

Conversation

mydea
Copy link
Member

@mydea mydea commented Dec 14, 2023

Rewrites usages of configureScope() to use getCurrentScope() instead. Note that this will rewrite this to code
blocks, which may not be the preferred syntax in all cases, but it's the only way to make this work somewhat reliably
with avoiding variable clashes etc.

This will rewrite:

Sentry.configureScope(scope => {
  scope.setTag('ccc', 'ccc');
  scope.setExtra('ddd', { ddd: 'ddd' });
});

to

{
  const scope = Sentry.getCurrentScope();
  scope.setTag('ccc', 'ccc');
  scope.setExtra('ddd', { ddd: 'ddd' });
}

@mydea mydea requested a review from Lms24 December 14, 2023 11:45
@mydea mydea self-assigned this Dec 14, 2023
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting - It's been a while! 😅

Good call with still wrapping the statements in a block to avoid variable colissions! Not sure but didn't we add some way for transformers to write something to the CLI if they actually changed stuff? Maybe we should add a message that folks should clean this up?

@mydea
Copy link
Member Author

mydea commented Dec 14, 2023

So I looked some more into this, and added a special handling for when there is only a single statement - in this case, we can rewrite it nicer:

configureScope((scope) => {
  scope.setTag('xx', 'yy');
});

becomes

getCurrentScope().setTag('xx', 'yy');

Which is much nicer!

I found another edge case though I also need to handle, which is getCurrentHub().configureScope() - there, it has to be getScope() instead of getCurrentScope() 😬


// Very hacky, but we check if the callee (e.g. hub.configureScope() or getCurrentHub().configureScope())
// contains "hub", and if so, we use `getScope()` instead of `getCurrentScope()`
let getScopeMethodName = /hub/i.test(j(calleeObj).toSource()) ? 'getScope' : 'getCurrentScope';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lms24 I added this somewhat hacky check, but I think it should work well enough...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there, it has to be getScope() instead of getCurrentScope() 😬

Are we adding another transformer to replace getCurrentHub().getScope() with getCurrentScope()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, figured that too - we should also do that then! But I guess that will be a general getCurrentHub() transformer that rewrites that to all the new/global APIs instead!

Comment on lines 64 to 66
if (callbackBody.type !== 'BlockStatement') {
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about configureScope(scope => scope.setTag(...))?


// Very hacky, but we check if the callee (e.g. hub.configureScope() or getCurrentHub().configureScope())
// contains "hub", and if so, we use `getScope()` instead of `getCurrentScope()`
let getScopeMethodName = /hub/i.test(j(calleeObj).toSource()) ? 'getScope' : 'getCurrentScope';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there, it has to be getScope() instead of getCurrentScope() 😬

Are we adding another transformer to replace getCurrentHub().getScope() with getCurrentScope()?

@mydea
Copy link
Member Author

mydea commented Dec 14, 2023

OK, I think I fix all/most edge cases - ran it on the monorepo, and it seemed good to me - no false positives anymore, at least!

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 🚀

@mydea mydea merged commit 241178d into main Dec 14, 2023
2 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants