All files / src/rules omit_preceding_zeros.ts

100% Statements 56/56
100% Branches 11/11
100% Functions 5/5
100% Lines 56/56

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 561x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9361x 9361x 9361x 9361x 9361x 27925x 27925x 27925x 27925x 27925x 27925x 27925x 27925x 27925x 9361x 9361x 8900x 8900x 9361x 9361x 216x 216x 9361x 9361x 243x 243x 243x 1357x 45x 45x 45x 4x 2x 2x 2x 2x 2x 2x 45x 1357x 243x 243x 243x 9361x 9361x
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Issue} from "../issue";
import * as Expressions from "../abap/2_statements/expressions";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {CallScreen, SetScreen} from "../abap/2_statements/statements";
 
export class OmitPrecedingZerosConf extends BasicRuleConfig {
}
 
export class OmitPrecedingZeros extends ABAPRule {
 
  private conf = new OmitPrecedingZerosConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "omit_preceding_zeros",
      title: "Omit preceding zeros",
      shortDescription: `Omit preceding zeros from integer constants`,
      tags: [RuleTag.SingleFile],
      badExample: `int = -001.`,
      goodExample: `int = -1.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: OmitPrecedingZerosConf): void {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    for (const s of file.getStatements()) {
      for (const i of s.findAllExpressions(Expressions.Integer)) {
        const token = i.getLastToken();
        const str = token.getStr();
        if (str.length > 1 && str.startsWith("0")) {
          if (s.get() instanceof CallScreen || s.get() instanceof SetScreen) {
            continue;
          }
          const message = "Omit preceding zeros";
          const issue = Issue.atToken(file, token, message, this.getMetadata().key, this.getConfig().severity);
          issues.push(issue);
        }
      }
    }
 
    return issues;
  }
 
}