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

100% Statements 27/27
87.5% Branches 7/8
100% Functions 2/2
100% Lines 27/27

    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
    52
    53
    54
    55
    56
    26x
    26x
    26x

    26x
    3x
    3x
    3x
    3x
    9x
    9x
    9x
    9x

    3x
    3x

    26x
    3x
    3x

    3x
    3x

    3x

    3x
    3x
    5x
    5x
    3x

    3x

    3x

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

    const paramList = (ctx: Context): NodeType[] => {
      ctx.expect(["("]);
      const params: NodeType[] = [];
      let node = null;
      while (ctx.token.value && ctx.token.value !== ")") {
        node = expression(ctx);
        Eif (node) {
          params.push(node);
          ctx.eat([";"]);
        }
      }

      ctx.expect([")"]);
      return params;
    };

    const forLoop = (ctx: Context): NodeType => {
      const node = ctx.startNode();
      ctx.eat(["for"]);

      // Pop the last expression from param list to append to the body of the loop.
      // This is important to do here as it'll be more difficult to acomplish later
      // in the generator accurately. In a for-loop we always want the afterthought
      // to follow the entire body, so here we are.
      const [initializer, condition, afterthought] = paramList(ctx);
      const body = [];

      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, afterthought],
        },
        Syntax.Loop
      );
    };

    export default forLoop;