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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11817x 11817x 11817x 11817x 80443x 80443x 80443x 80443x 80443x 80443x 80443x 80443x 80443x 80443x 80443x 11817x 11817x 11279x 11279x 11817x 11817x 253x 253x 11817x 11817x 296x 296x 296x 12x 12x 284x 284x 284x 284x 296x 26x 26x 26x 26x 26x 26x 11x 11x 15x 15x 15x 284x 284x 284x 284x 284x 284x 296x 10x 10x 10x 10x 10x 10x 284x 284x 284x 11817x 11817x 284x 284x 284x 284x 284x 284x 284x 284x 284x 284x 284x 161x 161x 20x 20x 161x 120x 120x 21x 21x 161x 161x 21x 21x 284x 284x 284x 11817x 11817x 15x 15x 15x 2x 2x 2x 2x 2x 2x 13x 15x 1x 1x 1x 1x 1x 1x 12x 12x 12x 15x 7x 7x 15x 11817x 11817x 22x 22x 5x 5x 17x 17x 17x 22x 10x 10x 10x 7x 7x 7x 7x 11817x 11817x 2x 2x 2x 2x 10x 10x 3x 3x 3x 10x 2x 2x 2x 10x 3x 3x 3x 3x 3x 3x 10x 2x 11817x 11817x 1x 1x 1x 1x 1x 7x 7x 2x 2x 2x 7x 1x 1x 1x 7x 4x 1x 1x 1x 4x 2x 2x 2x 2x 2x 2x 4x 7x 1x 11817x 11817x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 4x 10x 1x 1x 10x 10x 5x 5x 5x 5x 5x 10x 11817x | import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {EditHelper} from "../edit_helper";
import {ABAPFile} from "../abap/abap_file";
import {ExpressionNode} from "../abap/nodes";
const ABAP_BOOL = "ABAP_BOOL";
const ABAP_TRUE = "abap_true";
const ABAP_FALSE = "abap_false";
const VALID_VALUES = new Set(["ABAP_TRUE", "ABAP_FALSE", "ABAP_UNDEFINED"]);
export class PreferAbapBoolValuesConf extends BasicRuleConfig {
}
export class PreferAbapBoolValues extends ABAPRule {
private conf = new PreferAbapBoolValuesConf();
public getMetadata(): IRuleMetadata {
return {
key: "prefer_abap_bool_values",
title: "Prefer abap_true and abap_false",
shortDescription: `Use abap_true and abap_false instead of 'X' and ' ' for abap_bool variables`,
// eslint-disable-next-line max-len
extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#use-abap_true-abap_false-and-abap_undefined-for-abap_bool-variables`,
tags: [RuleTag.Styleguide, RuleTag.SingleFile, RuleTag.Quickfix],
badExample: `DATA lv_flag TYPE abap_bool.\nlv_flag = 'X'.`,
goodExample: `DATA lv_flag TYPE abap_bool.\nlv_flag = abap_true.`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: PreferAbapBoolValuesConf) {
this.conf = conf;
}
public runParsed(file: ABAPFile): Issue[] {
const issues: Issue[] = [];
const structure = file.getStructure();
if (structure === undefined) {
return [];
}
const boolVars = this.collectBoolVars(file);
// Check Move statements: target = source
for (const stat of structure.findAllStatements(Statements.Move)) {
const target = stat.findFirstExpression(Expressions.Target);
const source = stat.findFirstExpression(Expressions.Source);
if (target === undefined || source === undefined) {
continue;
}
const targetName = target.concatTokens().toUpperCase();
if (!boolVars.has(targetName)) {
continue;
}
this.checkSourceNode(source, file, issues);
}
// Check comparisons: IF/ELSEIF conditions
const compareStatements = [
...structure.findAllStatements(Statements.If),
...structure.findAllStatements(Statements.ElseIf),
];
for (const stat of compareStatements) {
const cond = stat.findFirstExpression(Expressions.Cond);
if (cond === undefined) {
continue;
}
for (const compare of cond.findAllExpressions(Expressions.Compare)) {
this.checkCompareBoolLiteral(compare, boolVars, file, issues);
}
}
return issues;
}
private collectBoolVars(file: ABAPFile): Set<string> {
const boolVars = new Set<string>();
const structure = file.getStructure();
if (structure === undefined) {
return boolVars;
}
const statements = [
...structure.findAllStatements(Statements.Data),
...structure.findAllStatements(Statements.ClassData),
...structure.findAllStatements(Statements.FieldSymbol),
];
for (const stat of statements) {
const typeNameExpr = stat.findFirstExpression(Expressions.TypeName);
if (typeNameExpr === undefined) {
continue;
}
if (typeNameExpr.concatTokens().toUpperCase() !== ABAP_BOOL) {
continue;
}
const nameExpr = stat.findFirstExpression(Expressions.DefinitionName)
?? stat.findFirstExpression(Expressions.FieldSymbol);
if (nameExpr === undefined) {
continue;
}
boolVars.add(nameExpr.concatTokens().toUpperCase());
}
return boolVars;
}
private checkSourceNode(source: ExpressionNode, file: ABAPFile, issues: Issue[]): void {
const firstToken = source.getFirstToken().getStr().toUpperCase();
if (firstToken === "COND") {
const condBody = source.findDirectExpression(Expressions.CondBody);
if (condBody) {
this.checkCondBodyBranches(condBody, file, issues);
}
return;
}
if (firstToken === "SWITCH") {
const switchBody = source.findDirectExpression(Expressions.SwitchBody);
if (switchBody) {
this.checkSwitchBodyBranches(switchBody, file, issues);
}
return;
}
// Plain literal assignment
const issue = this.checkLiteralSource(source, file);
if (issue) {
issues.push(issue);
}
}
private checkLiteralSource(source: ExpressionNode, file: ABAPFile): Issue | undefined {
const constantStr = source.findFirstExpression(Expressions.ConstantString);
if (constantStr === undefined) {
return undefined;
}
const token = constantStr.getFirstToken();
const val = token.getStr();
if (val === "'X'") {
const fix = EditHelper.replaceToken(file, token, ABAP_TRUE);
return Issue.atToken(file, token, `Use ${ABAP_TRUE} instead of 'X'`, this.getMetadata().key, this.conf.severity, fix);
}
if (val === "' '") {
const fix = EditHelper.replaceToken(file, token, ABAP_FALSE);
return Issue.atToken(file, token, `Use ${ABAP_FALSE} instead of ' '`, this.getMetadata().key, this.conf.severity, fix);
}
return undefined;
}
private checkCondBodyBranches(condBody: ExpressionNode, file: ABAPFile, issues: Issue[]): void {
// CondBody: WHEN <cond> THEN <source> [WHEN <cond> THEN <source>]* [ELSE <source>]
// Walk children; Source nodes after THEN/ELSE are the result values
let afterThenOrElse = false;
for (const child of condBody.getChildren()) {
const tokenStr = child.getFirstToken().getStr().toUpperCase();
if (tokenStr === "THEN" || tokenStr === "ELSE") {
afterThenOrElse = true;
continue;
}
if (tokenStr === "WHEN") {
afterThenOrElse = false;
continue;
}
if (afterThenOrElse && child instanceof ExpressionNode && child.get() instanceof Expressions.Source) {
const issue = this.checkLiteralSource(child as ExpressionNode, file);
if (issue) {
issues.push(issue);
}
afterThenOrElse = false;
}
}
}
private checkSwitchBodyBranches(switchBody: ExpressionNode, file: ABAPFile, issues: Issue[]): void {
// SwitchBody: <source> WHEN <val> [OR <val>]* THEN <source> [ELSE <source>]
// First Source is the operand; Source after THEN/ELSE are result values
let firstSourceSeen = false;
let afterThenOrElse = false;
for (const child of switchBody.getChildren()) {
const tokenStr = child.getFirstToken().getStr().toUpperCase();
if (tokenStr === "THEN" || tokenStr === "ELSE") {
afterThenOrElse = true;
continue;
}
if (tokenStr === "WHEN" || tokenStr === "OR") {
afterThenOrElse = false;
continue;
}
if (child instanceof ExpressionNode && child.get() instanceof Expressions.Source) {
if (!firstSourceSeen) {
firstSourceSeen = true;
continue;
}
if (afterThenOrElse) {
const issue = this.checkLiteralSource(child as ExpressionNode, file);
if (issue) {
issues.push(issue);
}
afterThenOrElse = false;
}
}
}
}
private checkCompareBoolLiteral(
compare: ExpressionNode,
boolVars: Set<string>,
file: ABAPFile,
issues: Issue[],
): void {
// Compare: Source CompareOperator Source (or other forms)
const sources = compare.findDirectExpressions(Expressions.Source);
if (sources.length < 2) {
return;
}
const [left, right] = sources;
const leftStr = left.concatTokens().toUpperCase();
const rightStr = right.concatTokens().toUpperCase();
let literalSource: ExpressionNode | undefined;
if (boolVars.has(leftStr) && !VALID_VALUES.has(rightStr)) {
literalSource = right;
} else if (boolVars.has(rightStr) && !VALID_VALUES.has(leftStr)) {
literalSource = left;
}
if (literalSource) {
const issue = this.checkLiteralSource(literalSource, file);
if (issue) {
issues.push(issue);
}
}
}
}
|