# All files / src/tokenizer/comments index.js

100% Statements 27/27
100% Branches 15/15
100% Functions 3/3
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
    57
    58
    59
    60
    61
    62
    63
    64
    65
    28x
    28x
    28x

    28x

    28x
    10306x
    10306x

    10306x
    1784x
    36x

    1784x

    16x
    16x

    16x
    16x

    60x
    60x

    1692x
    1654x
    1654x

    10306x
    10306x
    10306x

    10306x

    38x

    10268x

    10306x

     // @flow
    import token from "../token";
    import Syntax from "../../Syntax";
    import Stream from "../../utils/stream";

    import {
      SLASH,
      SINGLE_LINE,
      MULTI_LINE_START,
      MULTI_LINE_END,
      COMMENT_IDENTIFIERS,
    } from "./constants";

    const parser = char => {
      let isMultiline: boolean = false;
      let isSingleLine: boolean = false;
      let previous: string;

      const isComment = (current: string) => {
        if (Stream.eol(current)) {
          isSingleLine = false;
        }

        switch (`${previous}${current}`) {
          case MULTI_LINE_END: {
            isMultiline = false;
            return isComment;
          }
          case MULTI_LINE_START: {
            isMultiline = true;
            return isComment;
          }
          case SINGLE_LINE: {
            isSingleLine = true;
            return isComment;
          }
          default: {
            if (isMultiline || isSingleLine) {
              previous = current;
              return isComment;
            }
          }
        }
      };

      const maybeComment = (current: string) => {
        const buffer: string = previous;
        previous = current;

        if (
          current === SLASH ||
          isMultiline ||
          COMMENT_IDENTIFIERS.indexOf(`${buffer}${current}`) > -1
        ) {
          return isComment;
        }

        return null;
      };

      return maybeComment(char);
    };

    export default token(parser, Syntax.Comment);