All files / src/rules unnecessary_pragma.ts

98.46% Statements 193/196
98.55% Branches 68/69
100% Functions 10/10
98.46% Lines 193/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 20689x 20689x 20689x 20689x 20689x 1x 10348x 10348x 10348x 10348x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 30862x 10348x 10348x 11319x 11319x 10348x 10348x 247x 247x 10348x 10348x 272x 272x 272x 272x 272x 272x 1510x 1510x 1510x 1510x 8x 1510x 1502x 169x 66x 66x 1502x 1333x 284x 1333x 25x 1049x 2x 2x 2x 2x 1024x 1022x 1022x 1485x 1510x 3x 1510x 1482x 1482x 1485x 1485x 1485x 1510x 1282x 1282x 1510x 272x 272x 272x 10348x 10348x 1482x 1482x 1478x 1478x 4x 4x 1482x 3x 3x 3x 3x 1x 1x 1x 10348x 10348x 1282x 1282x 1281x 1281x 1x 1x 1x 1x 1x 1x 1x       10348x 10348x 1485x 1485x 1479x 1479x 6x 6x 6x 1485x 1485x 1485x 1485x 1485x 1485x 1485x 1485x 1485x 1485x 1485x 1485x 1485x 1485x 1485x 1x 1x 1x 1x 5x 5x 5x 10348x 10348x 1022x 15x 3x 3x 15x 1019x 1019x 1022x 1022x 1022x 1x 1x 1018x 1018x 10348x 10348x
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;
  }
 
}