This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add configureScope
transformer
#19
Merged
Merged
Changes from 1 commit
Commits
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 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 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,137 @@ | ||
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'); | ||
|
||
assertStringEquals( | ||
withImports, | ||
`import { getCurrentScope } from '@sentry/browser'; | ||
import * as Sentry from '@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( | ||
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' }); | ||
}; | ||
}` | ||
); | ||
}); | ||
}); |
This file contains 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,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); | ||
}, | ||
}; |
This file contains 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,128 @@ | ||
const { hasSentryImportOrRequire, replaceImported } = require('../../utils/jscodeshift.cjs'); | ||
const { wrapJscodeshift } = require('../../utils/dom.cjs'); | ||
|
||
/** | ||
* This transform converts usages of `configureScope((scope) => {})` to use `getCurrentScope()` instead. | ||
* | ||
* Replaces: | ||
* | ||
* ``` | ||
* configureScope((scope) => { | ||
* scope.setTag('foo', 'bar'); | ||
* scope.addEventProcessor(fn); | ||
* }); | ||
* ``` | ||
* | ||
* with | ||
* | ||
* ``` | ||
* const scope = getCurrentScope(); | ||
* scope.setTag('foo', 'bar'); | ||
* scope.addEventProcessor(fn); | ||
* | ||
* @param {import('jscodeshift').FileInfo} fileInfo | ||
* @param {import('jscodeshift').API} api | ||
* @param {import('jscodeshift').Options & { sentry: import('types').RunOptions & {sdk: string} }} options | ||
*/ | ||
module.exports = function (fileInfo, api, options) { | ||
const j = api.jscodeshift; | ||
const source = fileInfo.source; | ||
const fileName = fileInfo.path; | ||
|
||
return wrapJscodeshift(j, source, fileName, (j, source) => { | ||
const tree = j(source); | ||
|
||
// If no sentry import, skip it | ||
if (!hasSentryImportOrRequire(source)) { | ||
return undefined; | ||
} | ||
|
||
if (!source.includes('configureScope')) { | ||
return undefined; | ||
} | ||
|
||
// Replace `configureScope()` | ||
tree.find(j.CallExpression).forEach(path => { | ||
if (path.value.callee.type !== 'Identifier' || path.value.callee.name !== 'configureScope') { | ||
return; | ||
} | ||
|
||
const callbackFn = path.value.arguments[0]; | ||
if (callbackFn.type !== 'ArrowFunctionExpression' && callbackFn.type !== 'FunctionExpression') { | ||
return; | ||
} | ||
|
||
// The var name of the scope callback, e.g. (scope) => {} would be "scope" | ||
const scopeVarName = callbackFn.params[0]?.type === 'Identifier' ? callbackFn.params[0].name : undefined; | ||
|
||
if (!scopeVarName) { | ||
return; | ||
} | ||
|
||
const callbackBody = callbackFn.body; | ||
|
||
if (callbackBody.type !== 'BlockStatement') { | ||
return; | ||
} | ||
|
||
path.replace( | ||
j.blockStatement([ | ||
j.variableDeclaration('const', [ | ||
j.variableDeclarator(j.identifier(scopeVarName), j.callExpression(j.identifier('getCurrentScope'), [])), | ||
]), | ||
...callbackBody.body, | ||
]) | ||
); | ||
}); | ||
|
||
// Replace e.g. `SentryUtuils.configureScope()` | ||
tree.find(j.CallExpression).forEach(path => { | ||
if ( | ||
path.value.callee.type !== 'MemberExpression' || | ||
path.value.callee.property.type !== 'Identifier' || | ||
path.value.callee.property.name !== 'configureScope' | ||
) { | ||
return; | ||
} | ||
|
||
const calleeObj = path.value.callee.object; | ||
|
||
const callbackFn = path.value.arguments[0]; | ||
if (callbackFn.type !== 'ArrowFunctionExpression' && callbackFn.type !== 'FunctionExpression') { | ||
return; | ||
} | ||
|
||
// The var name of the scope callback, e.g. (scope) => {} would be "scope" | ||
const scopeVarName = callbackFn.params[0]?.type === 'Identifier' ? callbackFn.params[0].name : undefined; | ||
|
||
if (!scopeVarName) { | ||
return; | ||
} | ||
|
||
const callbackBody = callbackFn.body; | ||
|
||
if (callbackBody.type !== 'BlockStatement') { | ||
return; | ||
} | ||
|
||
path.replace( | ||
j.blockStatement([ | ||
j.variableDeclaration('const', [ | ||
j.variableDeclarator( | ||
j.identifier(scopeVarName), | ||
j.memberExpression(calleeObj, j.callExpression(j.identifier('getCurrentScope'), [])) | ||
), | ||
]), | ||
...callbackBody.body, | ||
]) | ||
); | ||
}); | ||
|
||
const sdk = options.sentry?.sdk; | ||
if (sdk) { | ||
replaceImported(j, tree, source, sdk, new Map([['configureScope', 'getCurrentScope']])); | ||
} | ||
|
||
return tree.toSource(); | ||
}); | ||
}; |
This file contains 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,25 @@ | ||
import { configureScope } from '@sentry/browser'; | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
function orig() { | ||
// do something | ||
} | ||
|
||
function doSomething() { | ||
configureScope((scope) => { | ||
scope.setTag('aaa', 'aaa'); | ||
scope.setExtra('bbb', { bbb: 'bbb' }); | ||
}); | ||
|
||
configureScope(function (scope) { | ||
scope.setTag('aaa', 'aaa'); | ||
scope.setExtra('bbb', { bbb: 'bbb' }); | ||
}); | ||
|
||
configureScope(orig); | ||
|
||
Sentry.configureScope((scope) => { | ||
scope.setTag('ccc', 'ccc'); | ||
scope.setExtra('ddd', { ddd: 'ddd' }); | ||
}); | ||
} |
This file contains 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,25 @@ | ||
import { configureScope } from '@sentry/browser'; | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
function orig(): void { | ||
// do something | ||
} | ||
|
||
function doSomething(): void { | ||
configureScope((scope) => { | ||
scope.setTag('aaa', 'aaa'); | ||
scope.setExtra('bbb', { bbb: 'bbb' }); | ||
}); | ||
|
||
configureScope(function (scope) { | ||
scope.setTag('aaa', 'aaa'); | ||
scope.setExtra('bbb', { bbb: 'bbb' }); | ||
}); | ||
|
||
configureScope(orig); | ||
|
||
Sentry.configureScope((scope) => { | ||
scope.setTag('ccc', 'ccc'); | ||
scope.setExtra('ddd', { ddd: 'ddd' }); | ||
}); | ||
} |
This file contains 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,25 @@ | ||
const { configureScope } = require('@sentry/browser'); | ||
const Sentry = require('@sentry/browser'); | ||
|
||
function orig() { | ||
// do something | ||
} | ||
|
||
function doSomething() { | ||
configureScope((scope) => { | ||
scope.setTag('aaa', 'aaa'); | ||
scope.setExtra('bbb', { bbb: 'bbb' }); | ||
}); | ||
|
||
configureScope(function (scope) { | ||
scope.setTag('aaa', 'aaa'); | ||
scope.setExtra('bbb', { bbb: 'bbb' }); | ||
}); | ||
|
||
configureScope(orig); | ||
|
||
Sentry.configureScope((scope) => { | ||
scope.setTag('ccc', 'ccc'); | ||
scope.setExtra('ddd', { ddd: 'ddd' }); | ||
}); | ||
} |
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.
There was a problem hiding this comment.
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(...))
?