# 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
    28x
    28x

    28x
    127x
    127x

    127x
    76x

    127x

    127x
    127x

    127x

    127x
    13x

    127x

    127x
    5x
    5x
    5x
    5x
    122x
    114x

    127x

     // @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;