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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22910x 22910x 22910x 22910x 22910x 1x 11459x 11459x 11459x 11459x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 34222x 11459x 11459x 12502x 12502x 11459x 11459x 254x 254x 11459x 11459x 288x 288x 288x 288x 288x 288x 1601x 1601x 1601x 1601x 1601x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1599x 1601x 5x 1601x 1594x 187x 66x 66x 1594x 1407x 314x 1407x 29x 1093x 2x 2x 2x 2x 1064x 1062x 1062x 1570x 1601x 3x 1601x 1567x 1567x 1570x 1570x 1570x 1601x 1367x 1367x 1601x 288x 288x 288x 11459x 11459x 1567x 1567x 1563x 1563x 4x 4x 1567x 3x 3x 3x 3x 1x 1x 1x 11459x 11459x 1367x 1367x 1365x 1365x 2x 1367x 1367x 1x 1x 1x 1x 1x 1x 1x 11459x 11459x 1570x 1570x 1564x 1564x 6x 6x 6x 1570x 1570x 1570x 1570x 1570x 1570x 1570x 1570x 1570x 1570x 1570x 1570x 1570x 1570x 1570x 1x 1x 1x 1x 5x 5x 5x 11459x 11459x 1062x 16x 3x 3x 16x 1059x 1059x 1062x 1062x 1062x 1x 1x 1058x 1058x 11459x 11459x | import {Issue} from "../issue";
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {StatementNode} from "../abap/nodes";
import {Comment, Empty, MacroContent} from "../abap/2_statements/statements/_statement";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {EditHelper} from "../edit_helper";
import {Pragma} from "../abap/1_lexer/tokens";
export class UnnecessaryPragmaConf extends BasicRuleConfig {
/** Allow NO_TEXT in global CLAS and INTF definitions,
its added automatically by SE24 in some cases where it should not */
public allowNoTextGlobal?: boolean = false;
}
export class UnnecessaryPragma extends ABAPRule {
private conf = new UnnecessaryPragmaConf();
public getMetadata(): IRuleMetadata {
return {
key: "unnecessary_pragma",
title: "Unnecessary Pragma",
shortDescription: `Finds pragmas which can be removed`,
extendedInformation: `* NO_HANDLER with handler
* NEEDED without definition
* NO_TEXT without texts
* SUBRC_OK where sy-subrc is checked
NO_HANDLER inside macros are not checked`,
tags: [RuleTag.SingleFile],
badExample: `TRY.
...
CATCH zcx_abapgit_exception ##NO_HANDLER.
RETURN. " it has a handler
ENDTRY.
MESSAGE w125(zbar) WITH c_foo INTO message ##NEEDED ##NO_TEXT.
SELECT SINGLE * FROM tadir INTO @DATA(sdfs) ##SUBRC_OK.
IF sy-subrc <> 0.
ENDIF.`,
goodExample: `TRY.
...
CATCH zcx_abapgit_exception.
RETURN.
ENDTRY.
MESSAGE w125(zbar) WITH c_foo INTO message.
SELECT SINGLE * FROM tadir INTO @DATA(sdfs).
IF sy-subrc <> 0.
ENDIF.
DATA: BEGIN OF blah ##NEEDED,
test1 TYPE string,
test2 TYPE string,
END OF blah.`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: UnnecessaryPragmaConf) {
this.conf = conf;
}
public runParsed(file: ABAPFile) {
const issues: Issue[] = [];
let noHandler: boolean = false;
let globalDefinition: boolean = false;
const statements = file.getStatements();
for (let i = 0; i < statements.length; i++) {
const statement = statements[i];
const nextStatement = statements[i + 1];
if (statement.get() instanceof Empty
&& statement.getChildren().length === 1) {
const tokens = statement.getTokens();
if (tokens.length === 1
&& tokens[0] instanceof Pragma) {
const message = "Pragma without a statement can be removed";
const fix = EditHelper.deleteToken(file, tokens[0]);
issues.push(Issue.atToken(file, tokens[0], message, this.getMetadata().key, this.conf.severity, fix));
continue;
}
}
if (statement.get() instanceof Statements.EndTry) {
noHandler = false;
} else if (statement.get() instanceof Statements.ClassDefinition
|| statement.get() instanceof Statements.Interface) {
if (statement.findDirectExpression(Expressions.ClassGlobal)) {
globalDefinition = true;
}
} else if (statement.get() instanceof Statements.EndClass
|| statement.get() instanceof Statements.EndInterface) {
globalDefinition = false;
} else if (statement.get() instanceof Comment) {
continue;
} else if (noHandler === true && !(statement.get() instanceof Statements.Catch)) {
const message = "NO_HANDLER pragma or pseudo comment can be removed";
const issue = Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity);
issues.push(issue);
noHandler = false;
} else {
noHandler = this.containsNoHandler(statement, statements[i + 1]);
}
if (this.getConfig().allowNoTextGlobal === true && globalDefinition === true) {
// skip
} else {
issues.push(...this.checkText(statement, file));
}
issues.push(...this.checkNeeded(statement, file));
if (globalDefinition === false) {
issues.push(...this.checkSubrc(statement, nextStatement, file));
}
}
return issues;
}
private checkText(statement: StatementNode, file: ABAPFile): Issue[] {
const p = statement.getPragmas().find(t => t.getStr().toUpperCase() === "##NO_TEXT");
if (p === undefined) {
return [];
}
if (statement.findFirstExpression(Expressions.ConstantString) === undefined
&& statement.findFirstExpression(Expressions.StringTemplate) === undefined) {
const message = "There is no text, NO_TEXT can be removed";
const fix = EditHelper.deleteToken(file, p);
return [Issue.atToken(file, p, message, this.getMetadata().key, this.getConfig().severity, fix)];
}
return [];
}
private checkSubrc(statement: StatementNode, next: StatementNode, file: ABAPFile): Issue[] {
const p = statement.getPragmas().find(t => t.getStr().toUpperCase() === "##SUBRC_OK");
if (p === undefined) {
return [];
}
const concat = next?.concatTokens().toUpperCase() || "";
if (concat.includes(" SY-SUBRC")) {
const message = "SUBRC_OK can be removed as sy-subrc is checked";
const fix = EditHelper.deleteToken(file, p);
return [Issue.atToken(file, p, message, this.getMetadata().key, this.getConfig().severity, fix)];
}
return [];
}
private checkNeeded(statement: StatementNode, file: ABAPFile): Issue[] {
const p = statement.getPragmas().find(t => t.getStr().toUpperCase() === "##NEEDED");
if (p === undefined) {
return [];
}
if (statement.findFirstExpression(Expressions.InlineData) === undefined
&& !(statement.get() instanceof Statements.Parameter)
&& !(statement.get() instanceof Statements.Data)
&& !(statement.get() instanceof Statements.DataBegin)
&& !(statement.get() instanceof Statements.ClassData)
&& !(statement.get() instanceof Statements.ClassDataBegin)
&& !(statement.get() instanceof Statements.Type)
&& !(statement.get() instanceof Statements.Form)
&& !(statement.get() instanceof Statements.Tables)
&& !(statement.get() instanceof Statements.TypeBegin)
&& !(statement.get() instanceof Statements.Constant)
&& !(statement.get() instanceof Statements.ConstantBegin)
&& !(statement.get() instanceof Statements.TypeEnum)
&& !(statement.get() instanceof Statements.TypeEnumBegin)
&& !(statement.get() instanceof Statements.MethodImplementation)
&& !(statement.get() instanceof Statements.MethodDef)
&& statement.findFirstExpression(Expressions.InlineFS) === undefined) {
const message = "There is no data definition, NEEDED can be removed";
const fix = EditHelper.deleteToken(file, p);
return [Issue.atToken(file, p, message, this.getMetadata().key, this.getConfig().severity, fix)];
}
return [];
}
private containsNoHandler(statement: StatementNode, next: StatementNode | undefined): boolean {
for (const t of statement.getPragmas()) {
if (t.getStr().toUpperCase() === "##NO_HANDLER") {
return true;
}
}
if (next
&& next.get() instanceof Comment
&& !(statement.get() instanceof MacroContent)
&& next.concatTokens().toUpperCase().includes("#EC NO_HANDLER")) {
return true;
}
return false;
}
}
|