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

Commit

Permalink
feat: Add configureScope transformer (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea authored Dec 14, 2023
1 parent dfa126e commit 241178d
Show file tree
Hide file tree
Showing 8 changed files with 555 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ but only a few that have been deprecated:
- `Handlers.ExpressRequest``PolymorphicRequest` (Type export)
- `Handlers.extractRequestData``extractRequestData`

### Use getCurrentScope() instead of configureScope()

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:

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

to

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

### Tracing Config v7>v8

Rewrites `tracePropagationTargets` and `tracingOrigins` from Integration-level config to root config on `Sentry.init()`.
Expand Down
182 changes: 182 additions & 0 deletions src/transformers/configureScope/configureScope.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import { afterEach, describe, it } from 'node:test';
import * as assert from 'node:assert';
import { rmSync } from 'node:fs';

import { getDirFileContent, getFixturePath, makeTmpDir } from '../../../test-helpers/testPaths.js';
import { assertStringEquals } from '../../../test-helpers/assert.js';

import configureScopeTransformer from './index.js';

describe('transformers | configureScope', () => {
let tmpDir = '';

afterEach(() => {
if (tmpDir) {
rmSync(tmpDir, { force: true, recursive: true });
tmpDir = '';
}
});

it('has correct name', () => {
assert.equal(configureScopeTransformer.name, 'Use getCurrentScope() instead of configureScope()');
});

it('works with app without Sentry', async () => {
tmpDir = makeTmpDir(getFixturePath('noSentry'));
await configureScopeTransformer.transform([tmpDir], { filePatterns: [] });

const actual1 = getDirFileContent(tmpDir, 'app.js');
assert.equal(actual1, getDirFileContent(`${process.cwd()}/test-fixtures/noSentry`, 'app.js'));
});

it('works with example files', async () => {
tmpDir = makeTmpDir(getFixturePath('configureScope'));
await configureScopeTransformer.transform([tmpDir], { filePatterns: [], sdk: '@sentry/browser' });

const withImports = getDirFileContent(tmpDir, 'withImports.js');
const withImportsTs = getDirFileContent(tmpDir, 'withImports.ts');
const withRequire = getDirFileContent(tmpDir, 'withRequire.js');
const onHub = getDirFileContent(tmpDir, 'onHub.js');

assertStringEquals(
withImports,
`import { getCurrentScope } from '@sentry/browser';
import * as Sentry from '@sentry/browser';
function orig() {
// do something
}
function doSomething() {
getCurrentScope().setTag('aaa', 'aaa');
Sentry.getCurrentScope().setTag('ccc', 'ccc');
{
const scope = getCurrentScope();
scope.setTag('aaa', 'aaa');
scope.setExtra('bbb', { bbb: 'bbb' });
};
{
const scope = getCurrentScope();
scope.setTag('aaa', 'aaa');
scope.setExtra('bbb', { bbb: 'bbb' });
};
configureScope(orig);
{
const scope = Sentry.getCurrentScope();
scope.setTag('ccc', 'ccc');
scope.setExtra('ddd', { ddd: 'ddd' });
};
getCurrentScope().addAttachment({ filename: 'scope.file', data: 'great content!' });
Sentry.getCurrentScope().addAttachment({ filename: 'scope.file', data: 'great content!' });
}`
);

assertStringEquals(
withImportsTs,
`import { getCurrentScope } from '@sentry/browser';
import * as Sentry from '@sentry/browser';
function orig(): void {
// do something
}
function doSomething(): void {
{
const scope = getCurrentScope();
scope.setTag('aaa', 'aaa');
scope.setExtra('bbb', { bbb: 'bbb' });
};
{
const scope = getCurrentScope();
scope.setTag('aaa', 'aaa');
scope.setExtra('bbb', { bbb: 'bbb' });
};
configureScope(orig);
{
const scope = Sentry.getCurrentScope();
scope.setTag('ccc', 'ccc');
scope.setExtra('ddd', { ddd: 'ddd' });
};
}
`
);

assertStringEquals(
withRequire,
`const { getCurrentScope } = require('@sentry/browser');
const Sentry = require('@sentry/browser');
function orig() {
// do something
}
function doSomething() {
{
const scope = getCurrentScope();
scope.setTag('aaa', 'aaa');
scope.setExtra('bbb', { bbb: 'bbb' });
};
{
const scope = getCurrentScope();
scope.setTag('aaa', 'aaa');
scope.setExtra('bbb', { bbb: 'bbb' });
};
configureScope(orig);
{
const scope = Sentry.getCurrentScope();
scope.setTag('ccc', 'ccc');
scope.setExtra('ddd', { ddd: 'ddd' });
};
}`
);

assertStringEquals(
onHub,
`import { getCurrentHub } from '@sentry/browser';
import * as Sentry from '@sentry/browser';
function orig() {
// do something
}
function doSomething() {
{
const scope = Sentry.getCurrentHub().getScope();
scope.setTag('aaa', 'aaa');
scope.setExtra('bbb', { bbb: 'bbb' });
};
{
const scope = getCurrentHub().getScope();
scope.setTag('aaa', 'aaa');
scope.setExtra('bbb', { bbb: 'bbb' });
};
const hub = Sentry.getCurrentHub();
{
const scope = hub.getScope();
scope.setTag('aaa', 'aaa');
scope.setExtra('bbb', { bbb: 'bbb' });
};
const currentHub = Sentry.getCurrentHub();
{
const scope = currentHub.getScope();
scope.setTag('aaa', 'aaa');
scope.setExtra('bbb', { bbb: 'bbb' });
};
}`
);
});
});
16 changes: 16 additions & 0 deletions src/transformers/configureScope/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import path from 'path';
import url from 'url';

import { runJscodeshift } from '../../utils/jscodeshift.js';

/**
* @type {import('types').Transformer}
*/
export default {
name: 'Use getCurrentScope() instead of configureScope()',
transform: async (files, options) => {
const transformPath = path.join(path.dirname(url.fileURLToPath(import.meta.url)), './transform.cjs');

await runJscodeshift(transformPath, files, options);
},
};
Loading

0 comments on commit 241178d

Please sign in to comment.