# All files / src/parser declaration.js

100% Statements 22/22
100% Branches 8/8
100% Functions 1/1
100% Lines 22/22

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

    27x
    179x
    179x

    179x
    115x

    179x

    179x
    179x

    179x

    179x
    19x

    179x

    179x
    4x
    4x
    4x
    4x
    175x
    167x

    179x

     // @flow
    import Syntax from '../Syntax';
    import expression from './expression';
    import type Context from './context';
    import type { NodeType } from '../flow/types';

    const declaration = (ctx: Context): NodeType => {
      const node = ctx.startNode();
      let Type = Syntax.Declaration;

      if (ctx.token.value === 'const') {
        Type = Syntax.ImmutableDeclaration;
      }

      ctx.eat(['const', 'let', 'function']);

      node.value = ctx.expect(null, Syntax.Identifier).value;
      ctx.expect([':']);

      let type = ctx.token.value;

      if (!ctx.eat(null, Syntax.Type)) {
        ctx.expect(null, Syntax.Identifier);
      }

      const params = [];
      // Parse generic
      if (ctx.eat(['<'])) {
        ctx.eat(['{']);
        params.push(expression(ctx));
        ctx.eat(['}']);
        ctx.eat(['>']);
      } else if (ctx.eat(['='])) {
        params.push(expression(ctx));
      }

      return ctx.endNode({ ...node, params, type }, Type);
    };

    export default declaration;