All files / src/rules use_bool_expression.ts

96.96% Statements 128/132
77.27% Branches 51/66
100% Functions 5/5
96.96% Lines 128/132

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 1321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10325x 10325x 10325x 10325x 30811x 30811x 30811x 30811x 30811x 30811x 30811x 30811x 30811x 30811x 30811x 30811x 30811x 30811x 30811x 30811x 30811x 30811x 30811x 10325x 10325x 9811x 9811x 10325x 10325x 241x 241x 10325x 10325x 269x 269x 269x 269x 269x 12x 12x 257x 269x 23x 2x 2x 21x 23x 23x 6x 6x 15x 23x 7x 7x 8x 23x 23x     8x 23x     8x 23x 23x 2x 2x 23x 23x 23x 23x 1x 1x 7x 23x 23x 23x 23x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 23x 257x 257x 269x 257x 3x 3x 3x 3x 3x 3x 3x 3x 3x 257x 257x 257x 257x 10325x 10325x 10325x
import {Issue} from "../issue";
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import * as Structures from "../abap/3_structures/structures";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Version} from "../version";
import {IRuleMetadata, RuleTag} from "./_irule";
import {EditHelper} from "../edit_helper";
import {ABAPFile} from "../abap/abap_file";
 
// note this rule assumes abap_true and abap_false is used for boolean variables
// some other rule will in the future find assignments to abap_bool that are not abap_true/abap_false/abap_undefined
 
export class UseBoolExpressionConf extends BasicRuleConfig {
}
 
export class UseBoolExpression extends ABAPRule {
  private conf = new UseBoolExpressionConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "use_bool_expression",
      title: "Use boolean expression",
      shortDescription: `Use boolean expression, xsdbool from 740sp08 and up, boolc from 702 and up`,
      extendedInformation:
        `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#use-xsdbool-to-set-boolean-variables`,
      tags: [RuleTag.Upport, RuleTag.Styleguide, RuleTag.Quickfix, RuleTag.SingleFile],
      badExample: `IF line IS INITIAL.
  has_entries = abap_false.
ELSE.
  has_entries = abap_true.
ENDIF.
 
DATA(fsdf) = COND #( WHEN foo <> bar THEN abap_true ELSE abap_false ).`,
      goodExample: `DATA(has_entries) = xsdbool( line IS NOT INITIAL ).
 
DATA(fsdf) = xsdbool( foo <> bar ).`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: UseBoolExpressionConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
    const stru = file.getStructure();
 
    const version = this.reg.getConfig().getVersion();
    if (stru === undefined || (version < Version.v702 && version !== Version.Cloud)) {
      return [];
    }
 
    for (const i of stru.findAllStructures(Structures.If)) {
      if (i.findDirectStructure(Structures.ElseIf) !== undefined) {
        continue;
      }
 
      const bodyNodes = i.findDirectStructure(Structures.Body)?.findAllStatementNodes();
      if (bodyNodes === undefined || bodyNodes.length !== 1) {
        continue;
      }
      const bodyStatement = bodyNodes[0];
      if (!(bodyStatement.get() instanceof Statements.Move)) {
        continue;
      }
 
      const elseNodes = i.findDirectStructure(Structures.Else)?.findDirectStructure(Structures.Body)?.findAllStatementNodes();
      if (elseNodes === undefined || elseNodes.length !== 1) {
        continue;
      }
      const elseStatement = elseNodes[0];
      if (!(elseStatement.get() instanceof Statements.Move)) {
        continue;
      }
 
      let bodyTarget = bodyStatement.findFirstExpression(Expressions.Target)?.concatTokens();
      if (bodyTarget?.startsWith("DATA(")) {
        bodyTarget = bodyTarget.substr(5, bodyTarget.length - 6);
      }
      const elseTarget = elseStatement.findFirstExpression(Expressions.Target)?.concatTokens();
      if (bodyTarget === undefined
          || elseTarget === undefined
          || bodyTarget.toUpperCase() !== elseTarget.toUpperCase()) {
        continue;
      }
 
      const bodySource = bodyStatement.findFirstExpression(Expressions.Source)?.concatTokens().toUpperCase();
      const elseSource = elseStatement.findFirstExpression(Expressions.Source)?.concatTokens().toUpperCase();
      if ((bodySource === "ABAP_TRUE" && elseSource === "ABAP_FALSE")
          || (bodySource === "ABAP_FALSE" && elseSource === "ABAP_TRUE")) {
        const func = ( this.reg.getConfig().getVersion() >= Version.v740sp08
          || this.reg.getConfig().getVersion() === Version.Cloud ) ? "xsdbool" : "boolc";
        const negate = bodySource === "ABAP_FALSE";
        const message = `Use ${func} instead of IF` + (negate ? ", negate expression" : "");
        const start = i.getFirstToken().getStart();
        const end = i.getLastToken().getEnd();
 
        const statement = bodyTarget + " = " + func + "( " +
          (negate ? "NOT ( " : "") +
          i.findFirstStatement(Statements.If)?.findFirstExpression(Expressions.Cond)?.concatTokens() +
          (negate ? " )" : "") +
          " ).";
        const fix = EditHelper.replaceRange(file, start, end, statement);
        issues.push(Issue.atRange(file, start, end, message, this.getMetadata().key, this.conf.severity, fix));
      }
    }
 
 
    if (version >= Version.v740sp08 || version === Version.Cloud) {
      for (const b of stru.findAllExpressions(Expressions.CondBody)) {
        const concat = b.concatTokens().toUpperCase();
        if (concat.endsWith(" THEN ABAP_TRUE ELSE ABAP_FALSE")
            || concat.endsWith(" THEN ABAP_TRUE")
            || concat.endsWith(" THEN ABAP_FALSE ELSE ABAP_TRUE")) {
          const message = "Use xsdbool";
          // eslint-disable-next-line max-len
          issues.push(Issue.atRange(file, b.getFirstToken().getStart(), b.getLastToken().getEnd(), message, this.getMetadata().key, this.conf.severity));
        }
      }
    }
 
    return issues;
  }
 
 
}