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

100% Statements 19/19
87.5% Branches 7/8
100% Functions 2/2
100% Lines 19/19

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

    23x

    23x

    66x
    66x
    66x

    23x
    125x
    125x
    125x
    125x
    66x
    66x
    66x

    59x
    23x

    36x

    36x

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

      // Native method
      if (identifier.Type === Syntax.Type && field.Type === Syntax.FunctionCall) {
        return {
          ...node,
          Type: Syntax.NativeMethod,
          type: identifier.value,
          value: identifier.value + "." + field.value,
          params: field.params,
        };
      }

      const type = identifier.type;

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

    export default mapArraySubscript;