All files / src/rules no_yoda_conditions.ts

94.59% Statements 105/111
83.78% Branches 31/37
100% Functions 9/9
94.59% Lines 105/111

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 1121x 1x 1x 1x 1x 1x 1x 1x 17845x 17845x 17845x 17845x 1x 8923x 8923x 8923x 8923x 8923x 26614x 26614x 26614x 26614x 26614x 26614x 26614x 26614x 26614x 26614x 26614x 26614x 26614x 26614x 8923x 8923x 8485x 8485x 8923x 8923x 203x 203x 8923x 8923x 231x 231x 231x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 4x 4x 28x 28x 32x     28x 32x 5x 1x 1x 1x 1x 1x 5x 5x 23x 23x 23x 23x 23x 23x 32x 32x 4x 4x 4x 4x 4x 32x 231x 231x 231x 8923x 8923x 27x     27x 27x 27x 8923x 8923x 5x     5x 5x 5x 8923x 8923x 45x 45x 8923x 8923x  
import * as Expressions from "../abap/2_statements/expressions";
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {ExpressionNode} from "../abap/nodes";
 
export class NoYodaConditionsConf extends BasicRuleConfig {
  /** Only report issues where the left side is a constant */
  public onlyConstants: boolean = false;
}
 
export class NoYodaConditions extends ABAPRule {
 
  private conf = new NoYodaConditionsConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "no_yoda_conditions",
      title: "No Yoda conditions",
      shortDescription: `Finds Yoda conditions and reports issues`,
      extendedInformation: `https://en.wikipedia.org/wiki/Yoda_conditions
 
Conditions with operators CP, NP, CS, NS, CA, NA, CO, CN are ignored`,
      tags: [RuleTag.SingleFile],
      badExample: `IF 0 <> sy-subrc.
ENDIF.`,
      goodExample: `IF sy-subrc <> 0.
ENDIF.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: NoYodaConditionsConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    for (const c of file.getStructure()?.findAllExpressions(Expressions.Compare) || []) {
      const operator = c.findDirectExpression(Expressions.CompareOperator)?.concatTokens().toUpperCase();
      if (operator === undefined
          || operator === "CP"
          || operator === "NP"
          || operator === "CS"
          || operator === "NS"
          || operator === "CA"
          || operator === "NA"
          || operator === "CO"
          || operator === "CN") {
        continue;
      }
 
      const sources = c.findDirectExpressions(Expressions.Source);
      if (sources.length !== 2) {
        continue;
      }
 
      if (this.conf.onlyConstants === true) {
        if (this.isConstant(sources[0]) === true && this.isConstant(sources[1]) === false) {
          const start = sources[0].getFirstToken().getStart();
          const end = sources[1].getLastToken().getEnd();
          const issue = Issue.atRange(file, start, end, "No Yoda conditions", this.getMetadata().key, this.conf.severity);
          issues.push(issue);
        }
        continue;
      }
 
  // Scenarios:
  //   constant COMPARE chain
  //   constant COMPARE multiple tokens with spaces
  //   fieldChain COMPARE multiple tokens with spaces
 
      if ((this.withoutSpaces(sources[0]) === false && this.withoutSpaces(sources[1]) === true) || (
        (this.isConstant(sources[0]) === true && this.isFieldChain(sources[1]) === true))) {
        const start = sources[0].getFirstToken().getStart();
        const end = sources[1].getLastToken().getEnd();
        const issue = Issue.atRange(file, start, end, "No Yoda conditions", this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
    }
 
    return issues;
  }
 
  private isConstant(node: ExpressionNode): boolean {
    if (node.getChildren().length > 1) {
      return false;
    }
 
    return node.getFirstChild()?.get() instanceof Expressions.Constant;
  }
 
  private isFieldChain(node: ExpressionNode): boolean {
    if (node.getChildren().length > 1) {
      return false;
    }
 
    return node.getFirstChild()?.get() instanceof Expressions.FieldChain;
  }
 
  private withoutSpaces(node: ExpressionNode): boolean {
    return node.concatTokensWithoutStringsAndComments().includes(" ");
  }
 
}