Open vanessayuenn opened 1 year ago
@kasperpeulen not sure if it helps, but I wrote this codemod to apply in the codebase when I replaced the libraries internally (demo link):
import type { FileInfo, API, Options } from 'jscodeshift';
export default function transform(
file: FileInfo,
api: API,
options: Options,
): string | undefined {
const j = api.jscodeshift;
const root = j(file.source);
// Define the libraries to gather imports from
const libraries = ['@storybook/jest', '@storybook/testing-library'];
// Initialize an array to store the specifiers to move
let specifiersToMove = [];
// Find import declarations
root.find(j.ImportDeclaration).forEach((path) => {
// Check if import source is a string and is in the libraries array
if (
typeof path.node.source.value === 'string' &&
libraries.includes(path.node.source.value)
) {
// Add the specifiers to the specifiersToMove array
specifiersToMove = [...specifiersToMove, ...path.node.specifiers];
// Remove the original import declaration
j(path).remove();
}
});
if (specifiersToMove.length > 0) {
// Create new import declaration for '@storybook/test'
const newImport = j.importDeclaration.from({
specifiers: specifiersToMove,
source: j.literal('@storybook/test'),
});
// Insert new import at the beginning of the file
root.find(j.Program).get('body', '0').insertBefore(newImport);
return root.toSource();
}
}
1.argTypesRegex
→ explicit actions@storybook/jest
&@testing-library
→@storybook/test