# All files / src/generator typecast.js

100% Statements 11/11
100% Branches 0/0
100% Functions 1/1
100% Lines 11/11

    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

    22x
    22x
    22x
    22x
    22x

    22x
    68x
    68x

    68x

    68x
    68x

     // @flow
    //
    import mapSyntax from "./map-syntax";
    import mergeBlock from "./merge-block";
    import { getTypecastOpcode } from "../emitter/opcode";
    import { TYPE_CAST } from "../semantics/metadata";
    import invariant from "invariant";
    import type { GeneratorType } from "./flow/types";

    const generateTypecast: GeneratorType = (node, parent) => {
      const metaTypecast = node.meta[TYPE_CAST];
      invariant(
        metaTypecast,
        `Cannot generate typecast for node: ${JSON.stringify(node)}`
      );

      const { to, from } = metaTypecast;

      const block = node.params.map(mapSyntax(parent)).reduce(mergeBlock, []);
      return [
        ...block,
        {
          kind: getTypecastOpcode(to, from),
          params: [],
        },
      ];
    };

    export default generateTypecast;