Last active
January 6, 2023 16:44
-
-
Save utvk/c1b22332fcbec037c357e2edcc1f3e42 to your computer and use it in GitHub Desktop.
jscodeshift add type attribute to jsx buttons
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
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