/
githubmirror
/
strapi
Обзор
Документация
Войти
/
githubmirror
/
strapi
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
packages/utils/upgrade/resources/examples/disable-jsx-buttons.code.ts
42 строки
1 KB
Convly
chore: add example codemod for react transformations
18 мар 2024, 14:02
18 мар 2024, 14:02
a8210a7
Код
Авторство
О чём код?
import type { Transform } from 'jscodeshift'; import { relative } from 'node:path'; const transform: Transform = (file, api) => { const j = api.jscodeshift; const root = j.withParser('JSX')(file.source); const isReactFile = file.path.endsWith('.jsx') || file.path.endsWith('.tsx'); if (!isReactFile) { return root.toSource(); } const fileName = relative(process.cwd(), file.path); console.log(`Found React file: ${fileName}`); const buttons = root.findJSXElements('Button'); console.log(`Found ${buttons.length} buttons in ${fileName}`); buttons.forEach((button) => { const { openingElement } = button.node; const isDisabled = openingElement.attributes.some( (attribute) => j.JSXAttribute.check(attribute) && attribute.name.name === 'disabled' ); console.log(`Is disabled? ${isDisabled}`); if (!isDisabled) { openingElement.attributes.push( j.jsxAttribute(j.jsxIdentifier('disabled'), j.jsxExpressionContainer(j.literal(true))) ); console.log('Added the disabled attribute'); } }); return root.toSource(); }; export default transform;