# All files / src/parser string-literal.js

100% Statements 23/23
100% Branches 13/13
100% Functions 1/1
100% Lines 23/23

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

    107x
    107x

    107x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x

    107x

    107x

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

    // Note: string literal does not increment the token.
    function stringLiteral(ctx: Context): NodeType {
      const node = ctx.startNode();
      node.value = ctx.token.value.substring(1, ctx.token.value.length - 1);

      // Replace escape sequences
      switch (node.value) {
        case "\\b":
          node.value = "\b";
          break;
        case "\\f":
          node.value = "\f";
          break;
        case "\\n":
          node.value = "\n";
          break;
        case "\\r":
          node.value = "\r";
          break;
        case "\\t":
          node.value = "\t";
          break;
        case "\\v":
          node.value = "\v";
          break;
        case "\\0":
          node.value = "\0";
          break;
        case "\\'":
          node.value = "'";
          break;
        case '\\"':
          node.value = '"';
      }

      const Type =
        ctx.token.value[0] === "'" && Array.from(node.value).length === 1
          ? Syntax.CharacterLiteral
          : Syntax.StringLiteral;
      return ctx.endNode(node, Type);
    }

    export default stringLiteral;