# All files / src/utils map-node.js

100% Statements 16/16
100% Branches 12/12
100% Functions 4/4
100% Lines 15/15

    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

    675x
    11082x
    395x

    10687x
    10687x
    5x

    10682x
    1770x

    8912x

    10687x
    1005x

    9682x
    9682x

    9682x

    675x

     // @flow
    import type { NodeType } from "../flow/types";

    type WalkerType = (node: NodeType, childMapper: any) => NodeType;
    type VisitorType = { [string]: WalkerType };

    export default function mapNode(
      visitor: VisitorType
    ): (node: NodeType) => NodeType {
      const nodeMapper = (node: NodeType): NodeType => {
        if (node == null) {
          return node;
        }

        const mappingFunction: WalkerType = (() => {
          if ("*" in visitor && typeof visitor["*"] === "function") {
            return visitor["*"];
          }

          if (node.Type in visitor && typeof visitor[node.Type] === "function") {
            return visitor[node.Type];
          }
          return identity => identity;
        })();

        if (mappingFunction.length === 2) {
          return mappingFunction(node, nodeMapper);
        }

        const mappedNode = mappingFunction(node);
        const params = mappedNode.params.map(nodeMapper);

        return {
          ...mappedNode,
          params,
        };
      };

      return nodeMapper;
    }