in packages/flow-upgrade/src/codemods/ReactUtils.js [30:77]
function getImportedReactName(path: any): ?string {
// All of the modules we want to treat as React.
const REACT_MODULES = new Set(['react', 'React']);
// Find the first require for React that does not destructure React but
// instead gives it a name.
const reactRequire = path
.findVariableDeclarators()
.filter(
j.filters.VariableDeclarator.requiresModule(Array.from(REACT_MODULES)),
)
.nodes()
.find(node => node.id && node.id.type === 'Identifier');
// If we found a require for React then return the name.
if (reactRequire) {
return reactRequire.id.name;
}
// Get all of the import declarations that import React.
const reactImports = path
.find(j.ImportDeclaration, {
type: 'ImportDeclaration',
source: {
type: 'Literal',
value: value => REACT_MODULES.has(value),
},
})
.nodes();
// For all of the React imports...
for (let i = 0; i < reactImports.length; i++) {
const reactImport = reactImports[i];
// ...and for all of each import's specifiers...
for (let j = 0; j < reactImport.specifiers.length; j++) {
const specifier = reactImport.specifiers[j];
// ...check to see if it is either a default specifier or a namespace
// specifier. If it is either and it has a local name then return that
// local name.
if (
(specifier.type === 'ImportDefaultSpecifier' ||
specifier.type === 'ImportNamespaceSpecifier') &&
specifier.local &&
specifier.local.type === 'Identifier'
) {
return specifier.local.name;
}
}
}
// Otherwise we can't find anything and should return null.
return null;
}