Skip to content

Instantly share code, notes, and snippets.

@darrentorpey
Last active November 11, 2022 12:46
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 darrentorpey/d77ed863719d644841b267f7bd7c2358 to your computer and use it in GitHub Desktop.
Save darrentorpey/d77ed863719d644841b267f7bd7c2358 to your computer and use it in GitHub Desktop.
jscodeshift codemod for Emotion CSS code that converts `styled('p')` expression to `styled.p` expressions etc.
/**
* Converts each `styled('p')` expression to a `styled.p` expression etc.
*/
function convertExpressions(j, root) {
root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: 'styled',
},
})
.filter(
p =>
p.value.arguments.length === 1 &&
p.value.arguments[0].type === 'Literal' &&
typeof p.value.arguments[0].value === 'string'
)
.replaceWith(p => `styled.${p.value.arguments[0].value}`)
}
export default function transformer(file, api) {
const j = api.jscodeshift
const root = j(file.source)
convertExpressions(j, root)
return root.toSource()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment