Skip to content

Instantly share code, notes, and snippets.

@alinz
Created May 9, 2016 21:51
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 alinz/240134594a8c7eaead927f6e242209d7 to your computer and use it in GitHub Desktop.
Save alinz/240134594a8c7eaead927f6e242209d7 to your computer and use it in GitHub Desktop.
Sample codemod transformation to refactor react-native < 25 to react-native 25
export default function transformer(file, api, options) {
const j = api.jscodeshift;
const printOptions = options.printOptions || {quote: 'single'};
const root = j(file.source);
const reactImport = () => {
return j.importDeclaration(
[
j.importDefaultSpecifier(
j.identifier('React')
),
j.importSpecifier(
j.identifier('Component'),
j.identifier('Component')
)
],
j.literal('react')
)
}
//first we need to remove Component from react-native import
//import { Component } from 'react-native'
const a = root.find(
j.ImportDeclaration, { source: { value: 'react-native' }}
).forEach((path) => {
j(path)
.find(j.ImportDefaultSpecifier, { local: { name: 'React' }})
.remove()
}).forEach((path) => {
j(path)
.find(j.ImportSpecifier, { local: { name: 'Component' }})
.remove()
});
root.get().value.program.body.unshift(reactImport());
return root.toSource();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment