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

100% Statements 17/17
75% Branches 3/4
100% Functions 2/2
100% Lines 17/17

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

    21x

    21x

    30x
    30x
    30x

    21x
    62x
    62x
    62x
    62x
    30x
    30x
    30x

    32x

    32x

     // @flow
    import Syntax from "../../Syntax";
    import curry from "curry";
    import type { NodeType } from "../../flow/types";
    import { TYPE_OBJECT, OBJECT_KEY_TYPES, ALIAS } from "../metadata";

    const patchStringSubscript = (
      byteOffsetsByKey: any,
      params: NodeType[]
    ): NodeType[] => {
      const field = params[1];
      const absoluteByteOffset = byteOffsetsByKey[field.value];
      return [
        params[0],
        {
          ...field,
          meta: { [ALIAS]: field.value },
          value: absoluteByteOffset,
          type: "i32",
          Type: Syntax.Constant,
        },
      ];
    };

    const mapArraySubscript = curry(({ userTypes }, node, mapChildren) => {
      const params = node.params.map(mapChildren);
      const [identifier, field] = params;
      const userType = userTypes[identifier.type];
      if (userType != null) {
        const metaObject = userType.meta[TYPE_OBJECT];
        const objectKeyTypeMap = userType.meta[OBJECT_KEY_TYPES];
        return {
          ...node,
          type: objectKeyTypeMap ? objectKeyTypeMap[field.value] : "i32",
          params: patchStringSubscript(metaObject, params),
        };
      }

      const type = identifier.type;

      return {
        ...node,
        type,
        params,
      };
    });

    export default mapArraySubscript;