All files / src/plugins LogParserErrorsPlugin.ts

20.83% Statements 5/24
0% Branches 0/15
50% Functions 1/2
20.83% Lines 5/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 431x         1x 1x 1x                                                               1x      
import { IArgdownPlugin, IRequestHandler, TokenNames } from "@argdown/core";
import { IFileRequest } from "../IFileRequest";
export interface ILogParserErrorsRequest {
  logParserErrors?: boolean;
}
export class LogParserErrorsPlugin implements IArgdownPlugin {
  name = "LogParserErrorsPlugin";
  run: IRequestHandler = (request, response, logger) => {
    if (!(<ILogParserErrorsRequest>request).logParserErrors) {
      return;
    }
    const fileRequest = <IFileRequest>request;
    if (response.parserErrors && response.parserErrors.length > 0) {
      const inputFile = fileRequest.inputPath;
      const nrOfErrors = response.parserErrors.length;
      if (inputFile) {
        logger.log("error", `\u001b[31m\u001b[1mArgdown syntax errors in ${inputFile}: ${nrOfErrors}\u001b[0m\n`);
      } else {
        logger.log("error", `\u001b[31m\u001b[1mArgdown syntax errors in input: ${nrOfErrors}\u001b[0m\n`);
      }
      for (let error of response.parserErrors) {
        const message = error.message;
        var startLine, startColumn;
        if (error.token && error.token.tokenType && error.token.tokenType.tokenName === TokenNames.EOF) {
          // EOF does not have a token location, but the error saves the previousToken parsed
          if ((<any>error).previousToken) {
            startLine = (<any>error).previousToken.startLine;
            startColumn = (<any>error).previousToken.startColumn;
          }
        } else {
          startLine = error.token.startLine;
          startColumn = error.token.startColumn;
        }
        logger.log("error", `\u001b[31mAt ${startLine}:${startColumn}\u001b[0m\n${message}\n`);
      }
    }
    return response;
  };
}
module.exports = {
  LogParserErrorsPlugin: LogParserErrorsPlugin
};