All files / src/rules contains_tab.ts

97.06% Statements 66/68
91.67% Branches 11/12
100% Functions 8/8
97.06% Lines 66/68

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 681x 1x 1x 1x 1x 1x 1x 1x 15306x 15306x 15306x 15306x 1x 7654x 7654x 7654x 7654x 7654x 22822x 22822x 22822x 22822x 22822x 22822x 22822x 22822x 22822x 22822x 7654x 7654x 7x 7x 7654x 7654x 7353x     7353x 7353x 7654x 7654x 139x 139x 7654x 7654x 161x 161x 161x 161x 1023x 1023x 7x 7x 12x 12x 7x 7x 161x 161x 161x 7654x 7654x 7x 7x 7x 7x 7x 7654x
import {Issue} from "../issue";
import {Position} from "../position";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {EditHelper} from "../edit_helper";
import {ABAPFile} from "../abap/abap_file";
 
export class ContainsTabConf extends BasicRuleConfig {
  /** quick fix replace with number of spaces */
  public spaces: number = 1;
}
 
export class ContainsTab extends ABAPRule {
 
  private conf = new ContainsTabConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "contains_tab",
      title: "Code contains tab",
      shortDescription: `Checks for usage of tabs (enable to enforce spaces)`,
      extendedInformation: `
https://docs.abapopenchecks.org/checks/09/
https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#indent-and-snap-to-tab`,
      tags: [RuleTag.Whitespace, RuleTag.Quickfix, RuleTag.Styleguide, RuleTag.SingleFile],
    };
  }
 
  private getMessage(): string {
    return "Code should not contain tabs";
  }
 
  public getConfig() {
    if (this.conf.spaces === undefined) {
      this.conf.spaces = 1;
    }
    return this.conf;
  }
 
  public setConfig(conf: ContainsTabConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    const lines = file.getRaw().split("\n");
    lines.forEach((_, i) => {
      const tabCol = lines[i].indexOf("\t");
      if (tabCol >= 0) {
        let tabAmount = 1;
        while(lines[i].indexOf("\t", tabCol + tabAmount - 1) >= 0){
          tabAmount++;
        }
        issues.push(this.createIssue(i, tabCol, tabAmount, file));
      }
    });
    return issues;
  }
 
  private createIssue(line: number, tabCol: number, tabAmount: number, file: ABAPFile) {
    const tabStartPos = new Position(line + 1, tabCol + 1);
    const tabEndPos = new Position(line + 1, tabCol + tabAmount);
    const fix = EditHelper.replaceRange(file, tabStartPos, tabEndPos, " ".repeat(this.getConfig().spaces));
    return Issue.atRange(file, tabStartPos, tabEndPos, this.getMessage(), this.getMetadata().key, this.conf.severity, fix);
  }
}