Created
May 11, 2023 15:26
-
-
Save AlexFrazer/673f9c4637a5fc0165a06ce587404474 to your computer and use it in GitHub Desktop.
Changes a prop value to a new prop value
This file contains 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
// 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