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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11817x 11817x 11817x 11817x 11817x 35313x 35313x 35313x 35313x 35313x 35313x 35313x 35313x 35313x 35313x 35313x 35313x 35313x 35313x 35313x 35313x 11817x 11817x 11279x 11279x 11817x 11817x 253x 253x 11817x 11817x 284x 284x 284x 284x 12x 12x 272x 284x 11x 11x 2x 2x 9x 9x 11x 9x 9x 11x 9x 9x 11x 1x 1x 8x 8x 11x 3x 3x 5x 5x 5x 11x 5x 11x 1x 1x 4x 11x 4x 4x 11x 4x 11x 4x 11x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 272x 272x 272x 11817x 11817x | import {Issue} from "../issue";
import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import * as Structures from "../abap/3_structures/structures";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {EditHelper} from "../edit_helper";
export class UnnecessaryLoopGuardConf extends BasicRuleConfig {
}
export class UnnecessaryLoopGuard extends ABAPRule {
private conf = new UnnecessaryLoopGuardConf();
public getMetadata(): IRuleMetadata {
return {
key: "unnecessary_loop_guard",
title: "Unnecessary loop guard",
shortDescription: `Detects unnecessary IS [NOT] INITIAL check before LOOP AT`,
extendedInformation: `LOOP AT iterates zero times on an empty table, so an IS [NOT] INITIAL guard is redundant.`,
tags: [RuleTag.SingleFile, RuleTag.Styleguide, RuleTag.Quickfix],
badExample: `IF lt_data IS NOT INITIAL.
LOOP AT lt_data INTO DATA(ls_item).
WRITE ls_item-name.
ENDLOOP.
ENDIF.`,
goodExample: `LOOP AT lt_data INTO DATA(ls_item).
WRITE ls_item-name.
ENDLOOP.`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: UnnecessaryLoopGuardConf): void {
this.conf = conf;
}
public runParsed(file: ABAPFile) {
const issues: Issue[] = [];
const stru = file.getStructure();
if (stru === undefined) {
return issues;
}
for (const ifStru of stru.findAllStructuresRecursive(Structures.If)) {
if (ifStru.findDirectStructures(Structures.ElseIf).length > 0
|| ifStru.findDirectStructures(Structures.Else).length > 0) {
continue;
}
const ifStatement = ifStru.findDirectStatement(Statements.If);
if (ifStatement === undefined) {
continue;
}
const cond = ifStatement.findDirectExpression(Expressions.Cond);
if (cond === undefined) {
continue;
}
const compareNodes = cond.findDirectExpressions(Expressions.Compare);
if (compareNodes.length !== 1) {
continue;
}
const m = compareNodes[0].concatTokens().toUpperCase().match(/^(.+?)\s+IS\s+(?:NOT\s+)?INITIAL\s*$/);
if (!m) {
continue;
}
const tableName = m[1];
const bodyStructures = ifStru.findDirectStructures(Structures.Body);
if (bodyStructures.length !== 1) {
continue;
}
const normalStructures = bodyStructures[0].findDirectStructures(Structures.Normal);
if (normalStructures.length !== 1) {
continue;
}
const loopStructures = normalStructures[0].findDirectStructures(Structures.Loop);
if (loopStructures.length !== 1) {
continue;
}
const loopStatement = loopStructures[0].findDirectStatement(Statements.Loop);
if (loopStatement === undefined) {
continue;
}
const loopSource = loopStatement.findFirstExpression(Expressions.LoopSource);
if (loopSource === undefined) {
continue;
}
if (loopSource.concatTokens().toUpperCase() !== tableName) {
continue;
}
const endifStatement = ifStru.findDirectStatement(Statements.EndIf)!;
const fix = EditHelper.merge(EditHelper.deleteStatement(file, ifStatement), EditHelper.deleteStatement(file, endifStatement));
issues.push(Issue.atStatement(
file,
ifStatement,
"Unnecessary IS [NOT] INITIAL check before LOOP AT",
this.getMetadata().key,
this.conf.severity,
fix,
));
}
return issues;
}
}
|