All files / src/rules omit_preceding_zeros.ts

92.85% Statements 65/70
68.42% Branches 13/19
100% Functions 5/5
92.85% Lines 65/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 1x 1x 1x 1x 15471x 15471x 15471x 15471x 15471x 46277x 46277x 46277x 46277x 46277x 46277x 46277x 46277x 46277x 15471x 15471x 14912x 14912x 15471x 15471x 264x 264x 15471x 15471x 307x 307x 307x 307x 1663x 59x 59x 59x 6x 2x 2x 4x 4x 4x 4x 4x 59x 1663x 1663x 4x 4x 4x           4x 1663x 307x 307x 307x 15471x 15471x
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";
import {EditHelper} from "../edit_helper";
 
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, RuleTag.Quickfix],
      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[] = [];
    const message = "Omit preceding zeros";
 
    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 replace = str.replace(/^0+/, "");
          const fix = EditHelper.replaceRange(file, token.getStart(), token.getEnd(), replace);
          const issue = Issue.atToken(file, token, message, this.getMetadata().key, this.getConfig().severity, fix);
          issues.push(issue);
        }
      }
 
      for (const i of s.findAllExpressions(Expressions.ParameterException)) {
        const token = i.findDirectExpression(Expressions.SimpleName)?.getFirstToken();
        const str = token?.getStr();
        if (token && str && str.length > 1 && str.startsWith("0")) {
          const replace = str.replace(/^0+/, "");
          const fix = EditHelper.replaceRange(file, token.getStart(), token.getEnd(), replace);
          const issue = Issue.atToken(file, token, message, this.getMetadata().key, this.getConfig().severity, fix);
          issues.push(issue);
        }
      }
    }
 
    return issues;
  }
 
}