# 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

    858x
    19353x
    501x

    18852x
    18852x
    5x

    18847x
    2791x

    16056x

    18852x
    1642x

    17210x
    17210x

    17210x

    858x

     // @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;
    }