All files / src/utils getFlowType.js

94.64% Statements 53/56
75% Branches 15/20
100% Functions 17/17
98.15% Lines 53/54
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196                                          4x   4x                 4x           4x                     8x   8x   8x       19x   19x 4x   4x   5x       15x   15x 1x       19x       4x             4x 1x     4x 2x           4x 6x           4x       3x     8x         1x     4x         3x   3x   3x 3x   3x       2x                   2x 3x 3x   3x           2x       3x   3x 6x     3x                     79x     79x 79x 36x 43x 8x 35x 35x       79x       79x    
/*
 * Copyright (c) 2015, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @flow
 *
 */
 
/* eslint no-use-before-define: 0 */
 
import getPropertyName from './getPropertyName';
import printValue from './printValue';
import recast from 'recast';
import getTypeAnnotation from '../utils/getTypeAnnotation';
import resolveToValue from '../utils/resolveToValue';
import isUnreachableFlowType from '../utils/isUnreachableFlowType';
 
const { types: { namedTypes: types } } = recast;
 
const flowTypes = {
  AnyTypeAnnotation: 'any',
  BooleanTypeAnnotation: 'boolean',
  MixedTypeAnnotation: 'mixed',
  NumberTypeAnnotation: 'number',
  StringTypeAnnotation: 'string',
  VoidTypeAnnotation: 'void',
};
 
const flowLiteralTypes = {
  BooleanLiteralTypeAnnotation: 1,
  NumberLiteralTypeAnnotation: 1,
  StringLiteralTypeAnnotation: 1,
};
 
const namedTypes = {
  GenericTypeAnnotation: handleGenericTypeAnnotation,
  ObjectTypeAnnotation: handleObjectTypeAnnotation,
  UnionTypeAnnotation: handleUnionTypeAnnotation,
  NullableTypeAnnotation: handleNullableTypeAnnotation,
  FunctionTypeAnnotation: handleFunctionTypeAnnotation,
  IntersectionTypeAnnotation: handleIntersectionTypeAnnotation,
  TupleTypeAnnotation: handleTupleTypeAnnotation,
};
 
function getFlowTypeWithRequirements(path: NodePath): FlowTypeDescriptor {
  const type = getFlowType(path);
 
  type.required = !path.parentPath.node.optional;
 
  return type;
}
 
function handleGenericTypeAnnotation(path: NodePath) {
  let type = { name: path.node.id.name };
 
  if (path.node.typeParameters) {
    const params = path.get('typeParameters').get('params');
 
    type = {
      ...type,
      elements: params.map(param => getFlowType(param)),
      raw: printValue(path),
    };
  } else {
    let resolvedPath = resolveToValue(path.get('id'));
 
    if (!isUnreachableFlowType(resolvedPath)) {
      type = getFlowType(resolvedPath.get('right'));
    }
  }
 
  return type;
}
 
function handleObjectTypeAnnotation(path: NodePath) {
  const type = {
    name: 'signature',
    type: 'object',
    raw: printValue(path),
    signature: { properties: [] },
  };
 
  path.get('callProperties').each(param => {
    type.signature.constructor = getFlowType(param.get('value'));
  });
 
  path.get('indexers').each(param => {
    type.signature.properties.push({
      key: getFlowType(param.get('key')),
      value: getFlowTypeWithRequirements(param.get('value')),
    });
  });
 
  path.get('properties').each(param => {
    type.signature.properties.push({
        key: getPropertyName(param),
        value: getFlowTypeWithRequirements(param.get('value')),
    });
  });
 
  return type;
}
 
function handleUnionTypeAnnotation(path: NodePath) {
  return {
    name: 'union',
    raw: printValue(path),
    elements: path.get('types').map(subType => getFlowType(subType)),
  };
}
 
function handleIntersectionTypeAnnotation(path: NodePath) {
  return {
    name: 'intersection',
    raw: printValue(path),
    elements: path.get('types').map(subType => getFlowType(subType)),
  };
}
 
function handleNullableTypeAnnotation(path: NodePath) {
  const typeAnnotation = getTypeAnnotation(path);
 
  Iif (!typeAnnotation) return null;
 
  const type = getFlowType(typeAnnotation);
  type.nullable = true;
 
  return type;
}
 
function handleFunctionTypeAnnotation(path: NodePath) {
  const type = {
    name: 'signature',
    type: 'function',
    raw: printValue(path),
    signature: {
      arguments: [],
      return: getFlowType(path.get('returnType')),
    },
  };
 
  path.get('params').each(param => {
    const typeAnnotation = getTypeAnnotation(param);
    Iif (!typeAnnotation) return null;
 
    type.signature.arguments.push({
      name: getPropertyName(param.get('name')),
      type: getFlowType(typeAnnotation),
    });
  });
 
  return type;
}
 
function handleTupleTypeAnnotation(path: NodePath) {
  const type = { name: 'tuple', raw: printValue(path), elements: [] };
 
  path.get('types').each(param => {
    type.elements.push(getFlowType(param));
  });
 
  return type;
}
 
/**
 * Tries to identify the flow type by inspecting the path for known
 * flow type names. This method doesn't check whether the found type is actually
 * existing. It simply assumes that a match is always valid.
 *
 * If there is no match, "unknown" is returned.
 */
export default function getFlowType(path: NodePath): FlowTypeDescriptor {
  const node = path.node;
  let type: ?FlowTypeDescriptor;
 
  Eif (types.Type.check(node)) {
    if (node.type in flowTypes) {
      type = { name: flowTypes[node.type] };
    } else if (node.type in flowLiteralTypes) {
      type = { name: 'literal', value: node.raw || `${node.value}` };
    } else Eif (node.type in namedTypes) {
      type = namedTypes[node.type](path);
    }
  }
 
  Iif (!type) {
    type = { name: 'unknown' };
  }
 
  return type;
}