/
githubmirror
/
material-ui
Обзор
Документация
Войти
/
githubmirror
/
material-ui
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
packages/mui-codemod/src/util/findComponentJSX.js
39 строк
1 KB
Siriwat K
[codemod] Add package name option (#45977)
12 май 2025, 04:42
Не верифицирован
12 май 2025, 04:42
9f4df51
Код
Авторство
О чём код?
/** * Find all the JSXElements of a given component name. * * @param {import('jscodeshift')} j * @param {{ root: import('jscodeshift').Collection; componentName: string; packageName?: string }} options * @param {(path: import('jscodeshift').ASTPath<import('jscodeshift').JSXElement>) => void} callback * */ export default function findComponentJSX(j, options, callback) { const { root, componentName, packageName = '@mui/material' } = options; // case 1: import ComponentName from '@mui/material/ComponentName'; // case 2: import { ComponentName } from '@mui/material'; // case 3: import { ComponentName as SomethingElse } from '@mui/material'; const importName = new Set(); root .find(j.ImportDeclaration) .filter((path) => path.node.source.value.match(new RegExp(`^${packageName}(/${componentName})?$`)), ) .forEach((path) => { path.node.specifiers.forEach((specifier) => { if (specifier.type === 'ImportDefaultSpecifier') { importName.add(specifier.local.name); } if (specifier.type === 'ImportSpecifier' && specifier.imported.name === componentName) { importName.add(specifier.local.name); } }); }); [...importName].forEach((name) => { root.findJSXElements(name).forEach((elementPath) => { callback(elementPath); }); }); }