Created
March 5, 2023 13:38
-
-
Save kyo-ago/68347430bb6ee2a42fa599ec7342af29 to your computer and use it in GitHub Desktop.
react-admin v3 to v4 upgrade code-mod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Transform} from "jscodeshift"; | |
const transform: Transform = (fileInfo, api, options) => { | |
const j = api.jscodeshift; | |
const root = j(fileInfo.source); | |
root.find(j.JSXOpeningElement, { | |
name: { name: 'TextInput' }, | |
}) | |
.replaceWith(({node}) => { | |
const allowEmpty = node.attributes.find(attr => attr.type === "JSXAttribute" && attr.name.name === 'allowEmpty'); | |
const validate = node.attributes.find(attr => attr.type === "JSXAttribute" && attr.name.name === 'validate'); | |
node.attributes = node.attributes.filter(attr => attr.type === "JSXAttribute" && attr.name.name !== 'allowEmpty'); | |
if (!allowEmpty && !validate) { | |
node.attributes.push(j.jsxAttribute(j.jsxIdentifier('validate'), j.jsxExpressionContainer( | |
j.callExpression(j.identifier('required'), []) | |
))); | |
} | |
return node; | |
}); | |
root.find(j.CallExpression, { | |
callee: { | |
name: 'notify' | |
} | |
}).forEach(path => { | |
const args = path.value.arguments; | |
// notify('XXX', 'info'); | |
if (args.length === 2 && args[1].type === 'StringLiteral') { | |
args[1] = j.objectExpression([ | |
j.property('init', j.identifier('type'), args[1]) | |
]); | |
} | |
// notify('XXX', 'info', { smart_count: 1 }); | |
if (args.length === 3 && args[2].type === 'ObjectExpression') { | |
args[1] = j.objectExpression([ | |
j.property('init', j.identifier('type'), args[1]), | |
j.property('init', j.identifier('messageArgs'), args[2]) | |
]); | |
args.pop(); | |
} | |
}); | |
return root.toSource(); | |
}; | |
export default transform; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment