All files / src/rules contains_tab.ts

97.14% Statements 68/70
91.66% Branches 11/12
100% Functions 8/8
97.14% Lines 68/70

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 701x 1x 1x 1x 1x 1x 1x 1x 22292x 22292x 22292x 22292x 1x 11147x 11147x 11147x 11147x 11147x 33250x 33250x 33250x 33250x 33250x 33250x 33250x 33250x 33250x 33250x 33250x 33250x 11147x 11147x 7x 7x 11147x 11147x 10590x     10590x 10590x 11147x 11147x 266x 266x 11147x 11147x 288x 288x 288x 288x 1931x 1931x 7x 7x 12x 12x 7x 7x 288x 288x 288x 11147x 11147x 7x 7x 7x 7x 7x 11147x
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],
      badExample: `\tWRITE 'hello world'.`,
      goodExample: `  WRITE 'hello world'.`,
    };
  }
 
  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);
  }
}