All files / src/rules space_before_colon.ts

96.77% Statements 60/62
91.67% Branches 11/12
100% Functions 6/6
96.77% Lines 60/62

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 621x 1x 1x 1x 1x 1x 1x 1x 1x 7653x 7653x 7653x 7653x 7653x 22815x 22815x 22815x 22815x 22815x 22815x 22815x 22815x 22815x 22815x 7653x 7653x 1x 1x 7653x 7653x 7346x 7346x 7653x 7653x 139x 139x 7653x 7653x 152x 152x 152x 152x 152x 3386x     3386x 3386x 3386x 1x 1x 1x 1x 1x 1x 3386x 3386x 152x 152x 152x 7653x 7653x
import {ABAPFile} from "../abap/abap_file";
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
 
export class SpaceBeforeColonConf extends BasicRuleConfig {
}
 
export class SpaceBeforeColon extends ABAPRule {
 
  private conf = new SpaceBeforeColonConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "space_before_colon",
      title: "Space before colon",
      shortDescription: `Checks that there are no spaces in front of colons in chained statements.`,
      extendedInformation: `https://docs.abapopenchecks.org/checks/80/`,
      tags: [RuleTag.Whitespace, RuleTag.SingleFile],
      badExample: `DATA : foo TYPE string.`,
      goodExample: `DATA: foo TYPE string.`,
    };
  }
 
  private getMessage(): string {
    return "Remove space before colon";
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: SpaceBeforeColonConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    let prev = file.getTokens()[0];
 
    for (const token of file.getTokens()) {
      if (token.getStr() === ":" && !prev) {
        const issue = Issue.atToken(file, token, this.getMessage(), this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      } else if (token.getStr() === ":"
          && prev.getRow() === token.getRow()
          && prev.getCol() + prev.getStr().length < token.getCol()) {
        const issue = Issue.atRowRange(file, token.getRow(),
                                       prev.getEnd().getCol(), token.getStart().getCol(),
                                       this.getMessage(), this.getMetadata().key, this.conf.severity);
//        const issue = Issue.atToken(file, token, this.getMessage(), this.getMetadata().key);
        issues.push(issue);
      }
      prev = token;
    }
 
    return issues;
  }
 
}