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

Commit

Permalink
fix incorrect deduping of named to namespace imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Aug 24, 2023
1 parent 86b50b5 commit 2d48de4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,21 @@ Sentry.init({
tracesSampleRate: 1.0,
});
Sentry.addExtensionMethods();`
Sentry.setTag('shouldBePrefixed', true);
Sentry.addExtensionMethods();
const transaction = Sentry.startTransaction({ name: 'test-transaction' });
Sentry.configureScope(scope => {
scope.setSpan(transaction);
});
transaction.setTag('shouldBePrefixed', false)
doLongRunningThing();
transaction.finish();`
);

assertStringEquals(
Expand Down Expand Up @@ -146,7 +160,7 @@ addExtensionMethods();`

assertStringEquals(
actual2,
` const { init: SentryInit } = require('@sentry/node');
`const { init: SentryInit } = require('@sentry/node');
const { addExtensionMethods } = require("@sentry/node");
SentryInit({
Expand Down
20 changes: 16 additions & 4 deletions src/utils/jscodeshift.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,24 @@ function dedupeImportStatements(packageName, root, j) {
)
.forEach(specifier => {
const id = specifier.node.imported.name;
root.find(j.Identifier, { name: id }).forEach(idPath => {
idPath.replace(j.memberExpression(j.identifier(packageNamespace), j.identifier(id)));
});
otherPackageImportPath.replace(undefined);
root
.find(j.Identifier, { name: id })
.filter(idPath => {
return idPath.parentPath.value.type !== 'MemberExpression';
})
.forEach(idPath => {
idPath.replace(j.memberExpression(j.identifier(packageNamespace), j.identifier(id)));
});
otherPackageImportPath.node.specifiers?.splice(
otherPackageImportPath.node.specifiers.indexOf(specifier.node),
1
);
});

if (!otherPackageImportPath.node.specifiers?.length) {
otherPackageImportPath.replace(undefined);
}

// case 1b: Old package import is a namespace import (e.g. import * as Tracing from '@sentry/tracing')
j(otherPackageImportPath)
.filter(t => t.node.specifiers?.length === 1)
Expand Down
17 changes: 16 additions & 1 deletion test-fixtures/tracingAppNodeEsm/namespace-named.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import * as Sentry from '@sentry/node';
import { addExtensionMethods } from '@sentry/tracing';
import { setTag } from '@sentry/node';
import { addExtensionMethods, startTransaction } from '@sentry/tracing';

Sentry.init({
dsn: '__DSN__',
tracesSampleRate: 1.0,
});

setTag('shouldBePrefixed', true);

addExtensionMethods();

const transaction = startTransaction({ name: 'test-transaction' });

Sentry.configureScope(scope => {
scope.setSpan(transaction);
});

transaction.setTag('shouldBePrefixed', false)

doLongRunningThing();

transaction.finish();

0 comments on commit 2d48de4

Please sign in to comment.