All files / src/rules 7bit_ascii.ts

100% Statements 73/73
100% Branches 11/11
100% Functions 6/6
100% Lines 73/73

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 731x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7653x 7653x 7653x 7653x 22816x 22816x 22816x 22816x 22816x 22816x 22816x 22816x 7653x 7653x 146x 146x 7653x 7653x 7346x 7346x 7653x 7653x 140x 140x 7653x 7653x 172x 172x 172x 180x 156x 156x 156x 1020x 1020x 1x 1x 1x 1x 1x 1x 1x 1020x 1020x 1020x 1020x 1020x 1x 1x 1x 1x 1x 1x 1x 1020x 1020x 156x 180x 172x 172x 172x 7653x 7653x
import {Issue} from "../issue";
import {Position} from "../position";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IObject} from "../objects/_iobject";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {IRegistry} from "../_iregistry";
 
export class SevenBitAsciiConf extends BasicRuleConfig {
}
 
export class SevenBitAscii implements IRule {
  private conf = new SevenBitAsciiConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "7bit_ascii",
      title: "Check for 7bit ascii",
      shortDescription: `Only allow characters from the 7bit ASCII set.`,
      extendedInformation: `https://docs.abapopenchecks.org/checks/05/`,
      tags: [RuleTag.SingleFile],
    };
  }
 
  public initialize(_reg: IRegistry) {
    return this;
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: SevenBitAsciiConf) {
    this.conf = conf;
  }
 
  public run(obj: IObject): Issue[] {
    const output: Issue[] = [];
 
    for (const file of obj.getFiles()) {
      if (file.getFilename().endsWith(".abap")) {
        const rows = file.getRawRows();
 
        for (let i = 0; i < rows.length; i++) {
          const found = /[\u007f-\uffff]/.exec(rows[i]);
          if (found !== null) {
            const column = found.index + 1;
            const start = new Position(i + 1, column);
            const end = new Position(i + 1, column + 1);
            const message = "Contains non 7 bit ascii character";
            const issue = Issue.atRange(file, start, end, message, this.getMetadata().key, this.conf.severity);
            output.push(issue);
          }
 
          // method getRawRows() splits by newline, so the carraige return
          // should always be last character if present
          const carriage = /\r.+$/.exec(rows[i]);
          if (carriage !== null) {
            const column = carriage.index + 1;
            const start = new Position(i + 1, column);
            const end = new Position(i + 1, column + 1);
            const message = "Dangling carriage return";
            const issue = Issue.atRange(file, start, end, message, this.getMetadata().key, this.conf.severity);
            output.push(issue);
          }
 
        }
      }
    }
 
    return output;
  }
 
}