All files / src/rules max_one_statement.ts

100% Statements 75/75
100% Branches 17/17
100% Functions 6/6
100% Lines 75/75

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 73 74 751x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7654x 7654x 7654x 7654x 7654x 22826x 22826x 22826x 22826x 22826x 22826x 22826x 22826x 22826x 22826x 22826x 22826x 22826x 22826x 7654x 7654x 11x 11x 7654x 7654x 7346x 7346x 7654x 7654x 139x 139x 7654x 7654x 156x 156x 156x 156x 156x 858x 858x 858x 858x 44x 44x 814x 814x 858x 1x 1x 813x 858x 11x 11x 11x 11x 11x 813x 813x 156x 156x 156x 7654x 7654x
import {Issue} from "../issue";
import {Comment, NativeSQL} from "../abap/2_statements/statements/_statement";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {EditHelper} from "../edit_helper";
import {IRuleMetadata, RuleTag} from "./_irule";
import {VirtualPosition} from "../position";
import {ABAPFile} from "../abap/abap_file";
 
export class MaxOneStatementConf extends BasicRuleConfig {
}
 
export class MaxOneStatement extends ABAPRule {
 
  private conf = new MaxOneStatementConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "max_one_statement",
      title: "Max one statement per line",
      shortDescription: `Checks that each line contains only a single statement.`,
      extendedInformation:
`Does not report empty statements, use rule empty_statement for detecting empty statements.
 
https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#no-more-than-one-statement-per-line
https://docs.abapopenchecks.org/checks/11/`,
      tags: [RuleTag.Styleguide, RuleTag.Quickfix, RuleTag.SingleFile],
      badExample: `WRITE foo. WRITE bar.`,
      goodExample: `WRITE foo.\nWRITE bar.`,
    };
  }
 
  private getMessage(): string {
    return "Only one statement is allowed per line";
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: MaxOneStatementConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    let prev: number = 0;
    let reported: number = 0;
    for (const statement of file.getStatements()) {
      const term = statement.getTerminator();
      if (statement.get() instanceof Comment
          || statement.get() instanceof NativeSQL
          || term === ",") {
        continue;
      }
 
      const pos = statement.getStart();
      if (pos instanceof VirtualPosition) {
        continue;
      }
      const row = pos.getRow();
      if (prev === row && row !== reported && statement.getFirstToken().getStr() !== ".") {
        const fix = EditHelper.insertAt(file, pos, "\n");
        const issue = Issue.atPosition(file, pos, this.getMessage(), this.getMetadata().key, this.conf.severity, fix);
        issues.push(issue);
        reported = row;
      }
      prev = row;
    }
 
    return issues;
  }
 
}