Skip to content

Instantly share code, notes, and snippets.

@kyo-ago
Created March 5, 2023 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyo-ago/68347430bb6ee2a42fa599ec7342af29 to your computer and use it in GitHub Desktop.
Save kyo-ago/68347430bb6ee2a42fa599ec7342af29 to your computer and use it in GitHub Desktop.
react-admin v3 to v4 upgrade code-mod
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