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

fix: Better support for .vue. and .svelte files using jscodeshift-adapters #71

Merged
merged 2 commits into from
Aug 6, 2024
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"chalk": "^5.3.0",
"globby": "^14.0.1",
"jscodeshift": "^0.15.2",
"jscodeshift-adapters": "1.0.2",
"node-html-parser": "^6.1.13",
"semver": "^7.6.0",
"yargs": "^17.7.2"
Expand Down
965 changes: 537 additions & 428 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function _run() {
.option('ignoreFilePatterns', {
alias: 'if',
describe: 'Glob pattern which files should be ignored',
default: ['node_modules', '**/*.svelte'],
default: ['node_modules'],
type: 'string',
array: true,
})
Expand Down
153 changes: 76 additions & 77 deletions src/transformers/addMigrationComments/transform.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { adapt } = require('jscodeshift-adapters');
const Sentry = require('@sentry/node');

const { wrapJscodeshift } = require('../../utils/dom.cjs');
const { addTodoComment } = require('../../utils/jscodeshift.cjs');

/**
Expand All @@ -9,94 +9,93 @@ const { addTodoComment } = require('../../utils/jscodeshift.cjs');
*
* @type {import('jscodeshift').Transform}
*/
module.exports = function (fileInfo, api) {
function addMigrationComments(fileInfo, api) {
const j = api.jscodeshift;
const source = fileInfo.source;
const fileName = fileInfo.path;

return wrapJscodeshift(j, source, fileName, (j, source) => {
const tree = j(source);
const tree = j(source);

let hasChanges = false;
let hasChanges = false;

const methodCommentMap = new Map([
[
'startTransaction',
'Use `startInactiveSpan()` instead - see https://github.com/getsentry/sentry-javascript/blob/develop/docs/v8-new-performance-apis.md',
],
[
'startChild',
'Use `startInactiveSpan()` instead - see https://github.com/getsentry/sentry-javascript/blob/develop/docs/v8-new-performance-apis.md',
],
[
'makeMain',
'Use `setCurrentClient()` instead - see https://github.com/getsentry/sentry-javascript/blob/develop/docs/v8-initializing.md',
],
[
'getActiveTransaction',
'Use `getActiveSpan()` instead. If you use this only to start a child, use `startInactiveSpan({ onlyIfParent: true })` instead - see https://github.com/getsentry/sentry-javascript/blob/develop/docs/v8-new-performance-apis.md',
],
]);
const methodCommentMap = new Map([
[
'startTransaction',
'Use `startInactiveSpan()` instead - see https://github.com/getsentry/sentry-javascript/blob/develop/docs/v8-new-performance-apis.md',
],
[
'startChild',
'Use `startInactiveSpan()` instead - see https://github.com/getsentry/sentry-javascript/blob/develop/docs/v8-new-performance-apis.md',
],
[
'makeMain',
'Use `setCurrentClient()` instead - see https://github.com/getsentry/sentry-javascript/blob/develop/docs/v8-initializing.md',
],
[
'getActiveTransaction',
'Use `getActiveSpan()` instead. If you use this only to start a child, use `startInactiveSpan({ onlyIfParent: true })` instead - see https://github.com/getsentry/sentry-javascript/blob/develop/docs/v8-new-performance-apis.md',
],
]);

// Find `xxx()` calls
tree
.find(j.CallExpression, {
callee: {
type: 'Identifier',
},
})
.forEach(path => {
if (path.value.callee.type === 'Identifier') {
const comment = methodCommentMap.get(path.value.callee.name);
if (comment) {
hasChanges = true;
addTodoComment(j, path, comment);
}
// Find `xxx()` calls
tree
.find(j.CallExpression, {
callee: {
type: 'Identifier',
},
})
.forEach(path => {
if (path.value.callee.type === 'Identifier') {
const comment = methodCommentMap.get(path.value.callee.name);
if (comment) {
hasChanges = true;
addTodoComment(j, path, comment);
}
});
}
});

// Find `Sentry.xxx()` calls
tree
.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
property: {
type: 'Identifier',
},
// Find `Sentry.xxx()` calls
tree
.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
property: {
type: 'Identifier',
},
})
.forEach(path => {
if (path.value.callee.type === 'MemberExpression' && path.value.callee.property.type === 'Identifier') {
const comment = methodCommentMap.get(path.value.callee.property.name);
if (comment) {
hasChanges = true;
addTodoComment(j, path, comment);
}
},
})
.forEach(path => {
if (path.value.callee.type === 'MemberExpression' && path.value.callee.property.type === 'Identifier') {
const comment = methodCommentMap.get(path.value.callee.property.name);
if (comment) {
hasChanges = true;
addTodoComment(j, path, comment);
}
});

// Find `new Hub()` & `new Sentry.Hub()`
tree.find(j.NewExpression).forEach(path => {
if (
(path.value.callee.type === 'Identifier' && path.value.callee.name === 'Hub') ||
(path.value.callee.type === 'MemberExpression' &&
path.value.callee.property.type === 'Identifier' &&
path.value.callee.property.name === 'Hub')
) {
hasChanges = true;
addTodoComment(
j,
path,
'Use `new Scope()` instead - see https://github.com/getsentry/sentry-javascript/blob/develop/docs/v8-initializing.md'
);
}
});

if (hasChanges) {
Sentry.setTag('added-todo-comments', true);
Sentry.metrics.increment('added-todo-comments', 1);
// Find `new Hub()` & `new Sentry.Hub()`
tree.find(j.NewExpression).forEach(path => {
if (
(path.value.callee.type === 'Identifier' && path.value.callee.name === 'Hub') ||
(path.value.callee.type === 'MemberExpression' &&
path.value.callee.property.type === 'Identifier' &&
path.value.callee.property.name === 'Hub')
) {
hasChanges = true;
addTodoComment(
j,
path,
'Use `new Scope()` instead - see https://github.com/getsentry/sentry-javascript/blob/develop/docs/v8-initializing.md'
);
}

return hasChanges ? tree.toSource() : undefined;
});
};

if (hasChanges) {
Sentry.setTag('added-todo-comments', true);
Sentry.metrics.increment('added-todo-comments', 1);
}

return hasChanges ? tree.toSource() : undefined;
}

module.exports = adapt(addMigrationComments);
Loading
Loading