# All files / src/parser while-loop.js

100% Statements 19/19
100% Branches 4/4
100% Functions 1/1
100% Lines 19/19

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

    26x
    26x

    26x
    5x
    5x
    5x

    5x
    5x
    5x

    5x
    5x

    5x
    5x
    30x
    30x
    17x

    5x

    5x

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

    const whileLoop = (ctx: Context): NodeType => {
      const node = ctx.startNode();
      ctx.eat(["while"]);
      ctx.expect(["("]);

      const initializer = ctx.makeNode({}, Syntax.Noop);
      const condition = expression(ctx);
      const body = [];

      ctx.expect([")"]);
      ctx.expect(["{"]);

      let stmt = null;
      while (ctx.token && ctx.token.value !== "}") {
        stmt = statement(ctx);
        if (stmt) {
          body.push(stmt);
        }
      }

      ctx.expect(["}"]);

      return ctx.endNode(
        {
          ...node,
          params: [initializer, condition, ...body],
        },
        Syntax.Loop
      );
    };

    export default whileLoop;