Skip to content

feat: support codemod for individual lodash package imports #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

hyesungoh
Copy link
Contributor

@hyesungoh hyesungoh commented Jun 15, 2025

close #6

only changes at 870141c (between #7)

handling individual lodash packages (like lodash.sortby)

I can not stacking pr in forked repo 😢 feel sorry to bother you

Comment on lines +119 to +153
// import sortBy from 'lodash.sortby' → import sortBy from 'es-toolkit/compat/sortby'
function transformLodashIndividualPackages(root: Collection, j: JSCodeshift): boolean {
const lodashIndividualImports = root.find(j.ImportDeclaration).filter((path) => {
const source = path.node.source.value;
if (typeof source !== 'string' || !source.startsWith('lodash.')) {
return false;
}

const packageName = source.replace('lodash.', '');
return lodashFunctionsLowerCaseToOriginalMap.has(packageName);
});

if (lodashIndividualImports.length === 0) {
return false;
}

lodashIndividualImports.replaceWith((path) => {
const { node } = path;
const modulePath = node.source.value as string;
const lowercaseFunctionName = modulePath.replace('lodash.', '');
const originalFunctionName = lodashFunctionsLowerCaseToOriginalMap.get(lowercaseFunctionName);

if (node.specifiers?.[0]?.local && originalFunctionName) {
const localIdentifier = node.specifiers[0].local;

return j.importDeclaration(
[j.importDefaultSpecifier(localIdentifier)],
j.literal(`es-toolkit/compat/${originalFunctionName}`),
);
}
return node;
});

return true;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

append here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in #7, this files at test directory

but it works with lowercased, so I moved here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support codemod for individual lodash package imports (e.g., lodash.sortby)
1 participant