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

    26x
    165x
    165x

    165x
    104x

    165x

    165x
    165x

    165x

    165x
    19x

    165x

    165x
    4x
    4x
    4x
    4x
    161x
    153x

    165x

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