All files / src/rules omit_preceding_zeros.ts

100% Statements 70/70
87.5% Branches 14/16
100% Functions 5/5
100% Lines 70/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 10328x 10328x 10328x 10328x 10328x 30815x 30815x 30815x 30815x 30815x 30815x 30815x 30815x 30815x 10328x 10328x 9818x 9818x 10328x 10328x 241x 241x 10328x 10328x 268x 268x 268x 268x 1454x 49x 49x 49x 4x 2x 2x 2x 2x 2x 2x 2x 49x 1454x 1454x 4x 4x 4x 2x 2x 2x 2x 2x 4x 1454x 268x 268x 268x 10328x 10328x
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;
  }
 
}