# All files / src/semantics/map-function map-pair.js

100% Statements 9/9
100% Branches 5/5
100% Functions 1/1
100% Lines 9/9

    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
    21x
    21x
    21x

    28x

    28x
    28x

    28x
    26x

    2x

     // @flow
    import curry from "curry";
    import Syntax from "../../Syntax";
    import { TYPE_CAST } from "../metadata";
    import type { NodeType } from "../../flow/types";

    export default curry(
      (options, typeCastMaybe: NodeType, transform): NodeType => {
        const [targetNode, typeNode] = typeCastMaybe.params.map(transform);

        const { type: from } = targetNode;
        const { value: to } = typeNode;

        // If both sides of a pair don't have types then it's not a typecast,
        // more likely a string: value pair in an object for example
        if (typeNode.Type === Syntax.Type && !!from && !!to) {
          return {
            ...typeCastMaybe,
            type: to,
            value: targetNode.value,
            Type: Syntax.TypeCast,
            meta: { ...typeCastMaybe.meta, [TYPE_CAST]: { to, from } },
            // We need to drop the typeNode here, because it's not something we can generate
            params: [targetNode],
          };
        }

        return {
          ...typeCastMaybe,
          params: typeCastMaybe.params.map(transform),
        };
      }
    );