All files / src/rules unnecessary_pragma.ts

98.12% Statements 157/160
98.03% Branches 50/51
100% Functions 9/9
98.12% Lines 157/160

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 1601x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9361x 9361x 9361x 9361x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 27929x 9361x 9361x 8902x 8902x 9361x 9361x 216x 216x 9361x 9361x 244x 244x 244x 244x 244x 1383x 1383x 1383x 1383x 8x 1383x 24x 1375x 2x 2x 2x 2x 1351x 1349x 1349x 1359x 1359x 1359x 1359x 1359x 244x 244x 244x 9361x 9361x 1359x 1359x 1356x 1356x 3x 3x 1359x 2x 2x 2x 1x 1x 1x 9361x 9361x 1359x 1359x 1358x 1358x 1x 1x 1x 1x 1x 1x       9361x 9361x 1359x 1359x 1357x 1357x 2x 2x 2x 1359x 1359x 1359x 1359x 1359x 1359x 1359x 1359x 1359x 1359x 1359x 1359x 1x 1x 1x 1x 1x 1x 9361x 9361x 1349x 9x 3x 3x 9x 1346x 1349x 1349x 1x 1x 1345x 1345x 9361x 9361x
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} from "../abap/2_statements/statements/_statement";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
 
export class UnnecessaryPragmaConf extends BasicRuleConfig {
}
 
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`,
      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.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: UnnecessaryPragmaConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
    let noHandler: 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 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]);
      }
 
      issues.push(...this.checkText(statement, file));
      issues.push(...this.checkNeeded(statement, file));
      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";
      return [Issue.atToken(file, p, message, this.getMetadata().key, this.getConfig().severity)];
    }
 
    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";
      return [Issue.atToken(file, p, message, this.getMetadata().key, this.getConfig().severity)];
    }

    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.DataEnd)
        && !(statement.get() instanceof Statements.Type)
        && !(statement.get() instanceof Statements.Form)
        && !(statement.get() instanceof Statements.TypeEnd)
        && !(statement.get() instanceof Statements.Constant)
        && !(statement.get() instanceof Statements.ConstantEnd)
        && !(statement.get() instanceof Statements.TypeEnum)
        && !(statement.get() instanceof Statements.TypeEnumEnd)
        && !(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";
      return [Issue.atToken(file, p, message, this.getMetadata().key, this.getConfig().severity)];
    }
 
    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
        && next.concatTokens().toUpperCase().includes("#EC NO_HANDLER")) {
      return true;
    }
    return false;
  }
 
}