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 | 1x 1x 1x 1x 1x 1x 1x 1x 23007x 23007x 23007x 23007x 1x 11504x 11504x 11504x 11504x 11504x 34378x 34378x 34378x 34378x 34378x 34378x 34378x 34378x 34378x 34378x 34378x 34378x 34378x 34378x 11504x 11504x 10974x 10974x 11504x 11504x 250x 250x 11504x 11504x 288x 288x 288x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 4x 4x 14x 14x 18x 14x 18x 5x 1x 1x 1x 1x 1x 5x 5x 9x 9x 9x 9x 9x 9x 18x 18x 4x 4x 4x 4x 4x 18x 288x 288x 288x 11504x 11504x 13x 13x 13x 13x 11504x 11504x 1x 1x 1x 1x 11504x 11504x 17x 17x 11504x 11504x | 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(" ");
}
}
|