All files / src issue.ts

91.67% Statements 33/36
86.67% Branches 26/30
100% Functions 15/15
91.67% Lines 33/36

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153  1x         1x                       1x           55x 55x 55x 55x                     166x       756x 756x 756x 756x                       1x 1x 1x 1x                     278x 278x                       425x 425x                       279x 279x                       1794x   1794x         1794x   1794x           81x       54118x       74x       13x       416x       181x       11x      
import {IFile} from "./files/_ifile";
import {Position, VirtualPosition} from "./position";
import {Token} from "./abap/1_lexer/tokens/_token";
import {Identifier} from "./abap/4_file_information/_identifier";
import {StatementNode} from "./abap/nodes";
import {IEdit} from "./edit_helper";
import {Severity} from "./severity";
 
interface IIssueData {
  filename: string;
  message: string;
  key: string;
  start: Position;
  end: Position;
  severity: Severity;
  fix?: IEdit;
}
 
export class Issue {
  private readonly data: IIssueData;
 
  //////////////////////////
 
  public static atRow(file: IFile, row: number, message: string, key: string, severity?: Severity) {
    const start = new Position(row, 1);
    const end = new Position(row, file.getRawRows()[row - 1].length + 1);
    severity = severity ?? Severity.Error;
    return new Issue({
      filename: file.getFilename(),
      message,
      key,
      start,
      end,
      severity,
    });
  }
 
  public static atStatement(file: IFile, statement: StatementNode, message: string, key: string, severity?: Severity, fix?: IEdit) {
    return this.atRange(file, statement.getStart(), statement.getEnd(), message, key, severity, fix);
  }
 
  public static atPosition(file: IFile, start: Position, message: string, key: string, severity?: Severity, fix?: IEdit) {
    const row = start.getRow();
    const end = new Position(row, file.getRawRows()[row - 1].length + 1);
    severity = severity ?? Severity.Error;
    return new Issue({
      filename: file.getFilename(),
      message,
      key,
      start,
      end,
      fix,
      severity,
    });
  }
 
  public static atRowRange(file: IFile, row: number, startCol: number, endCol: number, message: string, key: string, severity?: Severity) {
    const start = new Position(row, startCol);
    const end = new Position(row, endCol);
    severity = severity ?? Severity.Error;
    return new Issue({
      filename: file.getFilename(),
      message,
      key,
      start,
      end,
      severity,
    });
  }
 
  public static atRange(file: IFile, start: Position, end: Position, message: string, key: string, severity?: Severity, fix?: IEdit) {
    severity = severity ?? Severity.Error;
    return new Issue({
      filename: file.getFilename(),
      message,
      key,
      start,
      end,
      fix,
      severity,
    });
  }
 
  public static atToken(file: IFile, token: Token, message: string, key: string, severity?: Severity, fix?: IEdit) {
    severity = severity ?? Severity.Error;
    return new Issue({
      filename: file.getFilename(),
      message,
      key,
      start: token.getStart(),
      end: token.getEnd(),
      severity,
      fix,
    });
  }
 
  public static atIdentifier(identifier: Identifier, message: string, key: string, severity?: Severity, fix?: IEdit) {
    severity = severity ?? Severity.Error;
    return new Issue({
      filename: identifier.getFilename(),
      message,
      key,
      start: identifier.getStart(),
      end: identifier.getEnd(),
      severity,
      fix,
    });
  }
 
  private constructor(data: IIssueData) {
    this.data = data;
 
    Iif (this.data.start instanceof VirtualPosition) {
      // no quick fixes inside macros
      this.data.fix = undefined;
    }
 
    Iif (this.data.start.getCol() < 1) {
      throw new Error("issue, start col < 1");
    } else Iif (this.data.end.getCol() < 1) {
      throw new Error("issue, end col < 1");
    }
  }
 
  public getMessage(): string {
    return this.data.message;
  }
 
  public getKey(): string {
    return this.data.key;
  }
 
  public getStart(): Position {
    return this.data.start;
  }
 
  public getEnd(): Position {
    return this.data.end;
  }
 
  public getFilename(): string {
    return this.data.filename;
  }
 
  public getFix() {
    return this.data.fix;
  }
 
  public getSeverity() {
    return this.data.severity;
  }
 
}