All files / src/rules max_one_statement.ts

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

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 75 76 771x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12074x 12074x 12074x 12074x 12074x 105283x 105283x 105283x 105283x 105283x 105283x 105283x 105283x 105283x 105283x 105283x 105283x 105283x 105283x 105283x 105283x 12074x 12074x 12x 12x 12074x 12074x 11532x 11532x 12074x 12074x 255x 255x 12074x 12074x 282x 282x 282x 282x 282x 1607x 1607x 1607x 1607x 141x 141x 1466x 1466x 1607x 1x 1x 1465x 1607x 12x 12x 12x 12x 12x 1465x 1465x 282x 282x 282x 12074x 12074x
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 "../virtual_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.
 
Does not report anything for chained 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;
  }
 
}