Skip to content

Instantly share code, notes, and snippets.

@AlexFrazer
Created May 11, 2023 15:26
Show Gist options
  • Save AlexFrazer/673f9c4637a5fc0165a06ce587404474 to your computer and use it in GitHub Desktop.
Save AlexFrazer/673f9c4637a5fc0165a06ce587404474 to your computer and use it in GitHub Desktop.
Changes a prop value to a new prop value
// change-prop-value.js
const j = require('jscodeshift');
module.exports = function (fileInfo, api) {
const componentName = 'MyComponent'; // Replace with your component name
const propName = 'myProp'; // Replace with the prop name you want to change
const newValue = 'newValue'; // Replace with the new value for the prop
const root = j(fileInfo.source);
const updatePropValue = (path) => {
const prop = path.node.openingElement.attributes.find(
(attr) => attr.name && attr.name.name === propName
);
if (prop) {
prop.value = j.literal(newValue);
}
};
root
.find(j.JSXElement, {
openingElement: {
name: {
name: componentName,
},
},
})
.forEach(updatePropValue);
return root.toSource();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment