All files / src/rules contains_tab.ts

96.67% Statements 29/30
75% Branches 3/4
100% Functions 9/9
96.67% Lines 29/30

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   14540x     1x   7271x     21682x                       7x       7020x     7020x       114x       140x   140x 140x 874x 874x 7x 7x 12x   7x     140x       7x 7x 7x 7x    
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() {
    Iif (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);
  }
}