All files / src/rules prefer_xsdbool.ts

96.66% Statements 58/60
84.61% Branches 11/13
100% Functions 5/5
96.66% Lines 58/60

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 621x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11286x 11286x 11286x 11286x 11286x 33700x 33700x 33700x 33700x 33700x 33700x 33700x 33700x 33700x 33700x 33700x 33700x 11286x 11286x 10794x 10794x 11286x 11286x 230x 230x 11286x 11286x 245x 245x 245x     245x 245x 37x 35x 35x 2x 2x 2x 2x 2x 2x 245x 245x 245x 11286x 11286x    
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import * as Expressions from "../abap/2_statements/expressions";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {Version} from "../version";
import {EditHelper} from "../edit_helper";
import {ABAPFile} from "../abap/abap_file";
 
export class PreferXsdboolConf extends BasicRuleConfig {
}
 
export class PreferXsdbool extends ABAPRule {
 
  private conf = new PreferXsdboolConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "prefer_xsdbool",
      title: "Prefer xsdbool over boolc",
      shortDescription: `Prefer xsdbool over boolc`,
      extendedInformation: `Activates if language version is v740sp08 or above.
 
https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#use-xsdbool-to-set-boolean-variables`,
      tags: [RuleTag.Styleguide, RuleTag.Upport, RuleTag.Quickfix, RuleTag.SingleFile],
      badExample: `DATA(sdf) = boolc( 1 = 2 ).`,
      goodExample: `DATA(sdf) = xsdbool( 1 = 2 ).`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: PreferXsdboolConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile): Issue[] {
    const issues: Issue[] = [];
 
    if (this.reg.getConfig().getVersion() < Version.v740sp08 && this.reg.getConfig().getVersion() !== Version.Cloud) {
      return [];
    }
 
    for (const s of file.getStructure()?.findAllExpressions(Expressions.Source) || []) {
      if (s.concatTokens().toUpperCase().startsWith("BOOLC( ") === false) {
        continue;
      }
      const token = s.getFirstToken();
 
      const message = "Prefer xsdbool over boolc";
      const fix = EditHelper.replaceToken(file, token, "xsdbool");
      issues.push(Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity, fix));
    }
 
    return issues;
  }
 
}