Created
May 9, 2016 21:51
-
-
Save alinz/240134594a8c7eaead927f6e242209d7 to your computer and use it in GitHub Desktop.
Sample codemod transformation to refactor react-native < 25 to react-native 25
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
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