замена импорта с помощью проблемы jscodemod
Я новичок в jscodemod, поэтому благодарю за помощь в этом отношении.
Цель:
- Я пытаюсь заменить
old
импорт сnew one
Исходный код:
import type { Query } from 'api/types/query' // old
import type { IQuery } from '@demo/sdk' // new one
export interface IProps {
/** The query used to formulate QuickCalc selector */
query: Query
}
// component
export const DemoComponent: React.FC<IProps> = ({
query
}) => {
return <Demo query={query} />
}
Проблема:
Как я могу заменить все вхождения
Query
(с учетом регистра) сIQuery
с помощью JSCodeMod?
Я попытался решить проблему, но слишком ново, чтобы разобраться:
export default (fileInfo, api) => {
const j = api.jscodeshift;
const root = j(fileInfo.source);
// find declaration for "Query" import
const importDeclaration = root.find(j.importDeclaration, {
name: {
type: 'Identifier',
escapedText: 'Query',
},
});
// Need to somehow find and replace Query
console.log('importDeclaration', importDeclaration)
return root.toSource();
};