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

100% Statements 23/23
83.33% Branches 5/6
100% Functions 5/5
100% Lines 23/23

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

    21x

    45x

    45x
    6x
    6x

    6x

    2x

    7x
    7x

    1x
    1x
    1x
    1x
    1x

    1x
    3x

    3x

    6x
    6x

    39x

     // @flow
    import Syntax from "../../Syntax";
    import curry from "curry";
    import { TYPE_OBJECT } from "../metadata";

    import walkNode from "../../utils/walk-node";

    export default curry(function mapAssignment(options, node, mapChildren) {
      const [lhs, rhs] = node.params;

      // id = { <param>: <value> };
      if (rhs && rhs.Type === Syntax.ObjectLiteral) {
        const individualKeys = {};
        const spreadKeys = {};
        // We have to walk the nodes twice, once for regular prop keys and then again
        // for ...(spread)
        walkNode({
          // Top level Identifiers _inside_ an object literal === shorthand
          // Notice that we ignore chld mappers in both Pairs and Spread(s) so the
          // only way this is hit is if the identifier is TOP LEVEL
          [Syntax.Identifier]: (identifier, _) => {
            individualKeys[identifier.value] = {
              ...lhs,
              Type: Syntax.MemoryAssignment,
              params: [
                { ...lhs, Type: Syntax.ArraySubscript, params: [lhs, identifier] },
                identifier,
              ],
            };
          },
          [Syntax.Pair]: (pair, _) => {
            const [property, value] = pair.params;
            individualKeys[property.value] = {
              ...lhs,
              Type: Syntax.MemoryAssignment,
              params: [
                { ...lhs, Type: Syntax.ArraySubscript, params: [lhs, property] },
                value,
              ],
            };
          },
          [Syntax.Spread]: (spread, _) => {
            // find userType
            const { locals, userTypes } = options;
            const [target] = spread.params;
            const userType = userTypes[locals[target.value].type];
            const keyOffsetMap = userType.meta[TYPE_OBJECT];
            Eif (keyOffsetMap != null) {
              // map over the keys
              Object.keys(keyOffsetMap).forEach(key => {
                const offsetNode = {
                  ...target,
                  Type: Syntax.Identifier,
                  value: key,
                  params: [],
                };
                // profit
                spreadKeys[key] = {
                  ...lhs,
                  Type: Syntax.MemoryAssignment,
                  params: [
                    {
                      ...lhs,
                      Type: Syntax.ArraySubscript,
                      params: [lhs, { ...offsetNode }],
                    },
                    {
                      ...target,
                      Type: Syntax.ArraySubscript,
                      params: [
                        target,
                        {
                          ...offsetNode,
                        },
                      ],
                    },
                  ],
                };
              });
            }
          },
        })(rhs);

        const params = Object.values({ ...spreadKeys, ...individualKeys });
        return {
          ...lhs,
          Type: Syntax.Block,
          // We just created a bunch of MemoryAssignment nodes, map over them so that
          // the correct metadata is applied to everything
          params: params.map(mapChildren),
        };
      }

      return {
        ...node,
        params: node.params.map(mapChildren),
      };
    });