All files isReactComponentSuperClass.js

81.58% Statements 31/38
75% Branches 21/28
100% Functions 4/4
96.43% Lines 27/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55      1x 1x   1x 30x     1x 27x     1x 5x       30x 25x 25x   25x   25x   25x 23x   2x 1x 1x     23x     5x 5x 5x   4x 4x 4x 4x   2x              
// @flow
import type {Path} from './types';
 
const GLOBAL_NAME = 'React';
const MODULE_NAME = 'react';
 
let getBinding = path => {
  return path.scope.getBinding(path.node.name);
};
 
let isReactComponentMember = path => {
  return path.node.name === 'Component' || path.node.name === 'PureComponent';
};
 
let getSourceFromSpecifier = path => {
  return path.parent.source.value;
};
 
export default function isReactComponentSuperClass(superClass: Path) {
  if (superClass.isMemberExpression()) {
    let object: Path = superClass.get('object');
    let property: Path = superClass.get('property');
 
    Iif (!object.isIdentifier()) return false;
 
    let binding = getBinding(object);
 
    if (!binding) {
      if (object.node.name !== GLOBAL_NAME) return false;
    } else {
      if (binding.kind !== 'module') return false;
      Iif (!binding.path.isImportDefaultSpecifier()) return false;
      Iif (getSourceFromSpecifier(binding.path) !== MODULE_NAME) return false;
    }
 
    return isReactComponentMember(property);
  }
 
  Eif (superClass.isIdentifier()) {
    let binding = getBinding(superClass);
    if (!binding) return false;
 
    Iif (binding.kind !== 'module') return false;
    Iif (!binding.path.isImportSpecifier()) return false;
    Iif (getSourceFromSpecifier(binding.path) !== MODULE_NAME) return false;
    if (!isReactComponentMember(binding.path.get('imported'))) return false;
 
    return true;
  }
 
  throw superClass.buildCodeFrameError(
    `Unexpected super class type: ${superClass.type}`,
  );
}