jscodeshift script to replace "accessibilityTraits" and "accessibilityComponentType" with "accessibilityStates" and "accessibilityRole"
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
'use strict'; | |
export default function transformer(file, api) { | |
const j = api.jscodeshift; | |
const root = j(file.source); | |
let hasChanges = false; | |
const elements = root.find(j.JSXElement); | |
let values; | |
let valuess; | |
let valuesss; | |
elements.forEach(path => { | |
const openEl = path.node.openingElement; | |
hasChanges = true; | |
for (let i = 0; i < openEl.attributes.length; i++) { | |
if (openEl.attributes[i].name.name === 'accessibilityComponentType') { | |
openEl.attributes.splice(i, 1); | |
} | |
if (openEl.attributes[i].name.name === 'accessibilityTraits') { | |
if (openEl.attributes[i].value.expression) { | |
if (openEl.attributes[i].value.expression.type === 'Literal') { | |
values = openEl.attributes[i].value.expression.value; | |
openEl.attributes[i] = j.jsxAttribute( | |
j.jsxIdentifier('accessibilityRole'), | |
j.literal(values), | |
); | |
} | |
} | |
if (openEl.attributes[i].value) { | |
if ( | |
openEl.attributes[i].value && | |
openEl.attributes[i].value.type === 'Literal' | |
) { | |
valuess = openEl.attributes[i].value.value; | |
openEl.attributes[i] = j.jsxAttribute( | |
j.jsxIdentifier('accessibilityRole'), | |
j.literal(valuess), | |
); | |
} | |
} | |
if (openEl.attributes[i].value.expression) { | |
if ( | |
openEl.attributes[i].value.expression.type === 'ArrayExpression' && | |
openEl.attributes[i].value.expression.elements.length === 1 | |
) { | |
valuesss = openEl.attributes[i].value.expression.elements[0].value; | |
openEl.attributes[i] = j.jsxAttribute( | |
j.jsxIdentifier('accessibilityRole'), | |
j.literal(valuesss), | |
); | |
} | |
} | |
} | |
} | |
}); | |
if (hasChanges) { | |
return root.toSource(); | |
} else { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment