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

    26x

    26x
    14874x
    14874x

    14874x
    3917x
    67x

    3917x

    16x
    16x

    16x
    16x

    123x
    123x

    3762x
    3693x
    3693x

    14874x
    14874x
    14874x

    14874x

    69x

    14805x

    14874x

     // @flow
    import token from "../token";
    import Syntax from "../../Syntax";
    import { eol } 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 (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);