All files / src/rules unnecessary_pragma.ts

100% Statements 196/196
100% Branches 72/72
100% Functions 10/10
100% Lines 196/196

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 1961x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21010x 21010x 21010x 21010x 21010x 1x 10509x 10509x 10509x 10509x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 31332x 10509x 10509x 11514x 11514x 10509x 10509x 258x 258x 10509x 10509x 283x 283x 283x 283x 283x 283x 1565x 1565x 1565x 1565x 8x 1565x 1557x 173x 66x 66x 1557x 1384x 289x 1384x 25x 1095x 2x 2x 2x 2x 1070x 1068x 1068x 1540x 1565x 3x 1565x 1537x 1537x 1540x 1540x 1540x 1565x 1337x 1337x 1565x 283x 283x 283x 10509x 10509x 1537x 1537x 1533x 1533x 4x 4x 1537x 3x 3x 3x 3x 1x 1x 1x 10509x 10509x 1337x 1337x 1335x 1335x 2x 1337x 1337x 1x 1x 1x 1x 1x 1x 1x 10509x 10509x 1540x 1540x 1534x 1534x 6x 6x 6x 1540x 1540x 1540x 1540x 1540x 1540x 1540x 1540x 1540x 1540x 1540x 1540x 1540x 1540x 1540x 1x 1x 1x 1x 5x 5x 5x 10509x 10509x 1068x 16x 3x 3x 16x 1065x 1065x 1068x 1068x 1068x 1x 1x 1064x 1064x 10509x 10509x
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, MacroContent} from "../abap/2_statements/statements/_statement";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {EditHelper} from "../edit_helper";
 
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 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;
  }
 
}