Skip to content

Instantly share code, notes, and snippets.

@utvk
Last active January 6, 2023 16:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utvk/c1b22332fcbec037c357e2edcc1f3e42 to your computer and use it in GitHub Desktop.
Save utvk/c1b22332fcbec037c357e2edcc1f3e42 to your computer and use it in GitHub Desktop.
jscodeshift add type attribute to jsx buttons
module.exports = function(fi, api) {
var j = api.jscodeshift;
const hasNoTypeAttribute = function(path) {
return !path.value.openingElement.attributes.some(function(typePath) {
if (typePath.name.name !== 'type') {
return false;
}
return true;
});
};
const unshiftTypeAttribute = function(path) {
const type = j.jsxAttribute(j.jsxIdentifier('type'), j.literal('button'));
path.value.openingElement.attributes.unshift(type);
};
var out = j(fi.source)
.findJSXElements('button')
.filter(hasNoTypeAttribute)
.forEach(unshiftTypeAttribute)
.toSource();
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment