# All files / src/core native.js

100% Statements 12/12
100% Branches 5/5
100% Functions 4/4
100% Lines 12/12

    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

    23x
    23x

    81x

    81x
    81x
    276x
    276x
    276x

    32x

    32x

    46x

    244x

     /**
     * Native methods plugin
     *
     * @flow
     */
    import Syntax from 'walt-syntax';
    import { extendNode } from '../utils/extend-node';
    import type { SemanticPlugin } from '../flow/types';

    export default function nativePlugin(): SemanticPlugin {
      return {
        semantics() {
          return {
            [Syntax.FunctionCall]: next => (args, transform) => {
              const [node, context] = args;
              const [id, ...fnArgs] = node.params;
              if (
                id.Type === Syntax.ArraySubscript &&
                id.params[0] &&
                id.params[0].Type === Syntax.Type
              ) {
                const [type, method] = id.params;

                return extendNode(
                  {
                    value: `${type.value}.${method.value}`,
                    type: type.value,
                    params: fnArgs.map(p => transform([p, context])),
                    Type: Syntax.NativeMethod,
                  },
                  node
                );
              }

              return next(args);
            },
          };
        },
      };
    }