All files / src/core native.js

100% Statements 14/14
100% Branches 5/5
100% Functions 6/6
100% Lines 14/14
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          23x 23x       90x   90x 90x 685x 685x 685x         425x   425x       497x             260x   90x 6x                          
/**
 * 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.Access &&
            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);
        },
        [Syntax.Unreachable]: _ => ([node]) => {
          return extendNode(
            {
              value: 'unreachable',
              params: [],
              Type: Syntax.NativeMethod,
            },
            node
          );
        },
      };
    },
  };
}