Skip to content

Instantly share code, notes, and snippets.

@jhsu
Created January 22, 2018 19:42
Show Gist options
  • Save jhsu/94d876d980d5c5fcc313063d10f6f36d to your computer and use it in GitHub Desktop.
Save jhsu/94d876d980d5c5fcc313063d10f6f36d to your computer and use it in GitHub Desktop.
codemod for changing getFirst calls
export default function transformer(file, api) {
const j = api.jscodeshift;
return j(file.source)
.find(j.CallExpression, {
callee: { name: 'getFirst' },
})
.forEach(path => {
const [props, constructor, defaultValue] = path.value.arguments;
path.value.arguments = [props, constructor];
if (defaultValue) {
j(path).replaceWith(j.logicalExpression('||', path.value, defaultValue));
}
})
.toSource();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment