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 21997x 21997x 21997x 21997x 1x 10999x 10999x 10999x 10999x 10999x 32810x 32810x 32810x 32810x 32810x 32810x 32810x 32810x 32810x 32810x 32810x 32810x 32810x 32810x 10999x 10999x 10447x 10447x 10999x 10999x 261x 261x 10999x 10999x 288x 288x 288x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 4x 4x 30x 30x 34x 30x 34x 5x 1x 1x 1x 1x 1x 5x 5x 25x 25x 25x 25x 25x 25x 34x 34x 4x 4x 4x 4x 4x 34x 288x 288x 288x 10999x 10999x 29x 29x 29x 29x 10999x 10999x 6x 6x 6x 6x 10999x 10999x 49x 49x 10999x 10999x | 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(" "); } } |