All files / src issue.ts

95.78% Statements 182/190
84.37% Branches 27/32
94.44% Functions 17/18
95.78% Lines 182/190

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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 1901x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 1x 1x 346x 346x 346x 1x 1x 1198x 1198x 1198x 1198x 1198x 1198x 1198x 1198x 1198x 1198x 1198x 1198x 1198x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 480x 1x 1x 953x 953x 953x 953x 953x 953x 953x 953x 953x 953x 953x 1x 1x 494x 494x 494x 494x 494x 494x 494x 494x 494x 494x 494x 1x 1x 492x 492x 492x 492x 492x 492x 492x 492x 492x 492x 492x 1x 1x 3723x 3723x 3723x       3723x 3723x   3723x     3723x 1x 1x 1x 1x 1x 1x 274x 274x 1x 1x 991x 991x 1x 1x 22x 22x 1x 1x 18x 18x 1x 1x 20x 20x 1x 1x 472x 472x 1x 1x     1x 1x 15x 15x 1x 1x
import {IFile} from "./files/_ifile";
import {Position} from "./position";
import {VirtualPosition} from "./virtual_position";
import {AbstractToken} from "./abap/1_lexer/tokens/abstract_token";
import {Identifier} from "./abap/4_file_information/_identifier";
import {StatementNode} from "./abap/nodes";
import {IEdit} from "./edit_helper";
import {Severity} from "./severity";
 
type Fix = {
  description: string,
  edit: IEdit,
};
 
interface IIssueData {
  filename: string;
  message: string;
  key: string;
  start: Position;
  end: Position;
  severity: Severity;
  /** The default fix for the issue, it always makes sense to apply this fix
   *  These are applied by "abaplint --fix" and in the document formatter in vscode extension
   */
  defaultFix?: IEdit;
  /** Alternative quick fixes, the developer must choose which to apply */
  alternativeFixes?: Fix[];
}
 
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, alternativeFixes?: Fix[]) {
    return this.atRange(file, statement.getStart(), statement.getEnd(), message, key, severity, fix, alternativeFixes);
  }
 
  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,
      defaultFix: fix,
      severity,
    });
  }
 
  public static atRowRange(file: IFile, row: number, startCol: number,
                           endCol: number, message: string, key: string, severity?: Severity, fix?: IEdit) {
    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,
      defaultFix: fix,
      severity,
    });
  }
 
  public static atRange(file: IFile, start: Position, end: Position, message: string, key: string,
                        severity?: Severity, fix?: IEdit, alternativeFixes?: Fix[]) {
    severity = severity ?? Severity.Error;
    return new Issue({
      filename: file.getFilename(),
      message,
      key,
      start,
      end,
      defaultFix: fix,
      severity,
      alternativeFixes,
    });
  }
 
  public static atToken(file: IFile, token: AbstractToken, 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,
      defaultFix: fix,
    });
  }
 
  public static atTokenFilename(filename: string, token: AbstractToken, message: string, key: string, severity?: Severity, fix?: IEdit) {
    severity = severity ?? Severity.Error;
    return new Issue({
      filename: filename,
      message,
      key,
      start: token.getStart(),
      end: token.getEnd(),
      severity,
      defaultFix: 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,
      defaultFix: fix,
    });
  }
 
  public constructor(data: IIssueData) {
    this.data = data;
 
    if (this.data.start instanceof VirtualPosition) {
      // no quick fixes inside macros
      this.data.defaultFix = undefined;
    }
 
    if (this.data.start.getCol() < 1) {
      throw new Error("issue, start col < 1");
    } else if (this.data.end.getCol() < 1) {
      throw new Error("issue, end col < 1");
    }
  }
 
  public getData(): IIssueData {
    return this.data;
  }
 
  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 getDefaultFix() {
    return this.data.defaultFix;
  }
 
  public getAlternativeFixes() {
    return this.data.alternativeFixes;
  }
 
  public getSeverity() {
    return this.data.severity;
  }
 
}