Skip to content

Commit

Permalink
renameImport
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenn Creighton authored and benjamn committed Jul 31, 2020
1 parent 915c378 commit a184477
Showing 1 changed file with 43 additions and 41 deletions.
84 changes: 43 additions & 41 deletions jscodeshift-migrate-2-to-3.js
Expand Up @@ -17,26 +17,38 @@ export default function transformer(file, api) {

const source = j(file.source);

// Replace `@apollo/react-hooks` with `@apollo/client`
// Replace `apollo-client` with `@apollo/client`
renameImport('apollo-client', '@apollo/client');

source
.find(j.ImportDeclaration)
.filter(
path =>
path.value.source.value ===
'@apollo/react-hooks',
)
.find(j.Literal)
.replaceWith(path => ({
...path.value,
value: '@apollo/client',
}));
// TODO: Create if @apollo/client doesn't exist.
// source.find(j.Program).replaceWith(p => ({
// ...p.value,
// body: [
// {
// type: 'ImportDeclaration',
// specifiers: specifiers.map(
// importSpecifier,
// ),
// source: {
// type: 'Literal',
// value: '@apollo/client',
// },
// },
// ...p.value.body,
// ],
// }));

// Move import specifiers from `gql` and `apollo-link`
// modules to `@apollo/client`:
moveSpecifiersToApolloClient('@apollo/react-hooks', []);
moveSpecifiersToApolloClient('apollo-cache-inmemory', ['InMemoryCache']);
moveSpecifiersToApolloClient('graphql-tag', []);
moveSpecifiersToApolloClient('apollo-link', []);
moveSpecifiersToApolloClient('apollo-link-http', []);
moveSpecifiersToApolloClient('apollo-link-http-common', []);

moveSpecifiersToApolloClient('graphql-tag', ['gql']);
moveSpecifiersToApolloClient('apollo-link', ['Observable']);
renameImport('@apollo/react-components', '@apollo/client/react/components');
renameImport('@apollo/react-hoc', '@apollo/client/react/hoc');
renameImport('@apollo/react-ssr', '@apollo/client/react/ssr');
renameImport('@apollo/react-testing', '@apollo/client/testing');

return source.toSource();

Expand All @@ -47,13 +59,7 @@ export default function transformer(file, api) {
const moduleImport = getImport(moduleName);
moduleImport.remove();
if (moduleImport.size()) {
const clientImports = source
.find(j.ImportDeclaration)
.filter(
path =>
path.value.source.value ===
'@apollo/client',
);
const clientImports = getImport('@apollo/client');
if (clientImports.size()) {
clientImports.replaceWith(p => ({
...p.value,
Expand All @@ -62,27 +68,19 @@ export default function transformer(file, api) {
...p.value.specifiers,
],
}));
} else {
source.find(j.Program).replaceWith(p => ({
...p.value,
body: [
{
type: 'ImportDeclaration',
specifiers: specifiers.map(
importSpecifier,
),
source: {
type: 'Literal',
value: '@apollo/client',
},
},
...p.value.body,
],
}));
}
}
}

function renameImport(oldModuleName, newModuleName) {
getImport(oldModuleName)
.find(j.Literal)
.replaceWith(path => ({
...path.value,
value: newModuleName,
}));
}

function getImport(moduleName) {
return source
.find(j.ImportDeclaration)
Expand All @@ -101,3 +99,7 @@ function importSpecifier(name) {
},
};
}

// Warn about apollo-boost?
// Warn about graphql-anywhere?
// Warn about graphql-tag?

0 comments on commit a184477

Please sign in to comment.